Skip to content

Commit 44cf681

Browse files
committed
Refactor macros: works
1 parent b7171df commit 44cf681

File tree

4 files changed

+75
-134
lines changed

4 files changed

+75
-134
lines changed

examples/data_type.rs

Lines changed: 71 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//#[macro_use]
22
extern crate redismodule;
33

4-
use redismodule::{Context, Command, RedisResult, NextArg};
4+
use redismodule::{Context, RedisResult, NextArg};
55
use redismodule::native_types::RedisType;
66
use redismodule::redismodule::RedisValue;
77

@@ -55,35 +55,9 @@ fn alloc_get(ctx: &Context, args: Vec<String>) -> RedisResult {
5555

5656
//////////////////////////////////////////////////////
5757

58-
const MODULE_NAME: &str = "alloc";
59-
const MODULE_VERSION: c_int = 1;
60-
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-
66-
/*
67-
redis_module!(
68-
MODULE_NAME,
69-
MODULE_VERSION,
70-
[
71-
&MY_REDIS_TYPE,
72-
],
73-
[
74-
Command::new("alloc.set", alloc_set, "write"),
75-
]
76-
);
77-
*/
78-
7958
macro_rules! redis_command {
8059
($ctx: expr, $command_name:expr, $command_handler:expr, $command_flags:expr) => {
8160
{
82-
///////////////////////////
83-
//let command_name = "alloc.set";
84-
//let command_flags = "write";
85-
///////////////////////////
86-
8761
let name = CString::new($command_name).unwrap();
8862
let flags = CString::new($command_flags).unwrap();
8963
let (firstkey, lastkey, keystep) = (1, 1, 1);
@@ -118,56 +92,76 @@ macro_rules! redis_command {
11892
}
11993

12094

121-
use std::os::raw::c_int;
122-
use std::ffi::CString;
123-
use std::slice;
124-
125-
use redismodule::raw;
126-
use redismodule::RedisString;
127-
128-
#[no_mangle]
129-
#[allow(non_snake_case)]
130-
pub extern "C" fn RedisModule_OnLoad(
131-
ctx: *mut raw::RedisModuleCtx,
132-
_argv: *mut *mut raw::RedisModuleString,
133-
_argc: c_int,
134-
) -> c_int {
135-
unsafe {
136-
///////////////////////////////////////
137-
let module_name = MODULE_NAME;
138-
let module_version = MODULE_VERSION;
139-
let data_types = vec![&MY_REDIS_TYPE];
140-
141-
///////////////////////////////////////
142-
143-
let module_name = CString::new(module_name).unwrap();
144-
let module_version = module_version as c_int;
145-
146-
if raw::Export_RedisModule_Init(
147-
ctx,
148-
module_name.as_ptr(),
149-
module_version,
150-
raw::REDISMODULE_APIVER_1 as c_int,
151-
) == raw::Status::Err as _ { return raw::Status::Err as _; }
152-
153-
for data_type in data_types {
154-
if data_type.create_data_type(ctx).is_err() {
155-
return raw::Status::Err as _;
95+
macro_rules! redis_module2 {
96+
(
97+
name: $module_name:expr,
98+
version: $module_version:expr,
99+
data_types: [
100+
$($data_type:ident),* $(,)*
101+
],
102+
commands: [
103+
$([
104+
$name:expr,
105+
$command:ident,
106+
$flags:expr
107+
]),* $(,)*
108+
] $(,)*
109+
) => {
110+
use std::os::raw::c_int;
111+
use std::ffi::CString;
112+
use std::slice;
113+
114+
use redismodule::raw;
115+
use redismodule::RedisString;
116+
117+
#[no_mangle]
118+
#[allow(non_snake_case)]
119+
pub extern "C" fn RedisModule_OnLoad(
120+
ctx: *mut raw::RedisModuleCtx,
121+
_argv: *mut *mut raw::RedisModuleString,
122+
_argc: c_int,
123+
) -> c_int {
124+
unsafe {
125+
let module_name = CString::new($module_name).unwrap();
126+
let module_version = $module_version as c_int;
127+
128+
if raw::Export_RedisModule_Init(
129+
ctx,
130+
module_name.as_ptr(),
131+
module_version,
132+
raw::REDISMODULE_APIVER_1 as c_int,
133+
) == raw::Status::Err as _ { return raw::Status::Err as _; }
134+
135+
$(
136+
if (&$data_type).create_data_type(ctx).is_err() {
137+
return raw::Status::Err as _;
138+
}
139+
)*
140+
141+
if true {
142+
redismodule::alloc::use_redis_alloc();
143+
} else {
144+
eprintln!("*** NOT USING Redis allocator ***");
145+
}
146+
147+
$(
148+
redis_command!(ctx, $name, $command, $flags);
149+
)*
150+
151+
raw::Status::Ok as _
156152
}
157153
}
158-
159-
if true {
160-
redismodule::alloc::use_redis_alloc();
161-
} else {
162-
eprintln!("*** NOT USING Redis allocator ***");
163-
}
164-
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");
170-
171-
raw::Status::Ok as _
172154
}
173-
}
155+
}
156+
157+
redis_module2!{
158+
name: "alloc",
159+
version: 1,
160+
data_types: [
161+
MY_REDIS_TYPE,
162+
],
163+
commands: [
164+
["alloc.set", alloc_set, "write"],
165+
["alloc.get", alloc_get, ""],
166+
],
167+
}

examples/hello.rs

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

4-
use redismodule::{Context, Command, RedisResult, RedisError, parse_integer};
4+
use redismodule::{Context, RedisResult, RedisError, parse_integer};
55
use redismodule::native_types::RedisType;
66

77
fn hello_mul(_: &Context, args: Vec<String>) -> RedisResult {
@@ -26,17 +26,17 @@ fn hello_mul(_: &Context, args: Vec<String>) -> RedisResult {
2626
//////////////////////////////////////////////////////
2727

2828
const MODULE_NAME: &str = "hello";
29-
const MODULE_VERSION: u32 = 1;
29+
const MODULE_VERSION: i32 = 1;
3030

3131
/*
32-
redis_module!(
32+
redis_module!{
3333
MODULE_NAME,
3434
MODULE_VERSION,
3535
[] as [RedisType; 0],
3636
[
3737
Command::new("hello.mul", hello_mul, "write"),
3838
]
39-
);
39+
}
4040
*/
4141

4242
//////////////////////////////////////////////////////

src/command.rs

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ pub mod native_types;
2020

2121
#[macro_use]
2222
mod macros;
23-
mod command;
2423
mod context;
2524
mod key;
2625

27-
pub use command::Command;
2826
pub use context::Context;
2927
pub use redismodule::*;
3028

0 commit comments

Comments
 (0)