Skip to content

Commit

Permalink
Add method for converting RedisString to &str (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
slavak authored Sep 29, 2020
1 parent 729e3d7 commit b23a5a5
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/redismodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::os::raw::{c_char, c_void};
use std::slice;
use std::str;
use std::string::FromUtf8Error;
use std::str::Utf8Error;

pub use crate::raw;
pub use crate::rediserror::RedisError;
Expand Down Expand Up @@ -82,7 +83,7 @@ impl RedisString {
RedisString { ctx, inner }
}

pub fn from_ptr<'a>(ptr: *const raw::RedisModuleString) -> Result<&'a str, str::Utf8Error> {
pub fn from_ptr<'a>(ptr: *const raw::RedisModuleString) -> Result<&'a str, Utf8Error> {
let mut len: libc::size_t = 0;
let bytes = unsafe { raw::RedisModule_StringPtrLen.unwrap()(ptr, &mut len) };

Expand All @@ -104,6 +105,10 @@ impl RedisString {
raw::string_ptr_len(self.inner, &mut len);
len == 0
}

pub fn try_as_str(&self) -> Result<&str, Utf8Error> {
Self::from_ptr(self.inner)
}
}

impl Drop for RedisString {
Expand Down

0 comments on commit b23a5a5

Please sign in to comment.