pub enum Value {
Int(i64),
Str(String),
Err(String),
BStr(Option<String>),
Array(Option<Vec<Value>>),
}
Expand description
In-memory representation of a RESP value.
Variants§
Int(i64)
An integer.
Str(String)
A simple string.
Err(String)
An error.
BStr(Option<String>)
A bulk string.
Array(Option<Vec<Value>>)
An array.
Implementations§
Source§impl Value
impl Value
Sourcepub fn encode(&self) -> String
pub fn encode(&self) -> String
Encodes a Value
as a string.
§Examples
let error = Value::err("ERR");
assert_eq!(error.encode(), "-ERR\r\n");
Sourcepub fn encode_bytes(&self) -> Vec<u8> ⓘ
pub fn encode_bytes(&self) -> Vec<u8> ⓘ
Encodes a Value
as a vector of bytes.
§Examples
let error = Value::err("ERR");
assert_eq!(&error.encode_bytes(), b"-ERR\r\n");
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Checks if a Value
is null.
NOTE: Only the Array
and BStr
types can represent a null value.
§Examples
let name = Value::BStr(None);
assert!(name.is_null());
let name = Value::b_str(Some("Josh"));
assert!(!name.is_null());
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks is a Value
is empty.
§Examples
let error = Value::err("");
assert!(error.is_empty());
let error = Value::err("ERR");
assert!(!error.is_empty())
Null values count as empty too:
let name = Value::BStr(None);
assert!(name.is_empty());
Sourcepub fn int(value: i64) -> Self
pub fn int(value: i64) -> Self
Constructs a new integer value.
NOTE: Using this function has no benefits, it’s simply here for completeness.
§Examples
let age = Value::int(-3);
println!("{:?}", age);
Sourcepub fn b_str<T>(value: Option<T>) -> Selfwhere
T: ToString,
pub fn b_str<T>(value: Option<T>) -> Selfwhere
T: ToString,
Constructs a new bulk string.
§Examples
let b_str = Value::b_str(Some("foobar"));
println!("{:?}", b_str);
Sourcepub fn array(values: Option<Vec<Value>>) -> Self
pub fn array(values: Option<Vec<Value>>) -> Self
Constructs a new array value.
NOTE: Using this function has no benefits, it’s simply here for completeness.
§Examples
let users = Value::array(Some(vec![
Value::b_str(Some("foo")),
Value::b_str(Some("bar")),
Value::b_str(Some("baz"))
]));
println!("{:?}", users);
Trait Implementations§
impl Eq for Value
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 UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more