Skip to content

Commit

Permalink
add StringBuffer(Vec<u8>) support for RedisValue (#161)
Browse files Browse the repository at this point in the history
* add StringBuffer(Vec<u8>) support for RedisValue
  • Loading branch information
gkorland authored Jul 18, 2021
1 parent 23cb18b commit 60d55f8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ impl Context {
raw::RedisModule_ReplyWithString.unwrap()(self.ctx, s.inner).into()
},

Ok(RedisValue::StringBuffer(s)) => unsafe {
raw::RedisModule_ReplyWithStringBuffer.unwrap()(
self.ctx,
s.as_ptr() as *const i8,
s.len() as usize,
)
.into()
},

Ok(RedisValue::Array(array)) => {
unsafe {
// According to the Redis source code this always succeeds,
Expand Down
16 changes: 16 additions & 0 deletions src/redisvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub enum RedisValue {
SimpleString(String),
BulkString(String),
BulkRedisString(RedisString),
StringBuffer(Vec<u8>),
Integer(i64),
Float(f64),
Array(Vec<RedisValue>),
Expand Down Expand Up @@ -49,6 +50,12 @@ impl From<RedisString> for RedisValue {
}
}

impl From<Vec<u8>> for RedisValue {
fn from(s: Vec<u8>) -> Self {
RedisValue::StringBuffer(s)
}
}

impl From<&RedisString> for RedisValue {
fn from(s: &RedisString) -> Self {
s.to_owned().into()
Expand Down Expand Up @@ -120,6 +127,15 @@ mod tests {
);
}

#[test]
fn from_vec() {
let v : Vec<u8> = vec![0,3,5,21,255];
assert_eq!(
RedisValue::from(v),
RedisValue::StringBuffer(vec![0,3,5,21,255])
);
}

#[test]
fn from_option_none() {
assert_eq!(RedisValue::from(None::<()>), RedisValue::Null,);
Expand Down

0 comments on commit 60d55f8

Please sign in to comment.