Skip to content

Commit

Permalink
Add support to casting RedisResult to CString.
Browse files Browse the repository at this point in the history
  • Loading branch information
nihohit committed Aug 16, 2022
1 parent 02fbee5 commit 36a641a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions redis/src/types.rs
Expand Up @@ -2,6 +2,7 @@ use std::collections::{BTreeMap, BTreeSet};
use std::convert::From;
use std::default::Default;
use std::error;
use std::ffi::{CString, NulError};
use std::fmt;
use std::hash::{BuildHasher, Hash};
use std::io;
Expand Down Expand Up @@ -265,6 +266,18 @@ impl From<Utf8Error> for RedisError {
}
}

impl From<NulError> for RedisError {
fn from(err: NulError) -> RedisError {
RedisError {
repr: ErrorRepr::WithDescriptionAndDetail(
ErrorKind::TypeError,
"Value contains interior nul terminator",
err.to_string(),
),
}
}
}

#[cfg(feature = "tls")]
impl From<native_tls::Error> for RedisError {
fn from(err: native_tls::Error) -> RedisError {
Expand Down Expand Up @@ -1147,6 +1160,17 @@ impl FromRedisValue for bool {
}
}

impl FromRedisValue for CString {
fn from_redis_value(v: &Value) -> RedisResult<CString> {
match *v {
Value::Data(ref bytes) => Ok(CString::new(bytes.clone())?),
Value::Okay => Ok(CString::new("OK")?),
Value::Status(ref val) => Ok(CString::new(val.as_bytes())?),
_ => invalid_type_error!(v, "Response type not CString compatible."),
}
}
}

impl FromRedisValue for String {
fn from_redis_value(v: &Value) -> RedisResult<String> {
match *v {
Expand Down

0 comments on commit 36a641a

Please sign in to comment.