Skip to content

Commit 4eb32d7

Browse files
committed
Allow the use of NextArg on Iter<&str> as well as Iter<String>
This makes it easier to write tests that use a Vec<&str> instead of manually calling `to_string()` on all items. The disadvantage of this change is that unlike the previous approach, if we already have an owned String, we clone it anyway.
1 parent 010c779 commit 4eb32d7

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/redismodule.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,38 @@ pub type RedisResult = Result<RedisValue, RedisError>;
1414
pub const REDIS_OK: RedisResult = Ok(RedisValue::SimpleStringStatic("OK"));
1515
pub const TYPE_METHOD_VERSION: u64 = raw::REDISMODULE_TYPE_METHOD_VERSION as u64;
1616

17-
pub trait NextArg: Iterator {
17+
pub trait NextArg {
1818
fn next_string(&mut self) -> Result<String, RedisError>;
1919
fn next_i64(&mut self) -> Result<i64, RedisError>;
2020
fn next_u64(&mut self) -> Result<u64, RedisError>;
2121
fn next_f64(&mut self) -> Result<f64, RedisError>;
2222
fn done(&mut self) -> Result<(), RedisError>;
2323
}
2424

25-
impl<T: Iterator<Item = String>> NextArg for T {
25+
impl<S, T> NextArg for T
26+
where
27+
T: Iterator<Item = S>,
28+
S: AsRef<str>,
29+
{
2630
fn next_string(&mut self) -> Result<String, RedisError> {
27-
self.next().map_or(Err(RedisError::WrongArity), Result::Ok)
31+
self.next()
32+
.map_or(Err(RedisError::WrongArity), |v| Ok(v.as_ref().to_string()))
2833
}
2934

3035
fn next_i64(&mut self) -> Result<i64, RedisError> {
3136
self.next()
32-
.map_or(Err(RedisError::WrongArity), |v| parse_integer(&v))
37+
.map_or(Err(RedisError::WrongArity), |v| parse_integer(v.as_ref()))
3338
}
3439

3540
fn next_u64(&mut self) -> Result<u64, RedisError> {
36-
self.next()
37-
.map_or(Err(RedisError::WrongArity), |v| parse_unsigned_integer(&v))
41+
self.next().map_or(Err(RedisError::WrongArity), |v| {
42+
parse_unsigned_integer(v.as_ref())
43+
})
3844
}
3945

4046
fn next_f64(&mut self) -> Result<f64, RedisError> {
4147
self.next()
42-
.map_or(Err(RedisError::WrongArity), |v| parse_float(&v))
48+
.map_or(Err(RedisError::WrongArity), |v| parse_float(v.as_ref()))
4349
}
4450

4551
/// Return an error if there are any more arguments
@@ -62,7 +68,6 @@ pub fn decode_args(
6268
.collect()
6369
}
6470

65-
6671
pub fn parse_unsigned_integer(arg: &str) -> Result<u64, RedisError> {
6772
arg.parse()
6873
.map_err(|_| RedisError::String(format!("Couldn't parse as unsigned integer: {}", arg)))

0 commit comments

Comments
 (0)