Skip to content

Commit 332d20e

Browse files
committed
Refactor module macro step 1: Expand manually
1 parent 5ca3f96 commit 332d20e

File tree

1 file changed

+131
-2
lines changed

1 file changed

+131
-2
lines changed

examples/data_type.rs

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[macro_use]
1+
//#[macro_use]
22
extern crate redismodule;
33

44
use redismodule::{Context, Command, RedisResult, NextArg};
@@ -51,14 +51,14 @@ fn alloc_get(ctx: &Context, args: Vec<String>) -> RedisResult {
5151
Ok("some value".into())
5252
}
5353
}
54-
5554
}
5655

5756
//////////////////////////////////////////////////////
5857

5958
const MODULE_NAME: &str = "alloc";
6059
const MODULE_VERSION: c_int = 1;
6160

61+
/*
6262
redis_module!(
6363
MODULE_NAME,
6464
MODULE_VERSION,
@@ -69,3 +69,132 @@ redis_module!(
6969
Command::new("alloc.set", alloc_set, "write"),
7070
]
7171
);
72+
*/
73+
74+
use std::os::raw::c_int;
75+
use std::ffi::CString;
76+
use std::slice;
77+
78+
use redismodule::raw;
79+
use redismodule::RedisString;
80+
81+
#[no_mangle]
82+
#[allow(non_snake_case)]
83+
pub extern "C" fn RedisModule_OnLoad(
84+
ctx: *mut raw::RedisModuleCtx,
85+
_argv: *mut *mut raw::RedisModuleString,
86+
_argc: c_int,
87+
) -> c_int {
88+
unsafe {
89+
///////////////////////////////////////
90+
let module_name = MODULE_NAME;
91+
let module_version = MODULE_VERSION;
92+
let data_types = vec![&MY_REDIS_TYPE];
93+
94+
///////////////////////////////////////
95+
96+
let module_name = CString::new(module_name).unwrap();
97+
let module_version = module_version as c_int;
98+
99+
if raw::Export_RedisModule_Init(
100+
ctx,
101+
module_name.as_ptr(),
102+
module_version,
103+
raw::REDISMODULE_APIVER_1 as c_int,
104+
) == raw::Status::Err as _ { return raw::Status::Err as _; }
105+
106+
for data_type in data_types {
107+
if data_type.create_data_type(ctx).is_err() {
108+
return raw::Status::Err as _;
109+
}
110+
}
111+
112+
if true {
113+
redismodule::alloc::use_redis_alloc();
114+
} else {
115+
eprintln!("*** NOT USING Redis allocator ***");
116+
}
117+
118+
//////////////////////////////
119+
// Command 1
120+
{
121+
///////////////////////////
122+
let command_name = "alloc.set";
123+
let command_flags = "write";
124+
///////////////////////////
125+
126+
let name = CString::new(command_name).unwrap();
127+
let flags = CString::new(command_flags).unwrap();
128+
let (firstkey, lastkey, keystep) = (1, 1, 1);
129+
130+
/////////////////////
131+
extern fn do_command(
132+
ctx: *mut raw::RedisModuleCtx,
133+
argv: *mut *mut raw::RedisModuleString,
134+
argc: c_int,
135+
) -> c_int {
136+
let context = Context::new(ctx);
137+
138+
let args: Vec<String> = unsafe { slice::from_raw_parts(argv, argc as usize) }
139+
.into_iter()
140+
.map(|a| RedisString::from_ptr(*a).expect("UTF8 encoding error in handler args").to_string())
141+
.collect();
142+
143+
let response = alloc_set(&context, args);
144+
context.reply(response) as c_int
145+
}
146+
/////////////////////
147+
148+
if raw::RedisModule_CreateCommand.unwrap()(
149+
ctx,
150+
name.as_ptr(),
151+
Some(do_command),
152+
flags.as_ptr(),
153+
firstkey, lastkey, keystep,
154+
) == raw::Status::Err as _ { return raw::Status::Err as _; }
155+
}
156+
//////////////////////////////
157+
158+
//////////////////////////////
159+
// Command 2
160+
{
161+
///////////////////////////
162+
let command_name = "alloc.get";
163+
let command_flags = "write";
164+
///////////////////////////
165+
166+
let name = CString::new(command_name).unwrap();
167+
let flags = CString::new(command_flags).unwrap();
168+
let (firstkey, lastkey, keystep) = (1, 1, 1);
169+
170+
/////////////////////
171+
extern fn do_command(
172+
ctx: *mut raw::RedisModuleCtx,
173+
argv: *mut *mut raw::RedisModuleString,
174+
argc: c_int,
175+
) -> c_int {
176+
let context = Context::new(ctx);
177+
178+
let args: Vec<String> = unsafe { slice::from_raw_parts(argv, argc as usize) }
179+
.into_iter()
180+
.map(|a| RedisString::from_ptr(*a).expect("UTF8 encoding error in handler args").to_string())
181+
.collect();
182+
183+
let response = alloc_get(&context, args);
184+
context.reply(response) as c_int
185+
}
186+
/////////////////////
187+
188+
if raw::RedisModule_CreateCommand.unwrap()(
189+
ctx,
190+
name.as_ptr(),
191+
Some(do_command),
192+
flags.as_ptr(),
193+
firstkey, lastkey, keystep,
194+
) == raw::Status::Err as _ { return raw::Status::Err as _; }
195+
}
196+
//////////////////////////////
197+
198+
raw::Status::Ok as _
199+
}
200+
}

0 commit comments

Comments
 (0)