Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds bytes-accepting versions of key methods #247

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,19 @@ impl RedisKey {
Ok(val)
}

pub fn read_bytes(&self) -> Option<Vec<u8>> {
if self.is_null() {
None
} else {
Some(read_key_bytes(self.key_inner))
}
}

pub fn hash_get(&self, field: &str) -> Result<Option<RedisString>, RedisError> {
self.hash_bytes_get(field.as_bytes())
}

pub fn hash_bytes_get(&self, field: &[u8]) -> Result<Option<RedisString>, RedisError> {
let val = if self.is_null() {
None
} else {
Expand Down Expand Up @@ -160,6 +172,15 @@ impl RedisKeyWritable {
Ok(Some(read_key(self.key_inner)?))
}

pub fn read_bytes(&self) -> Option<Vec<u8>> {
Some(read_key_bytes(self.key_inner))
}

#[allow(clippy::must_use_candidate)]
pub fn hash_bytes_set(&self, field: &[u8], value: RedisString) -> raw::Status {
raw::hash_bytes_set(self.key_inner, field, value.inner)
}

#[allow(clippy::must_use_candidate)]
pub fn hash_set(&self, field: &str, value: RedisString) -> raw::Status {
raw::hash_set(self.key_inner, field, value.inner)
Expand All @@ -171,6 +192,10 @@ impl RedisKeyWritable {
}

pub fn hash_get(&self, field: &str) -> Result<Option<RedisString>, RedisError> {
self.hash_bytes_get(field.as_bytes())
}

pub fn hash_bytes_get(&self, field: &[u8]) -> Result<Option<RedisString>, RedisError> {
Ok(hash_mget_key(self.ctx, self.key_inner, &[field])?
.pop()
.expect("hash_mget_key should return vector of same length as input"))
Expand Down Expand Up @@ -254,7 +279,11 @@ impl RedisKeyWritable {
}

pub fn write(&self, val: &str) -> RedisResult {
let val_str = RedisString::create(self.ctx, val);
self.write_bytes(val.as_bytes())
}

pub fn write_bytes(&self, val: &[u8]) -> RedisResult {
let val_str = RedisString::from_bytes(self.ctx, val);
match raw::string_set(self.key_inner, val_str.inner) {
raw::Status::Ok => REDIS_OK,
raw::Status::Err => Err(RedisError::Str("Error while setting key")),
Expand Down Expand Up @@ -454,6 +483,10 @@ impl Drop for RedisKeyWritable {
}

fn read_key(key: *mut raw::RedisModuleKey) -> Result<String, Utf8Error> {
String::from_utf8(read_key_bytes(key)).map_err(|e| e.utf8_error())
}

fn read_key_bytes(key: *mut raw::RedisModuleKey) -> Vec<u8> {
let mut length: size_t = 0;
from_byte_string(
raw::string_dma(key, &mut length, raw::KeyMode::READ),
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

pub use crate::context::InfoContext;
use std::os::raw::c_char;
use std::str::Utf8Error;
use strum_macros::AsRefStr;
extern crate num_traits;

Expand Down Expand Up @@ -52,14 +51,14 @@ pub enum LogLevel {
Warning,
}

fn from_byte_string(byte_str: *const c_char, length: size_t) -> Result<String, Utf8Error> {
fn from_byte_string(byte_str: *const c_char, length: size_t) -> Vec<u8> {
let mut vec_str: Vec<u8> = Vec::with_capacity(length as usize);
for j in 0..length {
let byte = unsafe { *byte_str.add(j) } as u8;
vec_str.insert(j, byte);
}

String::from_utf8(vec_str).map_err(|e| e.utf8_error())
vec_str
}

pub fn base_info_func(
Expand Down
6 changes: 5 additions & 1 deletion src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,12 @@ where
}
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn hash_set(key: *mut RedisModuleKey, field: &str, value: *mut RedisModuleString) -> Status {
hash_bytes_set(key, field.as_bytes(), value)
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn hash_bytes_set(key: *mut RedisModuleKey, field: &[u8], value: *mut RedisModuleString) -> Status {
let field = CString::new(field).unwrap();

unsafe {
Expand Down
10 changes: 7 additions & 3 deletions src/redismodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ impl RedisString {
Self { ctx, inner }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn create(ctx: *mut raw::RedisModuleCtx, s: &str) -> Self {
let str = CString::new(s).unwrap();
let inner = unsafe { raw::RedisModule_CreateString.unwrap()(ctx, str.as_ptr(), s.len()) };
Self::from_bytes(ctx, s.as_bytes())
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn from_bytes(ctx: *mut raw::RedisModuleCtx, bytes: &[u8]) -> Self {
let str = CString::new(bytes).unwrap();
let inner = unsafe { raw::RedisModule_CreateString.unwrap()(ctx, str.as_ptr(), bytes.len()) };

Self { ctx, inner }
}
Expand Down