Trait borsh::maybestd::io::prelude::Seek 1.0.0[−][src]
The Seek
trait provides a cursor which can be moved within a stream of
bytes.
The stream typically has a fixed size, allowing seeking relative to either end or the current offset.
Examples
File
s implement Seek
:
use std::io; use std::io::prelude::*; use std::fs::File; use std::io::SeekFrom; fn main() -> io::Result<()> { let mut f = File::open("foo.txt")?; // move the cursor 42 bytes from the start of the file f.seek(SeekFrom::Start(42))?; Ok(()) }
Required methods
pub fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
[src]
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 to a negative offset is considered an error.
Provided methods
pub fn stream_len(&mut self) -> Result<u64, Error>
[src]
seek_stream_len
)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
#![feature(seek_stream_len)] use std::{ io::{self, Seek}, fs::File, }; fn main() -> io::Result<()> { let mut f = File::open("foo.txt")?; let len = f.stream_len()?; println!("The file is currently {} bytes long", len); Ok(()) }
pub fn stream_position(&mut self) -> Result<u64, Error>
1.51.0[src]
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(()) }
Implementations on Foreign Types
impl<'_> Seek for &'_ File
[src]
impl Seek for File
[src]
impl<'_, S> Seek for &'_ mut S where
S: Seek + ?Sized,
[src]
Loading content...S: Seek + ?Sized,
Implementors
impl Seek for Empty
1.51.0[src]
pub fn seek(&mut self, _pos: SeekFrom) -> Result<u64, Error>
[src]
pub fn stream_len(&mut self) -> Result<u64, Error>
[src]
pub fn stream_position(&mut self) -> Result<u64, Error>
[src]
impl<R> Seek for BufReader<R> where
R: Seek,
[src]
R: Seek,
pub fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
[src]
Seek to an offset, in bytes, in the underlying reader.
The position used for seeking with SeekFrom::Current
(_)
is the
position the underlying reader would be at if the BufReader<R>
had no
internal buffer.
Seeking always discards the internal buffer, even if the seek position
would otherwise fall within it. This guarantees that calling
BufReader::into_inner()
immediately after a seek yields the underlying reader
at the same position.
To seek without discarding the internal buffer, use BufReader::seek_relative
.
See std::io::Seek
for more details.
Note: In the edge case where you’re seeking with SeekFrom::Current
(n)
where n
minus the internal buffer length overflows an i64
, two
seeks will be performed instead of one. If the second seek returns
Err
, the underlying reader will be left at the same position it would
have if you called seek
with SeekFrom::Current
(0)
.
pub fn stream_position(&mut self) -> Result<u64, Error>
[src]
Returns the current seek position from the start of the stream.
The value returned is equivalent to self.seek(SeekFrom::Current(0))
but does not flush the internal buffer. Due to this optimization the
function does not guarantee that calling .into_inner()
immediately
afterwards will yield the underlying reader at the same position. Use
BufReader::seek
instead if you require that guarantee.
Panics
This function will panic if the position of the inner reader is smaller
than the amount of buffered data. That can happen if the inner reader
has an incorrect implementation of Seek::stream_position
, or if the
position has gone out of sync due to calling Seek::seek
directly on
the underlying reader.
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(()) }
impl<S> Seek for Box<S, Global> where
S: Seek + ?Sized,
[src]
S: Seek + ?Sized,
impl<T> Seek for Cursor<T> where
T: AsRef<[u8]>,
[src]
T: AsRef<[u8]>,
pub fn seek(&mut self, style: SeekFrom) -> Result<u64, Error>
[src]
pub fn stream_len(&mut self) -> Result<u64, Error>
[src]
pub fn stream_position(&mut self) -> Result<u64, Error>
[src]
impl<W> Seek for BufWriter<W> where
W: Write + Seek,
[src]
W: Write + Seek,