Skip to content

Commit df9b317

Browse files
authored
replace as cast with cast::<> (RedisJSON#615)
* replace as cast with cast::<>
1 parent 93884f5 commit df9b317

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

src/c_api.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn create_rmstring(
4949
str: *mut *mut rawmod::RedisModuleString,
5050
) -> c_int {
5151
if let Ok(s) = CString::new(from_str) {
52-
let p = s.as_bytes_with_nul().as_ptr() as *const c_char;
52+
let p = s.as_bytes_with_nul().as_ptr().cast::<c_char>();
5353
let len = s.as_bytes().len();
5454
unsafe { *str = rawmod::RedisModule_CreateString.unwrap()(ctx, p, len) };
5555
return Status::Ok as c_int;
@@ -72,10 +72,10 @@ pub fn json_api_open_key_internal<M: Manager>(
7272
}
7373

7474
pub fn json_api_get_at<M: Manager>(_: M, json: *const c_void, index: size_t) -> *const c_void {
75-
let json = unsafe { &*(json as *const M::V) };
75+
let json = unsafe { &*(json.cast::<M::V>()) };
7676
match json.get_type() {
7777
SelectValueType::Array => match json.get_index(index) {
78-
Some(v) => v as *const M::V as *const c_void,
78+
Some(v) => (v as *const M::V).cast::<c_void>(),
7979
_ => null(),
8080
},
8181
_ => null(),
@@ -84,7 +84,7 @@ pub fn json_api_get_at<M: Manager>(_: M, json: *const c_void, index: size_t) ->
8484

8585
#[allow(clippy::not_unsafe_ptr_arg_deref)]
8686
pub fn json_api_get_len<M: Manager>(_: M, json: *const c_void, count: *mut libc::size_t) -> c_int {
87-
let json = unsafe { &*(json as *const M::V) };
87+
let json = unsafe { &*(json.cast::<M::V>()) };
8888
let len = match json.get_type() {
8989
SelectValueType::String => Some(json.get_str().len()),
9090
SelectValueType::Array => Some(json.len().unwrap()),
@@ -101,7 +101,7 @@ pub fn json_api_get_len<M: Manager>(_: M, json: *const c_void, count: *mut libc:
101101
}
102102

103103
pub fn json_api_get_type<M: Manager>(_: M, json: *const c_void) -> c_int {
104-
json_api_get_type_internal(unsafe { &*(json as *const M::V) }) as c_int
104+
json_api_get_type_internal(unsafe { &*(json.cast::<M::V>()) }) as c_int
105105
}
106106

107107
pub fn json_api_get_string<M: Manager>(
@@ -110,7 +110,7 @@ pub fn json_api_get_string<M: Manager>(
110110
str: *mut *const c_char,
111111
len: *mut size_t,
112112
) -> c_int {
113-
let json = unsafe { &*(json as *const M::V) };
113+
let json = unsafe { &*(json.cast::<M::V>()) };
114114
match json.get_type() {
115115
SelectValueType::String => {
116116
let s = json.as_str();
@@ -127,14 +127,14 @@ pub fn json_api_get_json<M: Manager>(
127127
ctx: *mut rawmod::RedisModuleCtx,
128128
str: *mut *mut rawmod::RedisModuleString,
129129
) -> c_int {
130-
let json = unsafe { &*(json as *const M::V) };
130+
let json = unsafe { &*(json.cast::<M::V>()) };
131131
let res = KeyValue::<M::V>::serialize_object(json, None, None, None);
132132
create_rmstring(ctx, &res, str)
133133
}
134134

135135
#[allow(clippy::not_unsafe_ptr_arg_deref)]
136136
pub fn json_api_get_int<M: Manager>(_: M, json: *const c_void, val: *mut c_longlong) -> c_int {
137-
let json = unsafe { &*(json as *const M::V) };
137+
let json = unsafe { &*(json.cast::<M::V>()) };
138138
match json.get_type() {
139139
SelectValueType::Long => {
140140
unsafe { *val = json.get_long() };
@@ -146,7 +146,7 @@ pub fn json_api_get_int<M: Manager>(_: M, json: *const c_void, val: *mut c_longl
146146

147147
#[allow(clippy::not_unsafe_ptr_arg_deref)]
148148
pub fn json_api_get_double<M: Manager>(_: M, json: *const c_void, val: *mut c_double) -> c_int {
149-
let json = unsafe { &*(json as *const M::V) };
149+
let json = unsafe { &*(json.cast::<M::V>()) };
150150
match json.get_type() {
151151
SelectValueType::Double => {
152152
unsafe { *val = json.get_double() };
@@ -158,7 +158,7 @@ pub fn json_api_get_double<M: Manager>(_: M, json: *const c_void, val: *mut c_do
158158

159159
#[allow(clippy::not_unsafe_ptr_arg_deref)]
160160
pub fn json_api_get_boolean<M: Manager>(_: M, json: *const c_void, val: *mut c_int) -> c_int {
161-
let json = unsafe { &*(json as *const M::V) };
161+
let json = unsafe { &*(json.cast::<M::V>()) };
162162
match json.get_type() {
163163
SelectValueType::Bool => {
164164
unsafe { *val = json.get_bool() as c_int };
@@ -174,7 +174,7 @@ pub fn json_api_get_boolean<M: Manager>(_: M, json: *const c_void, val: *mut c_i
174174
pub fn set_string(from_str: &str, str: *mut *const c_char, len: *mut size_t) -> c_int {
175175
if !str.is_null() {
176176
unsafe {
177-
*str = from_str.as_ptr() as *const c_char;
177+
*str = from_str.as_ptr().cast::<c_char>();
178178
*len = from_str.len();
179179
}
180180
return Status::Ok as c_int;
@@ -195,38 +195,38 @@ fn json_api_get_type_internal<V: SelectValue>(v: &V) -> JSONType {
195195
}
196196

197197
pub fn json_api_next<M: Manager>(_: M, iter: *mut c_void) -> *const c_void {
198-
let iter = unsafe { &mut *(iter as *mut ResultsIterator<M::V>) };
198+
let iter = unsafe { &mut *(iter.cast::<ResultsIterator<M::V>>()) };
199199
if iter.pos >= iter.results.len() {
200200
null_mut()
201201
} else {
202-
let res = iter.results[iter.pos] as *const M::V as *const c_void;
202+
let res = (iter.results[iter.pos] as *const M::V).cast::<c_void>();
203203
iter.pos += 1;
204204
res
205205
}
206206
}
207207

208208
pub fn json_api_len<M: Manager>(_: M, iter: *const c_void) -> size_t {
209-
let iter = unsafe { &*(iter as *mut ResultsIterator<M::V>) };
209+
let iter = unsafe { &*(iter.cast::<ResultsIterator<M::V>>()) };
210210
iter.results.len() as size_t
211211
}
212212

213213
pub fn json_api_free_iter<M: Manager>(_: M, iter: *mut c_void) {
214214
unsafe {
215-
Box::from_raw(iter as *mut ResultsIterator<M::V>);
215+
Box::from_raw(iter.cast::<ResultsIterator<M::V>>());
216216
}
217217
}
218218

219219
#[allow(clippy::not_unsafe_ptr_arg_deref)]
220220
pub fn json_api_get<M: Manager>(_: M, val: *const c_void, path: *const c_char) -> *const c_void {
221-
let v = unsafe { &*(val as *const M::V) };
221+
let v = unsafe { &*(val.cast::<M::V>()) };
222222
let mut selector = Selector::new();
223223
selector.value(v);
224224
let path = unsafe { CStr::from_ptr(path).to_str().unwrap() };
225225
if selector.str_path(path).is_err() {
226226
return null();
227227
}
228228
match selector.select() {
229-
Ok(s) => Box::into_raw(Box::new(ResultsIterator { results: s, pos: 0 })) as *mut c_void,
229+
Ok(s) => Box::into_raw(Box::new(ResultsIterator { results: s, pos: 0 })).cast::<c_void>(),
230230
Err(_) => null(),
231231
}
232232
}

src/redisjson.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'a> Path<'a> {
8282
if path == "." {
8383
cloned.replace_range(..1, "$");
8484
} else if path.starts_with('.') {
85-
cloned.insert(0, '$')
85+
cloned.insert(0, '$');
8686
} else {
8787
cloned.insert_str(0, "$.");
8888
}
@@ -99,11 +99,9 @@ impl<'a> Path<'a> {
9999
}
100100

101101
pub fn get_path(&'a self) -> &'a str {
102-
if let Some(s) = &self.fixed_path {
103-
s.as_str()
104-
} else {
105-
self.original_path
106-
}
102+
self.fixed_path
103+
.as_ref()
104+
.map_or(self.original_path, |s| s.as_str())
107105
}
108106

109107
pub fn get_original(&self) -> &'a str {
@@ -137,7 +135,7 @@ pub mod type_methods {
137135
};
138136
let v = m.from_str(&json_string, Format::JSON);
139137
match v {
140-
Ok(res) => Box::into_raw(Box::new(res)) as *mut c_void,
138+
Ok(res) => Box::into_raw(Box::new(res)).cast::<libc::c_void>(),
141139
Err(_) => null_mut(),
142140
}
143141
}
@@ -147,7 +145,7 @@ pub mod type_methods {
147145
};
148146
let v = m.from_str(&json_string, Format::JSON);
149147
match v {
150-
Ok(res) => Box::into_raw(Box::new(res)) as *mut c_void,
148+
Ok(res) => Box::into_raw(Box::new(res)).cast::<libc::c_void>(),
151149
Err(_) => null_mut(),
152150
}
153151
}
@@ -196,12 +194,12 @@ pub mod type_methods {
196194
}
197195
match get_manager_type() {
198196
ManagerType::SerdeValue => {
199-
let v = value as *mut RedisJSON<serde_json::Value>;
197+
let v = value.cast::<RedisJSON<serde_json::Value>>();
200198
// Take ownership of the data from Redis (causing it to be dropped when we return)
201199
Box::from_raw(v);
202200
}
203201
ManagerType::IValue => {
204-
let v = value as *mut RedisJSON<ijson::IValue>;
202+
let v = value.cast::<RedisJSON<ijson::IValue>>();
205203
// Take ownership of the data from Redis (causing it to be dropped when we return)
206204
Box::from_raw(v);
207205
}
@@ -213,12 +211,12 @@ pub mod type_methods {
213211
let mut out = serde_json::Serializer::new(Vec::new());
214212
let json = match get_manager_type() {
215213
ManagerType::SerdeValue => {
216-
let v = unsafe { &*(value as *mut RedisJSON<serde_json::Value>) };
214+
let v = unsafe { &*value.cast::<RedisJSON<serde_json::Value>>() };
217215
v.data.serialize(&mut out).unwrap();
218216
String::from_utf8(out.into_inner()).unwrap()
219217
}
220218
ManagerType::IValue => {
221-
let v = unsafe { &*(value as *mut RedisJSON<ijson::IValue>) };
219+
let v = unsafe { &*value.cast::<RedisJSON<ijson::IValue>>() };
222220
v.data.serialize(&mut out).unwrap();
223221
String::from_utf8(out.into_inner()).unwrap()
224222
}

0 commit comments

Comments
 (0)