From 7843af05a2bc178025bbfdee6ab7f9d37f85efe2 Mon Sep 17 00:00:00 2001 From: Xavientois Date: Fri, 15 Jan 2021 09:53:42 -0500 Subject: [PATCH 1/6] Add size hint for byte iterator over file --- library/std/src/io/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 3f5b7c0b29be6..ceb2d59c31b9c 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -253,6 +253,7 @@ mod tests; use crate::cmp; use crate::fmt; +use crate::fs; use crate::memchr; use crate::ops::{Deref, DerefMut}; use crate::ptr; @@ -2464,6 +2465,20 @@ impl Iterator for Bytes { } } +impl Iterator for Bytes { + type Item = Result; + + fn size_hint(&self) -> (usize, Option) { + match self.inner.metadata() { + Ok(metadata) => { + let file_length = metadata.len() as usize; + (file_length, Some(file_length)) + } + Err(_) => (0, None), + } + } +} + /// An iterator over the contents of an instance of `BufRead` split on a /// particular byte. /// From 0b5090a53463802144f32106a9d5cc0cf09b4676 Mon Sep 17 00:00:00 2001 From: Xavientois Date: Fri, 15 Jan 2021 10:24:32 -0500 Subject: [PATCH 2/6] Remove lower bound and fix compiler error from specialization --- library/std/src/io/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index ceb2d59c31b9c..a6fb4390eb246 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2466,13 +2466,11 @@ impl Iterator for Bytes { } impl Iterator for Bytes { - type Item = Result; - fn size_hint(&self) -> (usize, Option) { match self.inner.metadata() { Ok(metadata) => { let file_length = metadata.len() as usize; - (file_length, Some(file_length)) + (0, Some(file_length)) } Err(_) => (0, None), } From f4e240edafbe50f15773c387f96e7da0fb3edd4f Mon Sep 17 00:00:00 2001 From: Xavientois Date: Fri, 15 Jan 2021 10:31:52 -0500 Subject: [PATCH 3/6] Specify default implementation for parent in order to allow specialization for File --- library/std/src/io/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index a6fb4390eb246..666b07db78a4f 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2463,6 +2463,8 @@ impl Iterator for Bytes { }; } } + + default fn size_hint(&self) -> (usize, Option) {} } impl Iterator for Bytes { From ca124d1c2b0d6011fd3e346ac9a829fb5e5a854b Mon Sep 17 00:00:00 2001 From: Xavientois Date: Fri, 15 Jan 2021 10:39:59 -0500 Subject: [PATCH 4/6] Include default implementation for size_hint --- library/std/src/io/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 666b07db78a4f..29ff0fd8b49da 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2464,7 +2464,9 @@ impl Iterator for Bytes { } } - default fn size_hint(&self) -> (usize, Option) {} + default fn size_hint(&self) -> (usize, Option) { + (0, None) + } } impl Iterator for Bytes { From d90ac4fd8153892acf7b0c656e2c7d39b4616d0f Mon Sep 17 00:00:00 2001 From: Xavientois Date: Fri, 15 Jan 2021 10:54:51 -0500 Subject: [PATCH 5/6] Add stability attribute --- library/std/src/io/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 29ff0fd8b49da..b65b53679a38b 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2469,6 +2469,7 @@ impl Iterator for Bytes { } } +#[unstable(feature = "file_size_hint", reason = "New implementation optimization")] impl Iterator for Bytes { fn size_hint(&self) -> (usize, Option) { match self.inner.metadata() { From d69b3acca4e49ddce058fd177a380bfc0a9f2af8 Mon Sep 17 00:00:00 2001 From: Xavientois Date: Fri, 15 Jan 2021 11:02:01 -0500 Subject: [PATCH 6/6] Remove annotations for unstable attribute --- library/std/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index b65b53679a38b..20cffc09fd2a8 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2469,7 +2469,7 @@ impl Iterator for Bytes { } } -#[unstable(feature = "file_size_hint", reason = "New implementation optimization")] +#[unstable] impl Iterator for Bytes { fn size_hint(&self) -> (usize, Option) { match self.inner.metadata() {