-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add provided methods Seek::{stream_len, stream_position}
#58422
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e8ee00a
Add provided methods `Seek::{stream_len, stream_position}`
LukasKalbertodt 598a1b4
Avoid third seek operation in `Seek::stream_len` when possible
LukasKalbertodt c518f2d
Overwrite Cursor's `Seek::stream_{len, position}` for performance
LukasKalbertodt ea40aa4
Change "undefined" to "unspecified" in `Seek::stream_len` docs
LukasKalbertodt f95219f
Apply suggestions from code review
tbu- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1329,6 +1329,85 @@ pub trait Seek { | |
/// [`SeekFrom::Start`]: enum.SeekFrom.html#variant.Start | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
fn seek(&mut self, pos: SeekFrom) -> Result<u64>; | ||
|
||
/// Returns the length of this stream (in bytes). | ||
/// | ||
/// This method is implemented using up to three seek operations. If this | ||
/// method returns successfully, the seek position is unchanged (i.e. the | ||
/// position before calling this method is the same as afterwards). | ||
/// However, if this method returns an error, the seek position is | ||
/// unspecified. | ||
/// | ||
/// If you need to obtain the length of *many* streams and you don't care | ||
/// about the seek position afterwards, you can reduce the number of seek | ||
/// operations by simply calling `seek(SeekFrom::End(0))` and use its | ||
/// return value (it is also the stream length). | ||
/// | ||
/// Note that length of a stream can change over time (for example, when | ||
/// data is appended to a file). So calling this method multiply times does | ||
LukasKalbertodt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// not necessarily return the same length each time. | ||
/// | ||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// #![feature(seek_convenience)] | ||
/// use std::{ | ||
/// io::{self, Seek}, | ||
/// fs::File, | ||
/// }; | ||
/// | ||
/// fn main() -> io::Result<()> { | ||
/// let mut f = File::open("foo.txt")?; | ||
/// | ||
/// let len = f.stream_len()?; | ||
/// println!("The file is currently {} bytes long", len); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "seek_convenience", issue = "0")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we don't have tracking issue for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
fn stream_len(&mut self) -> Result<u64> { | ||
let old_pos = self.stream_position()?; | ||
let len = self.seek(SeekFrom::End(0))?; | ||
|
||
// Avoid seeking a third time when we were already at the end of the | ||
// stream. The branch is usually way cheaper than a seek operation. | ||
if old_pos != len { | ||
self.seek(SeekFrom::Start(old_pos))?; | ||
} | ||
|
||
Ok(len) | ||
} | ||
|
||
/// Returns the current seek position from the start of the stream. | ||
/// | ||
/// This is equivalent to `self.seek(SeekFrom::Current(0))`. | ||
/// | ||
/// | ||
/// # Example | ||
/// | ||
/// ```no_run | ||
/// #![feature(seek_convenience)] | ||
/// use std::{ | ||
/// io::{self, BufRead, BufReader, Seek}, | ||
/// fs::File, | ||
/// }; | ||
/// | ||
/// fn main() -> io::Result<()> { | ||
/// let mut f = BufReader::new(File::open("foo.txt")?); | ||
/// | ||
/// let before = f.stream_position()?; | ||
/// f.read_line(&mut String::new())?; | ||
/// let after = f.stream_position()?; | ||
/// | ||
/// println!("The first line was {} bytes long", after - before); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "seek_convenience", issue = "0")] | ||
fn stream_position(&mut self) -> Result<u64> { | ||
self.seek(SeekFrom::Current(0)) | ||
} | ||
} | ||
|
||
/// Enumeration of possible methods to seek within an I/O object. | ||
|
@@ -2157,8 +2236,7 @@ impl<B: BufRead> Iterator for Lines<B> { | |
mod tests { | ||
use crate::io::prelude::*; | ||
use crate::io; | ||
use super::Cursor; | ||
use super::repeat; | ||
use super::{Cursor, SeekFrom, repeat}; | ||
|
||
#[test] | ||
#[cfg_attr(target_os = "emscripten", ignore)] | ||
|
@@ -2380,4 +2458,50 @@ mod tests { | |
super::read_to_end(&mut lr, &mut vec) | ||
}); | ||
} | ||
|
||
#[test] | ||
fn seek_len() -> io::Result<()> { | ||
let mut c = Cursor::new(vec![0; 15]); | ||
assert_eq!(c.stream_len()?, 15); | ||
|
||
c.seek(SeekFrom::End(0))?; | ||
let old_pos = c.stream_position()?; | ||
assert_eq!(c.stream_len()?, 15); | ||
assert_eq!(c.stream_position()?, old_pos); | ||
|
||
c.seek(SeekFrom::Start(7))?; | ||
c.seek(SeekFrom::Current(2))?; | ||
let old_pos = c.stream_position()?; | ||
assert_eq!(c.stream_len()?, 15); | ||
assert_eq!(c.stream_position()?, old_pos); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn seek_position() -> io::Result<()> { | ||
// All `asserts` are duplicated here to make sure the method does not | ||
// change anything about the seek state. | ||
let mut c = Cursor::new(vec![0; 15]); | ||
assert_eq!(c.stream_position()?, 0); | ||
assert_eq!(c.stream_position()?, 0); | ||
|
||
c.seek(SeekFrom::End(0))?; | ||
assert_eq!(c.stream_position()?, 15); | ||
assert_eq!(c.stream_position()?, 15); | ||
|
||
|
||
c.seek(SeekFrom::Start(7))?; | ||
c.seek(SeekFrom::Current(2))?; | ||
assert_eq!(c.stream_position()?, 9); | ||
assert_eq!(c.stream_position()?, 9); | ||
|
||
c.seek(SeekFrom::End(-3))?; | ||
c.seek(SeekFrom::Current(1))?; | ||
c.seek(SeekFrom::Current(-5))?; | ||
assert_eq!(c.stream_position()?, 8); | ||
assert_eq!(c.stream_position()?, 8); | ||
|
||
Ok(()) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.