Skip to content

Commit e50fb63

Browse files
committed
Implement size_hint on various sys iterators
Continuation of #49205
1 parent 1c5283b commit e50fb63

File tree

12 files changed

+63
-0
lines changed

12 files changed

+63
-0
lines changed

src/libstd/fs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,10 @@ impl Iterator for ReadDir {
12601260
fn next(&mut self) -> Option<io::Result<DirEntry>> {
12611261
self.0.next().map(|entry| entry.map(DirEntry))
12621262
}
1263+
1264+
fn size_hint(&self) -> (usize, Option<usize>) {
1265+
self.0.size_hint()
1266+
}
12631267
}
12641268

12651269
impl DirEntry {

src/libstd/path.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,10 @@ impl<'a> Iterator for Iter<'a> {
897897
fn next(&mut self) -> Option<&'a OsStr> {
898898
self.inner.next().map(Component::as_os_str)
899899
}
900+
901+
fn size_hint(&self) -> (usize, Option<usize>) {
902+
self.inner.size_hint()
903+
}
900904
}
901905

902906
#[stable(feature = "rust1", since = "1.0.0")]

src/libstd/sys/cloudabi/shims/fs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ impl Iterator for ReadDir {
150150
fn next(&mut self) -> Option<io::Result<DirEntry>> {
151151
match self.0 {}
152152
}
153+
154+
fn size_hint(&self) -> (usize, Option<usize>) {
155+
(0, Some(0))
156+
}
153157
}
154158

155159
impl DirEntry {

src/libstd/sys/cloudabi/shims/net.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ impl Iterator for LookupHost {
289289
fn next(&mut self) -> Option<SocketAddr> {
290290
match self.0 {}
291291
}
292+
293+
fn size_hint(&self) -> (usize, Option<usize>) {
294+
(0, Some(0))
295+
}
292296
}
293297

294298
pub fn lookup_host(_: &str) -> io::Result<LookupHost> {

src/libstd/sys/cloudabi/shims/os.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ impl<'a> Iterator for SplitPaths<'a> {
5353
fn next(&mut self) -> Option<PathBuf> {
5454
match *self.0 {}
5555
}
56+
57+
fn size_hint(&self) -> (usize, Option<usize>) {
58+
(0, Some(0))
59+
}
5660
}
5761

5862
#[derive(Debug)]

src/libstd/sys/redox/fs.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ impl Iterator for ReadDir {
167167
}
168168
}
169169
}
170+
171+
fn size_hint(&self) -> (usize, Option<usize>) {
172+
// There's at most one entry for every newline; and there are at most
173+
// two skipped entries.
174+
let upper = self.data[(i + 1)..].iter()
175+
.filter(|byte| byte == b'\n')
176+
.count();
177+
let lower = upper.saturating_sub(2);
178+
(lower, Some(upper))
179+
}
170180
}
171181

172182
impl DirEntry {

src/libstd/sys/redox/net/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ impl Iterator for LookupHost {
3636
fn next(&mut self) -> Option<Self::Item> {
3737
self.0.next()
3838
}
39+
40+
fn size_hint(&self) -> (usize, Option<usize>) {
41+
self.0.size_hint()
42+
}
3943
}
4044

4145
pub fn lookup_host(host: &str) -> Result<LookupHost> {

src/libstd/sys/unix/l4re.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ pub mod net {
429429
fn next(&mut self) -> Option<SocketAddr> {
430430
None
431431
}
432+
433+
fn size_hint(&self) -> (usize, Option<usize>) {
434+
(0, Some(0))
435+
}
432436
}
433437

434438
unsafe impl Sync for LookupHost {}

src/libstd/sys/wasm/fs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ impl Iterator for ReadDir {
152152
fn next(&mut self) -> Option<io::Result<DirEntry>> {
153153
match self.0 {}
154154
}
155+
156+
fn size_hint(&self) -> (usize, Option<usize>) {
157+
(0, Some(0))
158+
}
155159
}
156160

157161
impl DirEntry {

src/libstd/sys/wasm/net.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ impl Iterator for LookupHost {
291291
fn next(&mut self) -> Option<SocketAddr> {
292292
match self.0 {}
293293
}
294+
295+
fn size_hint(&self) -> (usize, Option<usize>) {
296+
(0, Some(0))
297+
}
294298
}
295299

296300
pub fn lookup_host(_: &str) -> io::Result<LookupHost> {

src/libstd/sys/wasm/os.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ impl<'a> Iterator for SplitPaths<'a> {
4343
fn next(&mut self) -> Option<PathBuf> {
4444
match *self.0 {}
4545
}
46+
47+
fn size_hint(&self) -> (usize, Option<usize>) {
48+
(0, Some(0))
49+
}
4650
}
4751

4852
#[derive(Debug)]
@@ -77,6 +81,10 @@ impl Iterator for Env {
7781
fn next(&mut self) -> Option<(OsString, OsString)> {
7882
match self.0 {}
7983
}
84+
85+
fn size_hint(&self) -> (usize, Option<usize>) {
86+
(0, Some(0))
87+
}
8088
}
8189

8290
pub fn env() -> Env {

src/libstd/sys/windows/os.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ impl<'a> Iterator for SplitPaths<'a> {
192192
Some(super::os2path(&in_progress))
193193
}
194194
}
195+
196+
fn size_hint(&self) -> (usize, Option<usize>) {
197+
// There will be at most N + 1 entries, where N is the number of
198+
// remaining semicolons.
199+
let data = self.data.clone();
200+
let semicolons = data.filter(|b| b == (';' as u16)).count();
201+
202+
(0, Some(semicolons + 1))
203+
}
195204
}
196205

197206
#[derive(Debug)]

0 commit comments

Comments
 (0)