Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add size hint for byte iterator over file #81044

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2462,6 +2463,23 @@ impl<R: Read> Iterator for Bytes<R> {
};
}
}

default fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}

#[unstable]
impl Iterator for Bytes<fs::File> {
fn size_hint(&self) -> (usize, Option<usize>) {
match self.inner.metadata() {
Ok(metadata) => {
let file_length = metadata.len() as usize;
(0, Some(file_length))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation will never actually help, because in practice only the .0 of the size_hint() is ever used: https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187?u=scottmcm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a shame. Then this implementation would basically just add system call overhead while providing very little benefit :(

}
Err(_) => (0, None),
}
}
}

/// An iterator over the contents of an instance of `BufRead` split on a
Expand Down