From 1049c9614639b4127d1d1bdaf09dea8dbba4b26a Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sun, 19 Apr 2020 02:57:39 +0200 Subject: [PATCH 1/2] implement size_hint on sys iterators --- src/libstd/fs.rs | 4 ++++ src/libstd/path.rs | 2 ++ src/libstd/sys/cloudabi/shims/fs.rs | 4 ++++ src/libstd/sys/cloudabi/shims/net.rs | 3 +++ src/libstd/sys/cloudabi/shims/os.rs | 3 +++ src/libstd/sys/wasm/fs.rs | 4 ++++ src/libstd/sys/wasm/net.rs | 3 +++ src/libstd/sys/wasm/os.rs | 6 ++++++ src/libstd/sys/windows/os.rs | 9 +++++++++ 9 files changed, 38 insertions(+) diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 119bdfcb0f442..6f63dec91d865 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -1370,6 +1370,10 @@ impl Iterator for ReadDir { fn next(&mut self) -> Option> { self.0.next().map(|entry| entry.map(DirEntry)) } + + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } } impl DirEntry { diff --git a/src/libstd/path.rs b/src/libstd/path.rs index b8361d3e82599..02b3124de2541 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -846,6 +846,8 @@ impl<'a> Iterator for Iter<'a> { fn next(&mut self) -> Option<&'a OsStr> { self.inner.next().map(Component::as_os_str) } + + fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libstd/sys/cloudabi/shims/fs.rs b/src/libstd/sys/cloudabi/shims/fs.rs index e6160d1457d26..25d3c6f10f3dc 100644 --- a/src/libstd/sys/cloudabi/shims/fs.rs +++ b/src/libstd/sys/cloudabi/shims/fs.rs @@ -140,6 +140,10 @@ impl Iterator for ReadDir { fn next(&mut self) -> Option> { match self.0 {} } + + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } } impl DirEntry { diff --git a/src/libstd/sys/cloudabi/shims/net.rs b/src/libstd/sys/cloudabi/shims/net.rs index 67c436fa7955d..a5f0407f414e9 100644 --- a/src/libstd/sys/cloudabi/shims/net.rs +++ b/src/libstd/sys/cloudabi/shims/net.rs @@ -299,6 +299,9 @@ impl Iterator for LookupHost { fn next(&mut self) -> Option { match self.0 {} } + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } } impl TryFrom<&str> for LookupHost { diff --git a/src/libstd/sys/cloudabi/shims/os.rs b/src/libstd/sys/cloudabi/shims/os.rs index 779e6d54b7c9f..25eb80aaf189d 100644 --- a/src/libstd/sys/cloudabi/shims/os.rs +++ b/src/libstd/sys/cloudabi/shims/os.rs @@ -43,6 +43,9 @@ impl<'a> Iterator for SplitPaths<'a> { fn next(&mut self) -> Option { match *self.0 {} } + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } } #[derive(Debug)] diff --git a/src/libstd/sys/wasm/fs.rs b/src/libstd/sys/wasm/fs.rs index e6160d1457d26..25d3c6f10f3dc 100644 --- a/src/libstd/sys/wasm/fs.rs +++ b/src/libstd/sys/wasm/fs.rs @@ -140,6 +140,10 @@ impl Iterator for ReadDir { fn next(&mut self) -> Option> { match self.0 {} } + + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } } impl DirEntry { diff --git a/src/libstd/sys/wasm/net.rs b/src/libstd/sys/wasm/net.rs index b7c3108f172f6..bd2e6b0eb9140 100644 --- a/src/libstd/sys/wasm/net.rs +++ b/src/libstd/sys/wasm/net.rs @@ -296,6 +296,9 @@ impl Iterator for LookupHost { fn next(&mut self) -> Option { match self.0 {} } + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } } impl TryFrom<&str> for LookupHost { diff --git a/src/libstd/sys/wasm/os.rs b/src/libstd/sys/wasm/os.rs index 91afdc8a5a0cc..fd8f9cc8e1496 100644 --- a/src/libstd/sys/wasm/os.rs +++ b/src/libstd/sys/wasm/os.rs @@ -33,6 +33,9 @@ impl<'a> Iterator for SplitPaths<'a> { fn next(&mut self) -> Option { match *self.0 {} } + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } } #[derive(Debug)] @@ -70,6 +73,9 @@ impl Iterator for Env { fn next(&mut self) -> Option<(OsString, OsString)> { match self.0 {} } + fn size_hint(&self) -> (usize, Option) { + (0, Some(0)) + } } pub fn env() -> Env { diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs index a0da2498bb7e0..68fcec0ff8d1d 100644 --- a/src/libstd/sys/windows/os.rs +++ b/src/libstd/sys/windows/os.rs @@ -185,6 +185,15 @@ impl<'a> Iterator for SplitPaths<'a> { Some(super::os2path(&in_progress)) } } + + fn size_hint(&self) -> (usize, Option) { + // There will be at most N + 1 entries, where N is the number of + // remaining semicolons. + let data = self.data.clone(); + let semicolons = data.filter(|&b| b == (';' as u16)).count(); + + (0, Some(semicolons + 1)) + } } #[derive(Debug)] From 4c8718457aedc12ca69ceab4ce7c93f91eb79e2b Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sun, 19 Apr 2020 18:21:39 +0200 Subject: [PATCH 2/2] remove from shim fs --- src/libstd/path.rs | 4 +++- src/libstd/sys/cloudabi/shims/fs.rs | 3 --- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 02b3124de2541..53d3ad62f5ec6 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -847,7 +847,9 @@ impl<'a> Iterator for Iter<'a> { self.inner.next().map(Component::as_os_str) } - fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libstd/sys/cloudabi/shims/fs.rs b/src/libstd/sys/cloudabi/shims/fs.rs index 25d3c6f10f3dc..f2a960b16d3ba 100644 --- a/src/libstd/sys/cloudabi/shims/fs.rs +++ b/src/libstd/sys/cloudabi/shims/fs.rs @@ -141,9 +141,6 @@ impl Iterator for ReadDir { match self.0 {} } - fn size_hint(&self) -> (usize, Option) { - (0, Some(0)) - } } impl DirEntry {