From 74e0e45f3c32aef30ab5cdb7bf1980eb087830de Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 10 May 2021 12:56:35 +0100 Subject: [PATCH 1/4] io::Seek: Mention that seeking can fail due to buffer flush fail Signed-off-by: Ian Jackson --- library/std/src/io/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 94c70c4f267b1..110cb86498e9a 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1663,6 +1663,8 @@ 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; From c3ca148ac05c6a07e95734f9783bd8fea2086789 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 10 May 2021 12:58:03 +0100 Subject: [PATCH 2/4] io::Seek: Provide rewind() Signed-off-by: Ian Jackson --- library/std/src/io/mod.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 110cb86498e9a..7165b155eef98 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; + /// 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 From 3113b6bd69127a0572a497464864db7d896f07b9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 10 May 2021 13:50:56 +0100 Subject: [PATCH 3/4] Fix typo in doc Co-authored-by: Mara Bos --- library/std/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 7165b155eef98..cebed3a7c1821 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1671,7 +1671,7 @@ pub trait Seek { /// Rewind to the beginning of a stream. /// - /// This is a convenience method, equivalent to `seek(SeekFrom::Start(0)`. + /// This is a convenience method, equivalent to `seek(SeekFrom::Start(0))`. /// /// # Errors /// From 7ae852e349eeb83243a1d1cc4c79c8997723215e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 10 May 2021 13:55:13 +0100 Subject: [PATCH 4/4] io::Seek::rewind: Set tracking issue Signed-off-by: Ian Jackson --- library/std/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index cebed3a7c1821..9f43379aff787 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1698,7 +1698,7 @@ pub trait Seek { /// f.read_to_string(&mut buf).unwrap(); /// assert_eq!(&buf, hello); /// ``` - #[unstable(feature = "seek_rewind", issue = "none")] + #[unstable(feature = "seek_rewind", issue = "85149")] fn rewind(&mut self) -> Result<()> { self.seek(SeekFrom::Start(0))?; Ok(())