about summary refs log tree commit diff
path: root/library/std/src/io
diff options
context:
space:
mode:
authorIan Jackson <ijackson@chiark.greenend.org.uk>2021-05-10 12:58:03 +0100
committerIan Jackson <ijackson@chiark.greenend.org.uk>2021-05-10 13:03:49 +0100
commitc3ca148ac05c6a07e95734f9783bd8fea2086789 (patch)
treef3120820506874a1164901c6f0e2d9a9724380a0 /library/std/src/io
parent74e0e45f3c32aef30ab5cdb7bf1980eb087830de (diff)
downloadrust-c3ca148ac05c6a07e95734f9783bd8fea2086789.tar.gz
rust-c3ca148ac05c6a07e95734f9783bd8fea2086789.zip
io::Seek: Provide rewind()
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Diffstat (limited to 'library/std/src/io')
-rw-r--r--library/std/src/io/mod.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs
index 110cb86498e..7165b155eef 100644
--- a/library/std/src/io/mod.rs
+++ b/library/std/src/io/mod.rs
@@ -1669,6 +1669,41 @@ pub trait Seek {
     #[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