about summary refs log tree commit diff
path: root/library/std/src/io/buffered/bufreader.rs
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2023-11-19 04:14:40 +0900
committerGitHub <noreply@github.com>2023-11-19 04:14:40 +0900
commitbaf3059f4e37bdc263a78ed2057b6e2b80cd883b (patch)
tree4581007449364b601a38591f6da7d161fe3c41a4 /library/std/src/io/buffered/bufreader.rs
parent28345f06d785213e6d37de5464c7070a4fc9ca67 (diff)
parentd9f7c9db02c023adfeba554971abbf11bb244994 (diff)
downloadrust-baf3059f4e37bdc263a78ed2057b6e2b80cd883b.tar.gz
rust-baf3059f4e37bdc263a78ed2057b6e2b80cd883b.zip
Rollup merge of #116750 - fintelia:seek_seek_relative, r=Mark-Simulacrum
Add Seek::seek_relative

The `BufReader` struct has a `seek_relative` method because its `Seek::seek` implementation involved dumping the internal buffer (https://github.com/rust-lang/rust/issues/31100).

Unfortunately, there isn't really a good way to take advantage of that method in generic code. This PR adds the same method to the main `Seek` trait with the straightforward default method, and an override for `BufReader` that calls its implementation.

_Also discussed in [this](https://internals.rust-lang.org/t/add-seek-seek-relative/19546) internals.rust-lang.org thread._
Diffstat (limited to 'library/std/src/io/buffered/bufreader.rs')
-rw-r--r--library/std/src/io/buffered/bufreader.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs
index 55aafc3db33..6c7494a6a6f 100644
--- a/library/std/src/io/buffered/bufreader.rs
+++ b/library/std/src/io/buffered/bufreader.rs
@@ -507,6 +507,16 @@ impl<R: ?Sized + Seek> Seek for BufReader<R> {
             )
         })
     }
+
+    /// 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<T: ?Sized> SizeHint for BufReader<T> {