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

Provide io::Seek::rewind #85146

Merged
merged 4 commits into from
May 10, 2021
Merged
Changes from 2 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
37 changes: 37 additions & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,10 +1663,47 @@ pub trait Seek {
///
/// # Errors
///
/// Seeking can fail, for example becaue it might involve flushing a buffer.
///
/// Seeking to a negative offset is considered an error.
#[stable(feature = "rust1", since = "1.0.0")]
fn seek(&mut self, pos: SeekFrom) -> Result<u64>;

/// Rewind to the beginning of a stream.
///
/// This is a convenience method, equivalent to `seek(SeekFrom::Start(0)`.
///
/// # Errors
///
/// Rewinding can fail, for example becaue it might involve flushing a buffer.
///
/// # Example
///
/// ```no_run
/// #![feature(seek_rewind)]
/// use std::io::{Read, Seek, Write};
/// use std::fs::OpenOptions;
///
/// let mut f = OpenOptions::new()
/// .write(true)
/// .read(true)
/// .create(true)
/// .open("foo.txt").unwrap();
///
/// let hello = "Hello!\n";
/// write!(f, "{}", hello).unwrap();
/// f.rewind().unwrap();
///
/// let mut buf = String::new();
/// f.read_to_string(&mut buf).unwrap();
/// assert_eq!(&buf, hello);
/// ```
#[unstable(feature = "seek_rewind", issue = "none")]
fn rewind(&mut self) -> Result<()> {
self.seek(SeekFrom::Start(0))?;
Ok(())
}

/// Returns the length of this stream (in bytes).
///
/// This method is implemented using up to three seek operations. If this
Expand Down