Skip to content

Commit b7171df

Browse files
committed
Add redis_command macro
1 parent 332d20e commit b7171df

File tree

3 files changed

+58
-81
lines changed

3 files changed

+58
-81
lines changed

examples/data_type.rs

Lines changed: 52 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ fn alloc_get(ctx: &Context, args: Vec<String>) -> RedisResult {
5858
const MODULE_NAME: &str = "alloc";
5959
const MODULE_VERSION: c_int = 1;
6060

61+
const COMMANDS: &[Command] = &[
62+
Command { name: "alloc.set", handler: alloc_set, flags: "write" },
63+
Command { name: "alloc.get", handler: alloc_get, flags: "write" },
64+
];
65+
6166
/*
6267
redis_module!(
6368
MODULE_NAME,
@@ -71,6 +76,48 @@ redis_module!(
7176
);
7277
*/
7378

79+
macro_rules! redis_command {
80+
($ctx: expr, $command_name:expr, $command_handler:expr, $command_flags:expr) => {
81+
{
82+
///////////////////////////
83+
//let command_name = "alloc.set";
84+
//let command_flags = "write";
85+
///////////////////////////
86+
87+
let name = CString::new($command_name).unwrap();
88+
let flags = CString::new($command_flags).unwrap();
89+
let (firstkey, lastkey, keystep) = (1, 1, 1);
90+
91+
/////////////////////
92+
extern fn do_command(
93+
ctx: *mut raw::RedisModuleCtx,
94+
argv: *mut *mut raw::RedisModuleString,
95+
argc: c_int,
96+
) -> c_int {
97+
let context = Context::new(ctx);
98+
99+
let args: Vec<String> = unsafe { slice::from_raw_parts(argv, argc as usize) }
100+
.into_iter()
101+
.map(|a| RedisString::from_ptr(*a).expect("UTF8 encoding error in handler args").to_string())
102+
.collect();
103+
104+
let response = $command_handler(&context, args);
105+
context.reply(response) as c_int
106+
}
107+
/////////////////////
108+
109+
if raw::RedisModule_CreateCommand.unwrap()(
110+
$ctx,
111+
name.as_ptr(),
112+
Some(do_command),
113+
flags.as_ptr(),
114+
firstkey, lastkey, keystep,
115+
) == raw::Status::Err as _ { return raw::Status::Err as _; }
116+
}
117+
}
118+
}
119+
120+
74121
use std::os::raw::c_int;
75122
use std::ffi::CString;
76123
use std::slice;
@@ -115,85 +162,11 @@ pub extern "C" fn RedisModule_OnLoad(
115162
eprintln!("*** NOT USING Redis allocator ***");
116163
}
117164

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-
//////////////////////////////
165+
// for command in COMMANDS {
166+
// redis_command!(ctx, command.name, command.handler, command.flags);
167+
// }
168+
redis_command!(ctx, "alloc.set", alloc_set, "write");
169+
redis_command!(ctx, "alloc.get", alloc_get, "write");
197170

198171
raw::Status::Ok as _
199172
}

examples/hello.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn hello_mul(_: &Context, args: Vec<String>) -> RedisResult {
2828
const MODULE_NAME: &str = "hello";
2929
const MODULE_VERSION: u32 = 1;
3030

31+
/*
3132
redis_module!(
3233
MODULE_NAME,
3334
MODULE_VERSION,
@@ -36,6 +37,7 @@ redis_module!(
3637
Command::new("hello.mul", hello_mul, "write"),
3738
]
3839
);
40+
*/
3941

4042
//////////////////////////////////////////////////////
4143

src/command.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use crate::raw;
55
use crate::context::Context;
66
use crate::{RedisString, RedisResult};
77

8-
pub struct Command<F> where F: Fn(&Context, Vec<String>) -> RedisResult {
8+
pub struct Command {
99
pub name: &'static str,
10+
pub handler: fn(&Context, Vec<String>) -> RedisResult,
1011
pub flags: &'static str,
11-
pub handler: F,
1212
}
1313

14+
/*
1415
impl<F> Command<F>
1516
where F: Fn(&Context, Vec<String>) -> RedisResult {
1617
pub fn new(name: &'static str, handler: F, flags: &'static str) -> Command<F> {
@@ -47,3 +48,4 @@ impl<F> Command<F>
4748
Some(do_command::<F> as _)
4849
}
4950
}
51+
*/

0 commit comments

Comments
 (0)