[][src]Trait seek_ext::SeekExt

pub trait SeekExt: Seek {
    fn stream_len(&mut self) -> Result<u64> { ... }
fn current_position(&mut self) -> Result<u64> { ... } }

Adds convenience methods to all types that implement io::Seek.

This is an extension trait that has a blanket impl which implements this trait for all T where T: io::Seek. You just need to import this trait into scope and then you can use its methods on all Seek types.

Provided methods

fn stream_len(&mut self) -> Result<u64>

Returns the length (in bytes) of this stream.

This method is implemented using 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 undefined.

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 use its return value (it is also the stream length).

Example

use std::io::{Cursor, Seek, SeekFrom};
use seek_ext::SeekExt;

let mut c = Cursor::new(vec![0; 6]);
let pos_before = c.seek(SeekFrom::Current(4))?;

assert_eq!(c.stream_len()?, 6);
assert_eq!(c.current_position()?, pos_before);

fn current_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::{Cursor, Seek, SeekFrom};
use seek_ext::SeekExt;

let mut c = Cursor::new(vec![0; 6]);

c.seek(SeekFrom::Current(4))?;
assert_eq!(c.current_position()?, 4);

c.seek(SeekFrom::Current(-3))?;
assert_eq!(c.current_position()?, 1);
Loading content...

Implementors

impl<T: Seek> SeekExt for T
[src]

fn stream_len(&mut self) -> Result<u64>
[src]

fn current_position(&mut self) -> Result<u64>
[src]

Loading content...