Skip to content

Commit 135dd8f

Browse files
committed
Native type works again: Removed mistaken freeing of memory
1 parent b94c281 commit 135dd8f

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

examples/data_type.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ fn alloc_set(ctx: &Context, args: Vec<String>) -> RedisResult {
3737
} else {
3838
ctx.log_debug(format!("key exists; getting value").as_str());
3939

40-
let value: MyType = key.get_value(&MY_REDIS_TYPE)?;
40+
let value: &mut MyType = key.get_value(&MY_REDIS_TYPE)?;
4141
ctx.log_debug(format!("got value: '{:?}'", value).as_str());
4242

43-
//value.data = "B".repeat(size as usize);
44-
//ctx.log_debug(format!("new value: '{:?}'", value).as_str());
43+
value.data = "B".repeat(size as usize);
44+
ctx.log_debug(format!("new value: '{:?}'", value).as_str());
4545
};
4646

4747
Ok(size.into())

src/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ impl Context {
8383
let msg = CString::new(s).unwrap();
8484
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
8585
}
86+
87+
Err(RedisError::MissingValue) => unsafe {
88+
raw::RedisModule_ReplyWithNull.unwrap()(self.ctx).into()
89+
}
8690
}
8791
}
8892

src/key.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,20 @@ impl RedisKeyWritable {
173173
Ok("OK".into())
174174
}
175175

176-
pub fn get_value<T: Default>(&self, redis_type: &RedisType) -> Result<T, RedisError> {
176+
pub fn get_value<T: Default>(&self, redis_type: &RedisType) -> Result<&mut T, RedisError> {
177177
self.verify_type(redis_type)?;
178178

179179
let value = unsafe {
180180
raw::RedisModule_ModuleTypeGetValue.unwrap()(self.key_inner) as *mut T
181181
};
182182

183183
if value.is_null() {
184-
return Ok(T::default());
184+
return Err(RedisError::MissingValue);
185185
}
186186

187-
let value = unsafe { Box::from_raw(value) };
187+
let value = unsafe { &mut *value };
188188

189-
Ok(*value)
189+
Ok(value)
190190
}
191191

192192
pub fn set_value<T: Debug>(&self, redis_type: &RedisType, value: T) -> Result<(), RedisError> {

src/redismodule.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum RedisError {
1111
WrongArity,
1212
Str(&'static str),
1313
String(String),
14+
MissingValue,
1415
}
1516

1617
#[derive(Debug, PartialEq)]

0 commit comments

Comments
 (0)