11//#[macro_use]
22extern crate redismodule;
33
4- use redismodule:: { Context , Command , RedisResult , NextArg } ;
4+ use redismodule:: { Context , RedisResult , NextArg } ;
55use redismodule:: native_types:: RedisType ;
66use 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-
7958macro_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+ }
0 commit comments