From d6337e65683ea982006ebfb4cf4881b1d3fe5aad Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sat, 14 Oct 2023 16:20:32 -0700 Subject: [PATCH 1/3] Add Seek::seek_relative --- library/std/src/io/buffered/bufreader.rs | 10 +++++++++ library/std/src/io/mod.rs | 26 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs index 7097dfef88d4e..db4f479d45953 100644 --- a/library/std/src/io/buffered/bufreader.rs +++ b/library/std/src/io/buffered/bufreader.rs @@ -492,6 +492,16 @@ impl Seek for BufReader { ) }) } + + /// Seeks relative to the current position. + /// + /// If the new position lies within the buffer, the buffer will not be + /// flushed, allowing for more efficient seeks. This method does not return + /// the location of the underlying reader, so the caller must track this + /// information themselves if it is required. + fn seek_relative(&mut self, offset: i64) -> io::Result<()> { + self.seek_relative(offset) + } } impl SizeHint for BufReader { diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index e6431abcf8215..ba2c0e217deb1 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1957,6 +1957,32 @@ pub trait Seek { fn stream_position(&mut self) -> Result { self.seek(SeekFrom::Current(0)) } + + /// Seeks relative to the current position. + /// + /// This is equivalent to `self.seek(SeekFrom::Current(offset))`. + /// + /// # Example + /// + /// ```no_run + /// #![feature(seek_seek_relative)] + /// use std::{ + /// io::{self, Seek}, + /// fs::File, + /// }; + /// + /// fn main() -> io::Result<()> { + /// let mut f = File::open("foo.txt")?; + /// f.seek_relative(10)?; + /// assert_eq!(f.stream_position()?, 10); + /// Ok(()) + /// } + /// ``` + #[unstable(feature = "seek_seek_relative", issue = "none")] + fn seek_relative(&mut self, offset: i64) -> Result<()> { + self.seek(SeekFrom::Current(offset))?; + Ok(()) + } } /// Enumeration of possible methods to seek within an I/O object. From bc058b6f45191a9c5af37856c3f5a4d4b2efec77 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sun, 29 Oct 2023 19:11:18 -0700 Subject: [PATCH 2/3] Add tracking issue --- 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 ba2c0e217deb1..9ee68a079bc4a 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1978,7 +1978,7 @@ pub trait Seek { /// Ok(()) /// } /// ``` - #[unstable(feature = "seek_seek_relative", issue = "none")] + #[unstable(feature = "seek_seek_relative", issue = "117374")] fn seek_relative(&mut self, offset: i64) -> Result<()> { self.seek(SeekFrom::Current(offset))?; Ok(()) From d9f7c9db02c023adfeba554971abbf11bb244994 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sat, 4 Nov 2023 15:45:55 -0700 Subject: [PATCH 3/3] Improve documentation --- library/std/src/io/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 9ee68a079bc4a..bd6a989ee9610 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1960,7 +1960,9 @@ pub trait Seek { /// Seeks relative to the current position. /// - /// This is equivalent to `self.seek(SeekFrom::Current(offset))`. + /// This is equivalent to `self.seek(SeekFrom::Current(offset))` but + /// doesn't return the new position which can allow some implementations + /// such as [`BufReader`] to perform more efficient seeks. /// /// # Example /// @@ -1978,6 +1980,8 @@ pub trait Seek { /// Ok(()) /// } /// ``` + /// + /// [`BufReader`]: crate::io::BufReader #[unstable(feature = "seek_seek_relative", issue = "117374")] fn seek_relative(&mut self, offset: i64) -> Result<()> { self.seek(SeekFrom::Current(offset))?;