Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Seek::seek_relative #116750

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions library/std/src/io/buffered/bufreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,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> {
Expand Down
26 changes: 26 additions & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,32 @@ pub trait Seek {
fn stream_position(&mut self) -> Result<u64> {
self.seek(SeekFrom::Current(0))
}

/// Seeks relative to the current position.
///
/// This is equivalent to `self.seek(SeekFrom::Current(offset))`.
fintelia marked this conversation as resolved.
Show resolved Hide resolved
///
/// # 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")]
fintelia marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down
Loading