SlipReader

Struct SlipReader 

Source
pub struct SlipReader<R> { /* private fields */ }
Expand description

Reader wrapper that decodes SLIP frames from an underlying byte stream.

A full streaming example is provided in examples/stream.rs. Use SlipReader::take_remainder to inspect buffered data when a stream ends mid-frame.

Implementations§

Source§

impl<R> SlipReader<R>

Source

pub fn new(inner: R) -> Self

Construct a new SlipReader around the provided source.

Source

pub fn get_ref(&self) -> &R

Borrow the underlying reader.

Source

pub fn get_mut(&mut self) -> &mut R

Borrow the underlying reader mutably.

Source

pub fn into_inner(self) -> R

Consume the wrapper and return the inner reader.

Source

pub fn into_inner_with_remainder(self) -> (R, FrameRemainder)

Consume the wrapper and return both the inner reader and any buffered remainder.

Source§

impl<R: Read> SlipReader<R>

Source

pub fn read_frame_into(&mut self, buffer: &mut Vec<u8>) -> Result<Option<usize>>

Read the next SLIP frame into the supplied buffer.

On success the buffer is populated with the decoded payload and the function returns the frame length. When the end of the underlying reader is reached without another complete frame, Ok(None) is returned.

Source

pub fn read_frame(&mut self) -> Result<Option<Vec<u8>>>

Read the next SLIP frame and return it as a freshly allocated Vec.

Source

pub fn read_frame_length(&mut self) -> Result<Option<usize>>

Read the next SLIP frame and return only its decoded length.

use slipstream::{SlipReader, encode_frame, Result};
use std::io::Cursor;

let encoded = [encode_frame(b"foo"), encode_frame(&[1])].concat();
let mut reader = SlipReader::new(Cursor::new(encoded));
assert_eq!(reader.read_frame_length()?, Some(3));
assert_eq!(reader.read_frame_length()?, Some(1));
assert!(reader.read_frame_length()?.is_none());
Source

pub fn take_remainder(&mut self) -> FrameRemainder

Take ownership of any pending decoded bytes accumulated for the current, incomplete frame.

use slipstream::{SlipReader, encode_frame, SlipError, Result};
use std::io::Cursor;

let mut encoded = encode_frame(b"data");
encoded.pop(); // remove END terminator
let mut reader = SlipReader::new(Cursor::new(encoded));
let mut frame = Vec::new();
assert!(matches!(
    reader.read_frame_into(&mut frame),
    Err(SlipError::UnexpectedEndOfFrame)
));
let remainder = reader.take_remainder();
assert_eq!(remainder.decoded, b"data");
assert!(!remainder.escape_pending);
Source

pub fn has_remainder(&self) -> bool

Check if an incomplete frame is currently buffered.

Auto Trait Implementations§

§

impl<R> Freeze for SlipReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for SlipReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for SlipReader<R>
where R: Send,

§

impl<R> Sync for SlipReader<R>
where R: Sync,

§

impl<R> Unpin for SlipReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for SlipReader<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.