Trait fred::types::FromRedis

source ·
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 for String or Str returns an empty string.
  • impl FromRedis for Bytes or Vec<T> returns an empty array.
  • impl FromRedis for any integer or float type returns 0
  • impl FromRedis for bool returns false
  • impl FromRedis for map or set types return an empty map or set.

Required Methods§

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FromRedis for Value

Available on crate feature serde-json only.
source§

impl FromRedis for bool

source§

impl FromRedis for f32

source§

impl FromRedis for f64

source§

impl FromRedis for i8

source§

impl FromRedis for i16

source§

impl FromRedis for i32

source§

impl FromRedis for i64

source§

impl FromRedis for i128

source§

impl FromRedis for isize

source§

impl FromRedis for u8

source§

impl FromRedis for u16

source§

impl FromRedis for u32

source§

impl FromRedis for u64

source§

impl FromRedis for u128

source§

impl FromRedis for ()

source§

impl FromRedis for usize

source§

impl FromRedis for String

source§

impl FromRedis for Bytes

source§

impl FromRedis for Str

source§

impl<K, V> FromRedis for BTreeMap<K, V>
where K: FromRedisKey + Ord, V: FromRedis,

source§

impl<K, V, S> FromRedis for HashMap<K, V, S>

source§

impl<T> FromRedis for Option<T>
where T: FromRedis,

source§

impl<T> FromRedis for Vec<T>
where T: FromRedis,

source§

impl<T, const N: usize> FromRedis for [T; N]
where T: FromRedis,

source§

impl<V> FromRedis for BTreeSet<V>
where V: FromRedis + Ord,

source§

impl<V, S> FromRedis for HashSet<V, S>
where V: FromRedis + Hash + Eq, S: BuildHasher + Default,

Implementors§

source§

impl FromRedis for RedisValue

source§

impl FromRedis for ClusterInfo

Available on crate feature i-cluster only.
source§

impl FromRedis for DatabaseMemoryStats

Available on crate feature i-memory only.
source§

impl FromRedis for GeoPosition

Available on crate feature i-geo only.
source§

impl FromRedis for MemoryStats

Available on crate feature i-memory only.
source§

impl FromRedis for RedisKey

source§

impl FromRedis for SlowlogEntry

Available on crate feature i-slowlog only.