pub trait Seek {
// Required method
fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
// Provided methods
fn rewind(&mut self) -> Result<()> { ... }
fn stream_len(&mut self) -> Result<u64> { ... }
fn stream_position(&mut self) -> Result<u64> { ... }
fn seek_relative(&mut self, offset: i64) -> Result<()> { ... }
}
Expand description
Required Methods§
1.0.0 · sourcefn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seek to an offset, in bytes, in a stream.
A seek beyond the end of a stream is allowed, but behavior is defined by the implementation.
If the seek operation completed successfully,
this method returns the new position from the start of the stream.
That position can be used later with SeekFrom::Start
.
§Errors
Seeking can fail, for example because it might involve flushing a buffer.
Seeking to a negative offset is considered an error.
Provided Methods§
1.55.0 · sourcefn rewind(&mut self) -> Result<()>
fn rewind(&mut self) -> 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 because it might involve flushing a buffer.
§Example
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);
sourcefn stream_len(&mut self) -> Result<u64>
🔬This is a nightly-only experimental API. (seek_stream_len
#59359)
fn stream_len(&mut self) -> Result<u64>
seek_stream_len
#59359)Returns the length of this stream (in bytes).
This method is implemented using up to three seek operations. If this method returns successfully, the seek position is unchanged (i.e. the position before calling this method is the same as afterwards). However, if this method returns an error, the seek position is unspecified.
If you need to obtain the length of many streams and you don’t care
about the seek position afterwards, you can reduce the number of seek
operations by simply calling seek(SeekFrom::End(0))
and using its
return value (it is also the stream length).
Note that length of a stream can change over time (for example, when data is appended to a file). So calling this method multiple times does not necessarily return the same length each time.
§Example
1.51.0 · sourcefn stream_position(&mut self) -> Result<u64>
fn stream_position(&mut self) -> Result<u64>
Returns the current seek position from the start of the stream.
This is equivalent to self.seek(SeekFrom::Current(0))
.
§Example
use std::{
io::{self, BufRead, BufReader, Seek},
fs::File,
};
fn main() -> io::Result<()> {
let mut f = BufReader::new(File::open("foo.txt")?);
let before = f.stream_position()?;
f.read_line(&mut String::new())?;
let after = f.stream_position()?;
println!("The first line was {} bytes long", after - before);
Ok(())
}