Skip to content

Commit 905e2a5

Browse files
authored
add key_type to RedisKey (#86)
* add key_type to RedisKey * add key_type read key too
1 parent c83a1b0 commit 905e2a5

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/key.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::ptr;
55
use std::str::Utf8Error;
66
use std::time::Duration;
77

8+
use raw::KeyType;
9+
810
use crate::from_byte_string;
911
use crate::native_types::RedisType;
1012
use crate::raw;
@@ -60,6 +62,10 @@ impl RedisKey {
6062
Ok(Some(value))
6163
}
6264

65+
pub fn key_type(&self) -> raw::KeyType {
66+
unsafe { raw::RedisModule_KeyType.unwrap()(self.key_inner) }.into()
67+
}
68+
6369
/// Detects whether the key pointer given to us by Redis is null.
6470
pub fn is_null(&self) -> bool {
6571
let null_key: *mut raw::RedisModuleKey = ptr::null_mut();
@@ -179,12 +185,12 @@ impl RedisKeyWritable {
179185
REDIS_OK
180186
}
181187

182-
pub fn is_empty(&self) -> bool {
183-
use raw::KeyType;
184-
185-
let key_type: KeyType = unsafe { raw::RedisModule_KeyType.unwrap()(self.key_inner) }.into();
188+
pub fn key_type(&self) -> raw::KeyType {
189+
unsafe { raw::RedisModule_KeyType.unwrap()(self.key_inner) }.into()
190+
}
186191

187-
key_type == KeyType::Empty
192+
pub fn is_empty(&self) -> bool {
193+
self.key_type() == KeyType::Empty
188194
}
189195

190196
pub fn get_value<T>(&self, redis_type: &RedisType) -> Result<Option<&mut T>, RedisError> {
@@ -261,8 +267,6 @@ fn to_raw_mode(mode: KeyMode) -> raw::KeyMode {
261267
}
262268

263269
fn verify_type(key_inner: *mut raw::RedisModuleKey, redis_type: &RedisType) -> RedisResult {
264-
use raw::KeyType;
265-
266270
let key_type: KeyType = unsafe { raw::RedisModule_KeyType.unwrap()(key_inner) }.into();
267271

268272
if key_type != KeyType::Empty {

0 commit comments

Comments
 (0)