pub trait FromRedisValue: Sized {
// Required method
fn from_redis_value(value: &RedisValue) -> RedisResult<Self>;
// Provided method
fn from_redis_u8(_: u8) -> Option<Self> { ... }
}
Expand description
Trait interface requires to implement method to convert RedisValue
into base type.
§Example
use redis_asio::{RedisResult, RedisError, RedisErrorKind, RedisValue,
FromRedisValue, from_redis_value};
#[derive(PartialEq, Debug)]
struct ClientStruct{
data: String,
}
impl FromRedisValue for ClientStruct {
fn from_redis_value(value: &RedisValue) -> RedisResult<Self> {
match value {
RedisValue::BulkString(data) => {
let data = String::from_utf8(data.clone())
.map_err(|err|
RedisError::new(RedisErrorKind::ParseError,
"Cannot parse".to_string()))?;
Ok(ClientStruct {data})
}
_ => Err(RedisError::new(RedisErrorKind::ParseError,
"Cannot parse".to_string()))
}
}
}
let redis_value = RedisValue::BulkString(b"some data".to_vec());
let origin = ClientStruct {data: "some data".to_string()};
assert_eq!(origin, from_redis_value::<ClientStruct>(&redis_value).unwrap());
Required Methods§
fn from_redis_value(value: &RedisValue) -> RedisResult<Self>
Provided Methods§
fn from_redis_u8(_: u8) -> Option<Self>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.