Skip to content

Commit 41ffbd2

Browse files
committed
Value::Nil to default
1 parent 886efa5 commit 41ffbd2

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

redis/src/types.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -1563,8 +1563,16 @@ impl_to_redis_args_for_set!(
15631563
where (T: ToRedisArgs + Hash + Eq, S: BuildHasher + Default)
15641564
);
15651565

1566+
/// @note: Redis cannot store empty sets so the application has to
1567+
/// check whether the set is empty and if so, not attempt to use that
1568+
/// result
15661569
macro_rules! impl_to_redis_args_for_map {
1567-
(for <$($TypeParam:ident),+> $MapType:ty, where ($($WhereClause:tt)+) ) => {
1570+
(
1571+
$(#[$meta:meta])*
1572+
for <$($TypeParam:ident),+> $MapType:ty,
1573+
where ($($WhereClause:tt)+)
1574+
) => {
1575+
$(#[$meta])*
15681576
impl< $($TypeParam),+ > ToRedisArgs for $MapType
15691577
where
15701578
$($WhereClause)+
@@ -1574,29 +1582,26 @@ macro_rules! impl_to_redis_args_for_map {
15741582
W: ?Sized + RedisWrite,
15751583
{
15761584
for (key, value) in self {
1585+
// Ensure key and value produce a single argument each
15771586
assert!(key.num_of_args() <= 1 && value.num_of_args() <= 1);
15781587
key.write_redis_args(out);
15791588
value.write_redis_args(out);
15801589
}
15811590
}
15821591

15831592
fn num_of_args(&self) -> usize {
1584-
self.len() * 2
1593+
self.len()
15851594
}
15861595
}
15871596
};
15881597
}
1589-
15901598
impl_to_redis_args_for_map!(
15911599
for <K, V> std::collections::HashMap<K, V>,
15921600
where (K: ToRedisArgs + Hash + Eq + Ord, V: ToRedisArgs)
15931601
);
15941602

1595-
/// this flattens BTreeMap into something that goes well with HMSET
1596-
/// @note: Redis cannot store empty sets so the application has to
1597-
/// check whether the set is empty and if so, not attempt to use that
1598-
/// result
15991603
impl_to_redis_args_for_map!(
1604+
/// this flattens BTreeMap into something that goes well with HMSET
16001605
for <K, V> std::collections::BTreeMap<K, V>,
16011606
where (K: ToRedisArgs + Hash + Eq + Ord, V: ToRedisArgs)
16021607
);
@@ -2079,18 +2084,30 @@ macro_rules! impl_from_redis_value_for_map {
20792084
{
20802085
fn from_redis_value(v: &Value) -> RedisResult<$MapType> {
20812086
let v = get_inner_value(v);
2082-
v.as_map_iter()
2083-
.ok_or_else(|| invalid_type_error_inner!(v, $err_msg))?
2084-
.map(|(k, v)| Ok((from_redis_value(k)?, from_redis_value(v)?)))
2085-
.collect()
2087+
match *v {
2088+
Value::Nil => Ok(Default::default()),
2089+
_ => v
2090+
.as_map_iter()
2091+
.ok_or_else(|| invalid_type_error_inner!(v, $err_msg))?
2092+
.map(|(k, v)| {
2093+
Ok((from_redis_value(k)?, from_redis_value(v)?))
2094+
})
2095+
.collect(),
2096+
}
20862097
}
20872098

20882099
fn from_owned_redis_value(v: Value) -> RedisResult<$MapType> {
20892100
let v = get_owned_inner_value(v);
2090-
v.into_map_iter()
2091-
.map_err(|v| invalid_type_error_inner!(v, $err_msg))?
2092-
.map(|(k, v)| Ok((from_owned_redis_value(k)?, from_owned_redis_value(v)?)))
2093-
.collect()
2101+
match v {
2102+
Value::Nil => Ok(Default::default()),
2103+
_ => v
2104+
.into_map_iter()
2105+
.map_err(|v| invalid_type_error_inner!(v, $err_msg))?
2106+
.map(|(k, v)| {
2107+
Ok((from_owned_redis_value(k)?, from_owned_redis_value(v)?))
2108+
})
2109+
.collect(),
2110+
}
20942111
}
20952112
}
20962113
};

0 commit comments

Comments
 (0)