Skip to content

Commit b94c281

Browse files
committed
Clean up native types; still crashes
1 parent cf6b460 commit b94c281

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

examples/data_type.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Default for MyType {
1515
}
1616
}
1717

18-
static MY_REDIS_TYPE: RedisType = RedisType::new();
18+
static MY_REDIS_TYPE: RedisType = RedisType::new("mytype123");
1919

2020
fn alloc_set(ctx: &Context, args: Vec<String>) -> RedisResult {
2121
let mut args = args.into_iter().skip(1);
@@ -79,6 +79,13 @@ fn alloc_get(ctx: &Context, args: Vec<String>) -> RedisResult {
7979
const MODULE_NAME: &str = "alloc";
8080
const MODULE_VERSION: c_int = 1;
8181

82-
redis_module!(MODULE_NAME, MODULE_VERSION, [
83-
Command::new("alloc.set", alloc_set, "write"),
84-
]);
82+
redis_module!(
83+
MODULE_NAME,
84+
MODULE_VERSION,
85+
[
86+
&MY_REDIS_TYPE,
87+
],
88+
[
89+
Command::new("alloc.set", alloc_set, "write"),
90+
]
91+
);

examples/hello.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
extern crate redismodule;
33

44
use redismodule::{Context, Command, RedisResult, RedisError, parse_integer};
5+
use redismodule::native_types::RedisType;
56

67
fn hello_mul(_: &Context, args: Vec<String>) -> RedisResult {
78
if args.len() < 2 {
@@ -27,9 +28,14 @@ fn hello_mul(_: &Context, args: Vec<String>) -> RedisResult {
2728
const MODULE_NAME: &str = "hello";
2829
const MODULE_VERSION: u32 = 1;
2930

30-
redis_module!(MODULE_NAME, MODULE_VERSION, [
31-
Command::new("hello.mul", hello_mul, "write"),
32-
]);
31+
redis_module!(
32+
MODULE_NAME,
33+
MODULE_VERSION,
34+
[] as [RedisType; 0],
35+
[
36+
Command::new("hello.mul", hello_mul, "write"),
37+
]
38+
);
3339

3440
//////////////////////////////////////////////////////
3541

src/macros.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ macro_rules! log_debug {
2323

2424
#[macro_export]
2525
macro_rules! redis_module (
26-
($module_name:expr, $module_version:expr, $commands:expr) => (
26+
($module_name:expr, $module_version:expr, $data_types:expr, $commands:expr) => (
2727
use std::os::raw::c_int;
2828
use std::ffi::CString;
2929

@@ -47,6 +47,12 @@ macro_rules! redis_module (
4747
raw::REDISMODULE_APIVER_1 as c_int,
4848
) == raw::Status::Err as _ { return raw::Status::Err as _; }
4949

50+
for data_type in &$data_types {
51+
if data_type.create_data_type(ctx).is_err() {
52+
return raw::Status::Err as _;
53+
}
54+
}
55+
5056
if true {
5157
redismodule::alloc::use_redis_alloc();
5258
} else {

src/native_types.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::os::raw::{c_int, c_void};
66
use crate::raw;
77

88
pub struct RedisType {
9+
name: &'static str,
910
pub raw_type: RefCell<*mut raw::RedisModuleType>,
1011
}
1112

@@ -14,24 +15,24 @@ pub struct RedisType {
1415
unsafe impl Sync for RedisType {}
1516

1617
impl RedisType {
17-
pub const fn new() -> Self {
18+
pub const fn new(name: &'static str) -> Self {
1819
RedisType {
20+
name,
1921
raw_type: RefCell::new(ptr::null_mut()),
2022
}
2123
}
2224

2325
pub fn create_data_type(
2426
&self,
2527
ctx: *mut raw::RedisModuleCtx,
26-
name: &str,
2728
) -> Result<(), &str> {
28-
if name.len() != 9 {
29+
if self.name.len() != 9 {
2930
let msg = "Redis requires the length of native type names to be exactly 9 characters";
30-
redis_log(ctx, format!("{}, name is: '{}'", msg, name).as_str());
31+
redis_log(ctx, format!("{}, name is: '{}'", msg, self.name).as_str());
3132
return Err(msg);
3233
}
3334

34-
let type_name = CString::new(name).unwrap();
35+
let type_name = CString::new(self.name).unwrap();
3536

3637
let mut type_methods = raw::RedisModuleTypeMethods {
3738
version: raw::REDISMODULE_TYPE_METHOD_VERSION as u64,
@@ -62,7 +63,7 @@ impl RedisType {
6263

6364
*self.raw_type.borrow_mut() = redis_type;
6465

65-
redis_log(ctx, format!("Created new data type '{}'", name).as_str());
66+
redis_log(ctx, format!("Created new data type '{}'", self.name).as_str());
6667

6768
Ok(())
6869
}

0 commit comments

Comments
 (0)