Skip to content

Commit 87276a3

Browse files
committed
Creating a native type works; added better error handling
1 parent f7bbb48 commit 87276a3

File tree

5 files changed

+28
-31
lines changed

5 files changed

+28
-31
lines changed

examples/data_type.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use redismodule::types::RedisModuleType;
1414
const MODULE_NAME: &str = "alloc";
1515
const MODULE_VERSION: c_int = 1;
1616

17-
static mut MY_TYPE: RedisModuleType = RedisModuleType::new("mytype");
17+
// TODO: Can we use a safe smart pointer instead of an unsafe mutable static variable?
18+
static mut MY_TYPE: RedisModuleType = RedisModuleType::new();
1819

1920
struct AllocSetCommand;
2021

@@ -109,12 +110,11 @@ pub extern "C" fn AllocDelCommand_Redis(
109110
AllocDelCommand::execute(ctx, argv, argc).into()
110111
}
111112

112-
//////////////////////////////////////////////////////
113-
114-
fn module_on_load(ctx: *mut raw::RedisModuleCtx) -> Result<(), ()> {
113+
fn module_on_load(ctx: *mut raw::RedisModuleCtx) -> Result<(), &'static str> {
115114
module_init(ctx, MODULE_NAME, MODULE_VERSION)?;
116115

117-
unsafe { MY_TYPE.create_data_type(ctx) }?;
116+
// FIXME: Make this safe (use a smart pointer?)
117+
unsafe { MY_TYPE.create_data_type(ctx, "mytype123") }?;
118118

119119
AllocSetCommand::create(ctx)?;
120120
AllocDelCommand::create(ctx)?;
@@ -129,7 +129,8 @@ pub extern "C" fn RedisModule_OnLoad(
129129
_argv: *mut *mut raw::RedisModuleString,
130130
_argc: c_int,
131131
) -> c_int {
132-
if let Err(_) = module_on_load(ctx) {
132+
if let Err(msg) = module_on_load(ctx) {
133+
eprintln!("Error loading module: {}", msg);
133134
return raw::Status::Err.into();
134135
}
135136

examples/hello.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub extern "C" fn HelloAddCommand_Redis(
102102

103103
//////////////////////////////////////////////////////
104104

105-
fn module_on_load(ctx: *mut raw::RedisModuleCtx) -> Result<(), ()> {
105+
fn module_on_load(ctx: *mut raw::RedisModuleCtx) -> Result<(), &'static str> {
106106
module_init(ctx, MODULE_NAME, MODULE_VERSION)?;
107107

108108
HelloMulCommand::create(ctx)?;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub trait Command {
139139
}
140140
}
141141

142-
fn create(ctx: *mut raw::RedisModuleCtx) -> Result<(), ()> {
142+
fn create(ctx: *mut raw::RedisModuleCtx) -> Result<(), &'static str> {
143143
raw::create_command(
144144
ctx,
145145
Self::name(),

src/raw.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ impl From<Status> for c_int {
8484
}
8585
}
8686

87-
impl From<Status> for Result<(), ()> {
87+
impl From<Status> for Result<(), &str> {
8888
fn from(s: Status) -> Self {
8989
match s {
9090
Status::Ok => Ok(()),
91-
Status::Err => Err(()),
91+
Status::Err => Err("Generic error"),
9292
}
9393
}
9494
}
@@ -107,7 +107,7 @@ pub fn create_command(
107107
firstkey: i32,
108108
lastkey: i32,
109109
keystep: i32,
110-
) -> Result<(), ()> {
110+
) -> Result<(), &'static str> {
111111
let name = CString::new(name).unwrap();
112112
let strflags = CString::new(strflags).unwrap();
113113

@@ -130,7 +130,7 @@ pub fn module_init(
130130
ctx: *mut RedisModuleCtx,
131131
modulename: &str,
132132
module_version: c_int,
133-
) -> Result<(), ()> {
133+
) -> Result<(), &str> {
134134
let modulename = CString::new(modulename.as_bytes()).unwrap();
135135

136136
let status: Status = unsafe {

src/types.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ use std::os::raw::{c_int, c_void};
44

55
use crate::raw;
66

7-
pub struct RedisModuleType<'a> {
8-
name: &'a str,
7+
pub struct RedisModuleType {
98
raw_type: *mut raw::RedisModuleType,
109
}
1110

1211
// We want to be able to create static instances of this type,
1312
// which means we need to implement Sync.
14-
unsafe impl<'a> Sync for RedisModuleType<'a> {}
13+
unsafe impl Sync for RedisModuleType {}
1514

16-
fn redis_log(
15+
pub fn redis_log(
1716
ctx: *mut raw::RedisModuleCtx,
1817
msg: &str,
1918
) {
@@ -24,21 +23,26 @@ fn redis_log(
2423
}
2524
}
2625

27-
impl<'a> RedisModuleType<'a> {
28-
pub const fn new(name: &'a str) -> Self {
26+
impl RedisModuleType {
27+
pub const fn new() -> Self {
2928
RedisModuleType {
30-
name,
3129
raw_type: ptr::null_mut(),
3230
}
3331
}
3432

3533
pub fn create_data_type(
3634
&mut self,
3735
ctx: *mut raw::RedisModuleCtx,
38-
) -> Result<(), ()> {
39-
let type_name = CString::new(self.name).unwrap();
36+
name: &str,
37+
) -> Result<(), &str> {
4038

41-
redis_log(ctx, "Here 1");
39+
if name.len() != 9 {
40+
let msg = "Redis requires the length of native type names to be exactly 9 characters";
41+
redis_log(ctx, format!("{}, name is: '{}'", msg, name).as_str());
42+
return Err(msg);
43+
}
44+
45+
let type_name = CString::new(name).unwrap();
4246

4347
let mut type_methods = raw::RedisModuleTypeMethods {
4448
version: raw::REDISMODULE_TYPE_METHOD_VERSION as u64,
@@ -53,8 +57,6 @@ impl<'a> RedisModuleType<'a> {
5357
digest: None,
5458
};
5559

56-
redis_log(ctx, "Here 2");
57-
5860
let redis_type = unsafe {
5961
raw::RedisModule_CreateDataType.unwrap()(
6062
ctx,
@@ -64,19 +66,13 @@ impl<'a> RedisModuleType<'a> {
6466
)
6567
};
6668

67-
redis_log(ctx, "Here 3");
68-
6969
if redis_type.is_null() {
7070
redis_log(ctx, "Error: created data type is null");
71-
return Err(());
71+
return Err("Error: created data type is null");
7272
}
7373

74-
redis_log(ctx, "Here 4");
75-
7674
self.raw_type = redis_type;
7775

78-
redis_log(ctx, "Here 5");
79-
8076
Ok(())
8177
}
8278
}

0 commit comments

Comments
 (0)