Skip to content

Commit baf3059

Browse files
authored
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 (#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._
2 parents 28345f0 + d9f7c9d commit baf3059

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

library/std/src/io/buffered/bufreader.rs

+10
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,16 @@ impl<R: ?Sized + Seek> Seek for BufReader<R> {
507507
)
508508
})
509509
}
510+
511+
/// Seeks relative to the current position.
512+
///
513+
/// If the new position lies within the buffer, the buffer will not be
514+
/// flushed, allowing for more efficient seeks. This method does not return
515+
/// the location of the underlying reader, so the caller must track this
516+
/// information themselves if it is required.
517+
fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
518+
self.seek_relative(offset)
519+
}
510520
}
511521

512522
impl<T: ?Sized> SizeHint for BufReader<T> {

library/std/src/io/mod.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,36 @@ pub trait Seek {
19571957
fn stream_position(&mut self) -> Result<u64> {
19581958
self.seek(SeekFrom::Current(0))
19591959
}
1960+
1961+
/// Seeks relative to the current position.
1962+
///
1963+
/// This is equivalent to `self.seek(SeekFrom::Current(offset))` but
1964+
/// doesn't return the new position which can allow some implementations
1965+
/// such as [`BufReader`] to perform more efficient seeks.
1966+
///
1967+
/// # Example
1968+
///
1969+
/// ```no_run
1970+
/// #![feature(seek_seek_relative)]
1971+
/// use std::{
1972+
/// io::{self, Seek},
1973+
/// fs::File,
1974+
/// };
1975+
///
1976+
/// fn main() -> io::Result<()> {
1977+
/// let mut f = File::open("foo.txt")?;
1978+
/// f.seek_relative(10)?;
1979+
/// assert_eq!(f.stream_position()?, 10);
1980+
/// Ok(())
1981+
/// }
1982+
/// ```
1983+
///
1984+
/// [`BufReader`]: crate::io::BufReader
1985+
#[unstable(feature = "seek_seek_relative", issue = "117374")]
1986+
fn seek_relative(&mut self, offset: i64) -> Result<()> {
1987+
self.seek(SeekFrom::Current(offset))?;
1988+
Ok(())
1989+
}
19601990
}
19611991

19621992
/// Enumeration of possible methods to seek within an I/O object.

0 commit comments

Comments
 (0)