#[derive(RedisValue)]
{
// Attributes available to this derive:
#[RedisValueAttr]
}
Expand description
The macro auto generate a From implementation that can convert the struct into RedisValue.
Example:
#[derive(RedisValue)]
struct RedisValueDeriveInner {
i: i64,
}
#[derive(RedisValue)]
struct RedisValueDerive {
i: i64,
f: f64,
s: String,
u: usize,
v: Vec<i64>,
v2: Vec<RedisValueDeriveInner>,
hash_map: HashMap<String, String>,
hash_set: HashSet<String>,
ordered_map: BTreeMap<String, RedisValueDeriveInner>,
ordered_set: BTreeSet<String>,
}
#[command(
{
flags: [ReadOnly, NoMandatoryKeys],
arity: -1,
key_spec: [
{
notes: "test redis value derive macro",
flags: [ReadOnly, Access],
begin_search: Index({ index : 0 }),
find_keys: Range({ last_key : 0, steps : 0, limit : 0 }),
}
]
}
)]
fn redis_value_derive(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValueDerive {
i: 10,
f: 1.1,
s: "s".to_owned(),
u: 20,
v: vec![1, 2, 3],
v2: vec![
RedisValueDeriveInner { i: 1 },
RedisValueDeriveInner { i: 2 },
],
hash_map: HashMap::from([("key".to_owned(), "val`".to_owned())]),
hash_set: HashSet::from(["key".to_owned()]),
ordered_map: BTreeMap::from([("key".to_owned(), RedisValueDeriveInner { i: 10 })]),
ordered_set: BTreeSet::from(["key".to_owned()]),
}
.into())
}
The From implementation generates a [RedisValue::OrderMap] such that the fields names are the map keys and the values are the result of running Into function on the field value and convert it into a RedisValue.
The code above will generate the following reply (in resp3):
127.0.0.1:6379> redis_value_derive
1# "f" => (double) 1.1
2# "hash_map" => 1# "key" => "val"
3# "hash_set" => 1~ "key"
4# "i" => (integer) 10
5# "ordered_map" => 1# "key" => 1# "i" => (integer) 10
6# "ordered_set" => 1~ "key"
7# "s" => "s"
8# "u" => (integer) 20
9# "v" =>
1) (integer) 1
2) (integer) 2
3) (integer) 3
10# "v2" =>
1) 1# "i" => (integer) 1
2) 1# "i" => (integer) 2
The derive proc macro can also be set on an Enum. In this case, the generated code will check the enum variant (using a match statement) and perform Into on the matched varient. This is usefull in case the command returns more than a single reply type and the reply type need to be decided at runtime.
It is possible to specify a field attribute that will define a specific behavior about the field. Supported attributes:
- flatten - indicate to inlines keys from a field into the parent struct.
Example:
#[derive(RedisValue)]
struct RedisValueDeriveInner {
i2: i64,
}
#[derive(RedisValue)]
struct RedisValueDerive {
i1: i64,
#[RedisValueAttr{flatten: true}]
inner: RedisValueDeriveInner
}
#[command(
{
flags: [ReadOnly, NoMandatoryKeys],
arity: -1,
key_spec: [
{
notes: "test redis value derive macro",
flags: [ReadOnly, Access],
begin_search: Index({ index : 0 }),
find_keys: Range({ last_key : 0, steps : 0, limit : 0 }),
}
]
}
)]
fn redis_value_derive(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValueDerive {
i1: 10,
inner: RedisValueDeriveInner{ i2: 10 },
}
.into())
}
The code above will generate the following reply (in resp3):
127.0.0.1:6379> redis_value_derive
1# "i1" => 10
2# "i2" => 10