pub trait FromRedis: Sized {
// Required method
fn from_value(value: RedisValue) -> Result<Self, RedisError>;
}
Expand description
A trait used to convert various forms of RedisValue into different types.
§Examples
let foo: usize = RedisValue::String("123".into()).convert()?;
let foo: i64 = RedisValue::String("123".into()).convert()?;
let foo: String = RedisValue::String("123".into()).convert()?;
let foo: Vec<u8> = RedisValue::Bytes(vec![102, 111, 111].into()).convert()?;
let foo: Vec<u8> = RedisValue::String("foo".into()).convert()?;
let foo: Vec<String> = RedisValue::Array(vec!["a".into(), "b".into()]).convert()?;
let foo: HashMap<String, u16> =
RedisValue::Array(vec!["a".into(), 1.into(), "b".into(), 2.into()]).convert()?;
let foo: (String, i64) = RedisValue::Array(vec!["a".into(), 1.into()]).convert()?;
let foo: Vec<(String, i64)> =
RedisValue::Array(vec!["a".into(), 1.into(), "b".into(), 2.into()]).convert()?;
// ...
§Bulk Values
This interface can also convert single-element vectors to scalar values in certain scenarios. This is often
useful with commands that conditionally return bulk values, or where the number of elements in the response
depends on the number of arguments (MGET
, etc).
For example:
let _: String = RedisValue::Array(vec![]).convert()?; // error
let _: String = RedisValue::Array(vec!["a".into()]).convert()?; // "a"
let _: String = RedisValue::Array(vec!["a".into(), "b".into()]).convert()?; // error
let _: Option<String> = RedisValue::Array(vec![]).convert()?; // None
let _: Option<String> = RedisValue::Array(vec!["a".into()]).convert()?; // Some("a")
let _: Option<String> = RedisValue::Array(vec!["a".into(), "b".into()]).convert()?; // error
§The default-nil-types
Feature Flag
By default a nil
value cannot be converted directly into any of the scalar types (u8
, String
, Bytes
,
etc). In practice this often requires callers to use an Option
or Vec
container with commands that can return
nil
.
The default-nil-types
feature flag can enable some further type conversion branches that treat nil
values as
default values for the relevant type. For RedisValue::Null
these include:
impl FromRedis
forString
orStr
returns an empty string.impl FromRedis
forBytes
orVec<T>
returns an empty array.impl FromRedis
for any integer or float type returns0
impl FromRedis
forbool
returnsfalse
impl FromRedis
for map or set types return an empty map or set.
Required Methods§
fn from_value(value: RedisValue) -> Result<Self, RedisError>
Object Safety§
This trait is not object safe.
Implementations on Foreign Types§
source§impl FromRedis for Value
Available on crate feature serde-json
only.
impl FromRedis for Value
Available on crate feature
serde-json
only.fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl FromRedis for bool
impl FromRedis for bool
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl FromRedis for f32
impl FromRedis for f32
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl FromRedis for f64
impl FromRedis for f64
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl FromRedis for i8
impl FromRedis for i8
fn from_value(value: RedisValue) -> Result<i8, RedisError>
source§impl FromRedis for i16
impl FromRedis for i16
fn from_value(value: RedisValue) -> Result<i16, RedisError>
source§impl FromRedis for i32
impl FromRedis for i32
fn from_value(value: RedisValue) -> Result<i32, RedisError>
source§impl FromRedis for i64
impl FromRedis for i64
fn from_value(value: RedisValue) -> Result<i64, RedisError>
source§impl FromRedis for i128
impl FromRedis for i128
fn from_value(value: RedisValue) -> Result<i128, RedisError>
source§impl FromRedis for isize
impl FromRedis for isize
fn from_value(value: RedisValue) -> Result<isize, RedisError>
source§impl FromRedis for u8
impl FromRedis for u8
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl FromRedis for u16
impl FromRedis for u16
fn from_value(value: RedisValue) -> Result<u16, RedisError>
source§impl FromRedis for u32
impl FromRedis for u32
fn from_value(value: RedisValue) -> Result<u32, RedisError>
source§impl FromRedis for u64
impl FromRedis for u64
fn from_value(value: RedisValue) -> Result<u64, RedisError>
source§impl FromRedis for u128
impl FromRedis for u128
fn from_value(value: RedisValue) -> Result<u128, RedisError>
source§impl FromRedis for ()
impl FromRedis for ()
fn from_value(_: RedisValue) -> Result<Self, RedisError>
source§impl FromRedis for usize
impl FromRedis for usize
fn from_value(value: RedisValue) -> Result<usize, RedisError>
source§impl FromRedis for String
impl FromRedis for String
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl FromRedis for Bytes
impl FromRedis for Bytes
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl FromRedis for Str
impl FromRedis for Str
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl<K, V> FromRedis for BTreeMap<K, V>
impl<K, V> FromRedis for BTreeMap<K, V>
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl<K, V, S> FromRedis for HashMap<K, V, S>
impl<K, V, S> FromRedis for HashMap<K, V, S>
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl<T> FromRedis for Option<T>where
T: FromRedis,
impl<T> FromRedis for Option<T>where
T: FromRedis,
fn from_value(value: RedisValue) -> Result<Option<T>, RedisError>
source§impl<T> FromRedis for Vec<T>where
T: FromRedis,
impl<T> FromRedis for Vec<T>where
T: FromRedis,
fn from_value(value: RedisValue) -> Result<Vec<T>, RedisError>
source§impl<T, const N: usize> FromRedis for [T; N]where
T: FromRedis,
impl<T, const N: usize> FromRedis for [T; N]where
T: FromRedis,
fn from_value(value: RedisValue) -> Result<[T; N], RedisError>
source§impl<V> FromRedis for BTreeSet<V>
impl<V> FromRedis for BTreeSet<V>
fn from_value(value: RedisValue) -> Result<Self, RedisError>
source§impl<V, S> FromRedis for HashSet<V, S>
impl<V, S> FromRedis for HashSet<V, S>
fn from_value(value: RedisValue) -> Result<Self, RedisError>
Implementors§
impl FromRedis for RedisValue
impl FromRedis for ClusterInfo
Available on crate feature
i-cluster
only.impl FromRedis for DatabaseMemoryStats
Available on crate feature
i-memory
only.impl FromRedis for GeoPosition
Available on crate feature
i-geo
only.impl FromRedis for MemoryStats
Available on crate feature
i-memory
only.impl FromRedis for RedisKey
impl FromRedis for SlowlogEntry
Available on crate feature
i-slowlog
only.