Enum Value

Source
pub enum Value {
    Null,
    NullArray,
    String(String),
    Error(String),
    Integer(i64),
    Bulk(String),
    BufBulk(Vec<u8>),
    Array(Vec<Value>),
}
Expand description

Represents a RESP value, see Redis Protocol specification.

Variants§

§

Null

Null bulk reply, $-1\r\n

§

NullArray

Null array reply, *-1\r\n

§

String(String)

For Simple Strings the first byte of the reply is “+”.

§

Error(String)

For Errors the first byte of the reply is “-”.

§

Integer(i64)

For Integers the first byte of the reply is “:”.

§

Bulk(String)

For Bulk Strings the first byte of the reply is “$”.

§

BufBulk(Vec<u8>)

For Bulk Strings the first byte of the reply is “$”.

§

Array(Vec<Value>)

For Arrays the first byte of the reply is “*”.

Implementations§

Source§

impl Value

Source

pub fn is_null(&self) -> bool

Returns true if the value is a Null or NullArray. Returns false otherwise.

§Examples
assert_eq!(Value::Null.is_null(), true);
assert_eq!(Value::NullArray.is_null(), true);
assert_eq!(Value::Integer(123).is_null(), false);
Source

pub fn is_error(&self) -> bool

Returns true if the value is a Error. Returns false otherwise.

§Examples
assert_eq!(Value::Null.is_error(), false);
assert_eq!(Value::Error("".to_string()).is_error(), true);
Source

pub fn encode(&self) -> Vec<u8>

Encode the value to RESP binary buffer.

§Examples
let val = Value::String("OK正".to_string());
assert_eq!(val.encode(), vec![43, 79, 75, 230, 173, 163, 13, 10]);
Source

pub fn to_encoded_string(&self) -> Result<String, Error>

Encode the value to RESP string.

§Examples
let val = Value::String("OK正".to_string());
assert_eq!(val.to_encoded_string().unwrap(), "+OK正\r\n");
Source

pub fn to_beautify_string(&self) -> String

Encode the value to beautify formated string.

§Examples
assert_eq!(Value::Null.to_beautify_string(), "(Null)");
assert_eq!(Value::NullArray.to_beautify_string(), "(Null Array)");
assert_eq!(Value::String("OK".to_string()).to_beautify_string(), "OK");
assert_eq!(Value::Error("Err".to_string()).to_beautify_string(), "(Error) Err");
assert_eq!(Value::Integer(123).to_beautify_string(), "(Integer) 123");
assert_eq!(Value::Bulk("Bulk String".to_string()).to_beautify_string(), "\"Bulk String\"");
assert_eq!(Value::BufBulk(vec![]).to_beautify_string(), "(Empty Buffer)");
assert_eq!(Value::BufBulk(vec![0, 100]).to_beautify_string(), "(Buffer) 00 64");
assert_eq!(Value::Array(vec![]).to_beautify_string(), "(Empty Array)");

A full formated example:

 1) (Null)
 2) (Null Array)
 3) OK
 4) (Error) Err
 5) (Integer) 123
 6) \"Bulk String\"
 7) (Empty Array)
 8) (Buffer) 00 64
 9) 1) (Empty Array)
    2) (Integer) 123
    3) \"Bulk String\"
10) 1) (Null)
    2) (Null Array)
    3) OK
    4) (Error) Err
    5) (Integer) 123
    6) \"Bulk String\"
    7) (Empty Array)
    8) (Buffer) 00 64
    9) 1) (Empty Array)
       2) (Integer) 123
       3) \"Bulk String\"
11) (Null)
12) 1) (Null)
    2) (Null Array)
    3) OK
    4) (Error) Err
    5) (Integer) 123
    6) \"Bulk String\"
    7) (Empty Array)
    8) (Buffer) 00 64
    9) 1) (Empty Array)
       2) (Integer) 123
       3) \"Bulk String\"
   10) 1) (Null)
       2) (Null Array)
       3) OK
       4) (Error) Err
       5) (Integer) 123
       6) \"Bulk String\"
       7) (Empty Array)
       8) (Buffer) 00 64
       9) 1) (Empty Array)
          2) (Integer) 123
          3) \"Bulk String\"
   11) (Null)
13) (Null)

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a copy 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<(), Error>

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 Eq for Value

Source§

impl Send for Value

Source§

impl StructuralPartialEq for Value

Source§

impl Sync for Value

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