[−][src]Struct fcc::ByteSeeker
A Seeker
walks through anything that implements Read
and Seek
to find the position of a certain byte
.
Examples
use std::io::Cursor; use fcc::{ByteSeeker, Result}; fn main() -> Result<()> { // `Cursor` implements `Read` and `Seek`. let mut cursor = Cursor::new(vec![1, 2, b'\n', 3]); let mut seeker = ByteSeeker::new(&mut cursor); let pos = seeker.seek(b'\n')?; assert_eq!(pos, 2); Ok(()) }
Implementations
impl<'a, RS: 'a + Read + Seek> ByteSeeker<'a, RS>
[src]
pub fn new(inner: &'a mut RS) -> Self
[src]
Creates a new ByteSeeker
from something that implements Read
and Seek
.
Examples
use std::io::Cursor; use fcc::{ByteSeeker, Result}; fn main() -> Result<()> { // `Cursor` implements `Read` and `Seek`. let mut cursor = Cursor::new(vec![1, 2, b'\n', 3]); let mut seeker = ByteSeeker::new(&mut cursor); let pos = seeker.seek(b'\n')?; assert_eq!(pos, 2); Ok(()) }
pub fn reset(&mut self)
[src]
Reset the initialized ByteSeeker
to its original state.
Examples
use std::io::Cursor; use fcc::{ByteSeeker, Result}; fn main() -> Result<()> { // `Cursor` implements `Read` and `Seek`. let mut cursor = Cursor::new(vec![1, b'\n', 3, b'\n']); let mut seeker = ByteSeeker::new(&mut cursor); let pos = seeker.seek(b'\n')?; assert_eq!(pos, 1); seeker.reset(); let pos = seeker.seek_back(b'\n')?; assert_eq!(pos, 3); Ok(()) }
pub fn seek_nth(&mut self, byte: u8, nth: usize) -> Result<usize>
[src]
Seeks the nth occurence of a specific byte forwards, and returns the new position from the start of the byte stream.
Errors
If the nth occurence of the given byte cannot be found, an error of
ErrorKind::ByteNotFound
will be returned. If any other IO errors was encountered, an error of
ErrorKind::Io
will be returned.
Examples
use std::io::Cursor; use std::iter; use fcc::ByteSeeker; let bytes: Vec<u8> = iter::repeat(0) .take(100) .chain(iter::repeat(b'\n').take(1)) .chain(iter::repeat(1).take(100)) .chain(iter::repeat(b'\n').take(1)) .chain(iter::repeat(2).take(100)) .collect(); let mut cursor = Cursor::new(bytes); let mut seeker = ByteSeeker::new(&mut cursor); assert_eq!(seeker.seek_nth(b'\n', 2).unwrap(), 100 + 1 + 100);
pub fn seek_nth_back(&mut self, byte: u8, nth: usize) -> Result<usize>
[src]
Seeks the nth occurence of a specific byte backwards, and returns the new position from the start of the byte stream.
Errors
If the nth occurence of the given byte cannot be found, an error of
ErrorKind::ByteNotFound
will be returned. If any other IO errors was encountered, an error of
ErrorKind::Io
will be returned.
Examples
use std::io::Cursor; use std::iter; use fcc::ByteSeeker; let bytes: Vec<u8> = iter::repeat(0) .take(100) .chain(iter::repeat(b'\n').take(1)) .chain(iter::repeat(1).take(100)) .chain(iter::repeat(b'\n').take(1)) .chain(iter::repeat(2).take(100)) .collect(); let mut cursor = Cursor::new(bytes); let mut seeker = ByteSeeker::new(&mut cursor); assert_eq!(seeker.seek_nth_back(b'\n', 2).unwrap(), 100);
pub fn seek(&mut self, byte: u8) -> Result<usize>
[src]
Searches for a specified byte forwards from the last seek
position. If the
initialized ByteSeeker
haven't been called seek
before, seek
will start from
the beginning.
The ByteSeeker
is stateful, which means you can call seek
multiple times until
reaching the end of underlying byte stream.
Errors
If no given byte was found, an error variant of ErrorKind::ByteNotFound
will be returned.
If any other errors were encountered, an error variant of ErrorKind::Io
will be returned.
Examples
use std::io::Cursor; use fcc::{ByteSeeker, Result}; fn main() -> Result<()> { let mut cursor = Cursor::new(vec![b'\n', 0, b'\n']); let mut seeker = ByteSeeker::new(&mut cursor); let pos = seeker.seek(b'\n')?; assert_eq!(pos, 0); let pos = seeker.seek(b'\n')?; assert_eq!(pos, 2); Ok(()) }
pub fn seek_back(&mut self, byte: u8) -> Result<usize>
[src]
Searches for a specified byte backwards from the last seek_back
position. If the
initialized ByteSeeker
haven't been called seek_back
before, seek_back
will start
from the end.
The ByteSeeker
is stateful, which means you can call seek_back
multiple times until
reaching the end of underlying byte stream.
Errors
If no given byte was found, an error variant of ErrorKind::ByteNotFound
will be returned.
If any other errors were encountered, an error variant of ErrorKind::Io
will be returned.
Examples
use std::io::Cursor; use fcc::{ByteSeeker, Result}; fn main() -> Result<()> { let mut cursor = Cursor::new(vec![b'\n', 0, b'\n']); let mut seeker = ByteSeeker::new(&mut cursor); let pos = seeker.seek_back(b'\n')?; assert_eq!(pos, 2); let pos = seeker.seek_back(b'\n')?; assert_eq!(pos, 0); Ok(()) }
Trait Implementations
Auto Trait Implementations
impl<'a, RS> RefUnwindSafe for ByteSeeker<'a, RS> where
RS: RefUnwindSafe,
RS: RefUnwindSafe,
impl<'a, RS> Send for ByteSeeker<'a, RS> where
RS: Send,
RS: Send,
impl<'a, RS> Sync for ByteSeeker<'a, RS> where
RS: Sync,
RS: Sync,
impl<'a, RS> Unpin for ByteSeeker<'a, RS>
impl<'a, RS> !UnwindSafe for ByteSeeker<'a, RS>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,