Skip to main content

Value

Enum Value 

Source
pub enum Value {
    SimpleString(Bytes),
    Error(Bytes),
    Integer(i64),
    BulkString(Bytes),
    Null,
    Array(Vec<Value>),
}
Expand description

A RESP protocol value.

This enum supports both RESP2 and RESP3 types. RESP3 types are only available when the resp3 feature is enabled.

Variants§

§

SimpleString(Bytes)

Simple string: +OK\r\n

§

Error(Bytes)

Error: -ERR message\r\n

§

Integer(i64)

Integer: :1000\r\n

§

BulkString(Bytes)

Bulk string: $6\r\nfoobar\r\n

§

Null

Null value. RESP2: $-1\r\n or *-1\r\n RESP3: _\r\n

§

Array(Vec<Value>)

Array: *2\r\n...

Implementations§

Source§

impl Value

Source

pub fn simple_string(s: &[u8]) -> Self

Create a simple string value.

Source

pub fn error(msg: &[u8]) -> Self

Create an error value.

Source

pub fn integer(n: i64) -> Self

Create an integer value.

Source

pub fn bulk_string(data: &[u8]) -> Self

Create a bulk string value.

Source

pub fn null() -> Self

Create a null value.

Source

pub fn array(elements: Vec<Value>) -> Self

Create an array value.

Source

pub fn is_null(&self) -> bool

Returns true if this is a null value.

Source

pub fn is_error(&self) -> bool

Returns true if this is an error value.

Source

pub fn is_simple_string(&self) -> bool

Returns true if this is a simple string.

Source

pub fn is_bulk_string(&self) -> bool

Returns true if this is a bulk string.

Source

pub fn is_integer(&self) -> bool

Returns true if this is an integer.

Source

pub fn is_array(&self) -> bool

Returns true if this is an array.

Source

pub fn as_bytes(&self) -> Option<&[u8]>

Returns the value as bytes if it’s a string type (simple or bulk).

Source

pub fn as_integer(&self) -> Option<i64>

Returns the value as an integer.

Source

pub fn as_array(&self) -> Option<&[Value]>

Returns the value as an array.

Source

pub fn parse(data: &[u8]) -> Result<(Self, usize), ParseError>

Parse a RESP value from a byte buffer.

Returns the parsed value and the number of bytes consumed.

§Errors

Returns ParseError::Incomplete if more data is needed to complete parsing. Returns other errors for malformed data.

Source

pub fn parse_with_options( data: &[u8], options: &ParseOptions, ) -> Result<(Self, usize), ParseError>

Parse a RESP value from raw bytes with custom options.

This allows configuring DoS protection limits like maximum collection size and bulk string length.

Source

pub fn parse_bytes(data: Bytes) -> Result<(Self, usize), ParseError>

Parse a RESP value zero-copy from a Bytes buffer.

String variants (BulkString, SimpleString, etc.) are returned as Bytes::slice() references into the input — no allocation or copy. Returns the parsed value and the number of bytes consumed.

Source

pub fn parse_bytes_with_options( data: Bytes, options: &ParseOptions, ) -> Result<(Self, usize), ParseError>

Parse a RESP value zero-copy from a Bytes buffer with custom options.

Source

pub fn encode(&self, buf: &mut [u8]) -> usize

Encode this value into a byte buffer.

Returns the number of bytes written.

§Panics

Panics if the buffer is too small. Use encoded_len() to check the required size.

Source

pub fn encoded_len(&self) -> usize

Calculate the encoded length of this value.

Source§

impl Value

Source

pub const OK: &'static [u8] = b"+OK\r\n"

OK simple string response.

Source

pub const PONG: &'static [u8] = b"+PONG\r\n"

PONG simple string response.

Source

pub const NULL_BULK: &'static [u8] = b"$-1\r\n"

Null bulk string response.

Source

pub const EMPTY_ARRAY: &'static [u8] = b"*0\r\n"

Empty array response.

Source

pub fn encode_ok(buf: &mut [u8]) -> usize

Encode an OK response directly to a buffer.

Source

pub fn encode_pong(buf: &mut [u8]) -> usize

Encode a PONG response directly to a buffer.

Source

pub fn encode_null_bulk(buf: &mut [u8]) -> usize

Encode a null bulk string response directly to a buffer.

Source

pub fn encode_int(buf: &mut [u8], n: i64) -> usize

Encode an integer response directly to a buffer.

Source

pub fn encode_bulk(buf: &mut [u8], data: &[u8]) -> usize

Encode a bulk string response directly to a buffer (zero-copy for data).

Source

pub fn encode_err(buf: &mut [u8], msg: &[u8]) -> usize

Encode an error response directly to a buffer.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl !Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.