Struct Decoder

Source
pub struct Decoder { /* private fields */ }
Expand description

A streaming RESP decoder.

Implementations§

Source§

impl Decoder

Source

pub fn new() -> Decoder

Creates a new decoder instance for decoding the RESP buffers.

§Examples
let mut decoder = Decoder::new();

let value = Value::Bulk("Hello".to_string());
assert_eq!(decoder.feed(&value.encode()).unwrap(), ());
assert_eq!(decoder.read().unwrap(), value);
assert_eq!(decoder.read(), None);
let value = Value::BufBulk("Hello".to_string().into_bytes());
assert_eq!(decoder.feed(&value.encode()).unwrap(), ());

// Always decode "$" buffers to Value::Bulk even if feed Value::BufBulk buffers
assert_eq!(decoder.read().unwrap(), Value::Bulk("Hello".to_string()));
assert_eq!(decoder.read(), None);
Source

pub fn with_buf_bulk() -> Decoder

Creates a new decoder instance for decoding the RESP buffers. The instance will decode bulk value to buffer bulk.

§Examples
let mut decoder = Decoder::with_buf_bulk();

let value = Value::Bulk("Hello".to_string());
assert_eq!(decoder.feed(&value.encode()).unwrap(), ());

// Always decode "$" buffers to Value::BufBulk even if feed Value::Bulk buffers
assert_eq!(decoder.read().unwrap(), Value::BufBulk("Hello".to_string().into_bytes()));
assert_eq!(decoder.read(), None);
let value = Value::BufBulk("Hello".to_string().into_bytes());
assert_eq!(decoder.feed(&value.encode()).unwrap(), ());
assert_eq!(decoder.read().unwrap(), value);
assert_eq!(decoder.read(), None);
Source

pub fn feed(&mut self, buf: &[u8]) -> Result<(), Error>

Feeds buffers to decoder. The buffer may contain one more values, or be a part of value. You can feed buffer at all times.

§Examples
let mut decoder = Decoder::new();
assert_eq!(decoder.buffer_len(), 0);

let value = Value::Bulk("Test".to_string());
let buf = value.encode();
assert_eq!(decoder.feed(&buf[0..4]).unwrap(), ());
assert_eq!(decoder.read(), None);
assert_eq!(decoder.buffer_len(), 4);
assert_eq!(decoder.result_len(), 0);

assert_eq!(decoder.feed(&buf[4..]).unwrap(), ());
assert_eq!(decoder.buffer_len(), 0);
assert_eq!(decoder.result_len(), 1);
assert_eq!(decoder.read().unwrap(), value);
assert_eq!(decoder.read(), None);
assert_eq!(decoder.buffer_len(), 0);
assert_eq!(decoder.result_len(), 0);
Source

pub fn read(&mut self) -> Option<Value>

Reads a decoded value, will return None if no value decoded.

Source

pub fn buffer_len(&self) -> usize

Returns the buffer’s length that wait for decoding. It usually is 0. Non-zero means that decoder need more buffer.

Source

pub fn result_len(&self) -> usize

Returns decoded values count. The decoded values will be hold by decoder, until you read them.

Trait Implementations§

Source§

impl Debug for Decoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.