Struct respite::RespReader

source ·
pub struct RespReader<Inner: AsyncRead + Unpin> { /* private fields */ }
Expand description

A wrapper for AsyncRead to allow reading a RESP stream, mainly in three ways.

  • Read each frame
  • Read values, possibly made up of multiple frames
  • Read requests like a Redis server

Implementations§

source§

impl<Inner: AsyncRead + Unpin> RespReader<Inner>

source

pub fn new(inner: Inner, config: RespConfig) -> Self

Create a new RespReader from a byte stream and a RespConfig.

source

pub async fn requests<F>(&mut self, f: F)
where F: FnMut(RespRequest),

Call f for each RespRequest received on this stream.

let input = "*2\r\n$1\r\na\r\n$1\r\nb\r\n".as_bytes();
let mut reader = RespReader::new(input, RespConfig::default());
let mut requests = Vec::new();

reader.requests(|request| { requests.push(request); }).await;

assert!(matches!(requests[0], RespRequest::Argument(_)));
assert!(matches!(requests[1], RespRequest::Argument(_)));
assert!(matches!(requests[2], RespRequest::End));
source

pub async fn value(&mut self) -> Result<Option<RespValue>, RespError>

Read the next RespValue from the stream.

let input = "$3\r\nhi!\r\n".as_bytes();
let mut reader = RespReader::new(input, RespConfig::default());
let frame = reader.value().await.unwrap();
assert_eq!(frame, Some(RespValue::String("hi!".into())));
source

pub async fn frame(&mut self) -> Result<Option<RespFrame>, RespError>

Read the next RespFrame from the stream.

let input = "$3\r\nhi!\r\n".as_bytes();
let mut reader = RespReader::new(input, RespConfig::default());
let frame = reader.frame().await.unwrap();
assert_eq!(frame, Some(RespFrame::BlobString("hi!".into())));

Trait Implementations§

source§

impl<Inner: Debug + AsyncRead + Unpin> Debug for RespReader<Inner>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Inner> Freeze for RespReader<Inner>
where Inner: Freeze,

§

impl<Inner> RefUnwindSafe for RespReader<Inner>
where Inner: RefUnwindSafe,

§

impl<Inner> Send for RespReader<Inner>
where Inner: Send,

§

impl<Inner> Sync for RespReader<Inner>
where Inner: Sync,

§

impl<Inner> Unpin for RespReader<Inner>

§

impl<Inner> UnwindSafe for RespReader<Inner>
where Inner: 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>,

§

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>,

§

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.