Skip to content

Commit c3ca148

Browse files
committed
io::Seek: Provide rewind()
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
1 parent 74e0e45 commit c3ca148

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

library/std/src/io/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,41 @@ pub trait Seek {
16691669
#[stable(feature = "rust1", since = "1.0.0")]
16701670
fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
16711671

1672+
/// Rewind to the beginning of a stream.
1673+
///
1674+
/// This is a convenience method, equivalent to `seek(SeekFrom::Start(0)`.
1675+
///
1676+
/// # Errors
1677+
///
1678+
/// Rewinding can fail, for example becaue it might involve flushing a buffer.
1679+
///
1680+
/// # Example
1681+
///
1682+
/// ```no_run
1683+
/// #![feature(seek_rewind)]
1684+
/// use std::io::{Read, Seek, Write};
1685+
/// use std::fs::OpenOptions;
1686+
///
1687+
/// let mut f = OpenOptions::new()
1688+
/// .write(true)
1689+
/// .read(true)
1690+
/// .create(true)
1691+
/// .open("foo.txt").unwrap();
1692+
///
1693+
/// let hello = "Hello!\n";
1694+
/// write!(f, "{}", hello).unwrap();
1695+
/// f.rewind().unwrap();
1696+
///
1697+
/// let mut buf = String::new();
1698+
/// f.read_to_string(&mut buf).unwrap();
1699+
/// assert_eq!(&buf, hello);
1700+
/// ```
1701+
#[unstable(feature = "seek_rewind", issue = "none")]
1702+
fn rewind(&mut self) -> Result<()> {
1703+
self.seek(SeekFrom::Start(0))?;
1704+
Ok(())
1705+
}
1706+
16721707
/// Returns the length of this stream (in bytes).
16731708
///
16741709
/// This method is implemented using up to three seek operations. If this

0 commit comments

Comments
 (0)