Skip to content

Commit 56d6457

Browse files
committed
Fix various lints and set debug_asserts
1 parent 53d137e commit 56d6457

38 files changed

+516
-402
lines changed

include/yyjson/yyjson.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -5265,7 +5265,7 @@ static_inline bool read_string(u8 **ptr,
52655265

52665266
u8 *cur = *ptr;
52675267
u8 **end = ptr;
5268-
u8 *src = ++cur, *dst, *pos;
5268+
u8 *src = ++cur, *dst;
52695269
u16 hi, lo;
52705270
u32 uni, tmp;
52715271

@@ -5332,7 +5332,6 @@ static_inline bool read_string(u8 **ptr,
53325332
consecutively. We process the byte sequences of the same length in each
53335333
loop, which is more friendly to branch prediction.
53345334
*/
5335-
pos = src;
53365335
#if YYJSON_DISABLE_UTF8_VALIDATION
53375336
while (true) repeat8({
53385337
if (likely((*src & 0xF0) == 0xE0)) src += 3;
@@ -5550,7 +5549,6 @@ static_inline bool read_string(u8 **ptr,
55505549

55515550
copy_utf8:
55525551
if (*src & 0x80) { /* non-ASCII character */
5553-
pos = src;
55545552
uni = byte_load_4(src);
55555553
#if YYJSON_DISABLE_UTF8_VALIDATION
55565554
while (true) repeat4({

src/alloc.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

3-
use std::alloc::{GlobalAlloc, Layout};
4-
use std::ffi::c_void;
3+
use core::alloc::{GlobalAlloc, Layout};
4+
use core::ffi::c_void;
55

66
struct PyMemAllocator {}
77

@@ -13,26 +13,26 @@ unsafe impl Sync for PyMemAllocator {}
1313
unsafe impl GlobalAlloc for PyMemAllocator {
1414
#[inline]
1515
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
16-
unsafe { pyo3_ffi::PyMem_Malloc(layout.size()) as *mut u8 }
16+
unsafe { pyo3_ffi::PyMem_Malloc(layout.size()).cast::<u8>() }
1717
}
1818

1919
#[inline]
2020
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
21-
unsafe { pyo3_ffi::PyMem_Free(ptr as *mut c_void) }
21+
unsafe { pyo3_ffi::PyMem_Free(ptr.cast::<c_void>()) }
2222
}
2323

2424
#[inline]
2525
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
2626
unsafe {
2727
let len = layout.size();
28-
let ptr = pyo3_ffi::PyMem_Malloc(len) as *mut u8;
28+
let ptr = pyo3_ffi::PyMem_Malloc(len).cast::<u8>();
2929
core::ptr::write_bytes(ptr, 0, len);
3030
ptr
3131
}
3232
}
3333

3434
#[inline]
3535
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
36-
unsafe { pyo3_ffi::PyMem_Realloc(ptr as *mut c_void, new_size) as *mut u8 }
36+
unsafe { pyo3_ffi::PyMem_Realloc(ptr.cast::<c_void>(), new_size).cast::<u8>() }
3737
}
3838
}

src/deserialize/backend/yyjson.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

3-
use crate::deserialize::pyobject::*;
3+
use crate::deserialize::pyobject::{
4+
get_unicode_key, parse_f64, parse_false, parse_i64, parse_none, parse_true, parse_u64,
5+
};
46
use crate::deserialize::DeserializeError;
5-
use crate::ffi::yyjson::*;
7+
use crate::ffi::yyjson::{
8+
yyjson_doc, yyjson_doc_free, yyjson_read_err, yyjson_read_opts, yyjson_val, YYJSON_READ_SUCCESS,
9+
};
610
use crate::str::unicode_from_str;
711
use crate::typeref::{yyjson_init, YYJSON_ALLOC, YYJSON_BUFFER_SIZE};
12+
use crate::util::usize_to_isize;
13+
814
use core::ffi::c_char;
915
use core::ptr::{null, null_mut, NonNull};
1016
use std::borrow::Cow;
@@ -49,12 +55,14 @@ fn unsafe_yyjson_is_ctn(val: *mut yyjson_val) -> bool {
4955
unsafe { (*val).tag as u8 & 0b00000110 == 0b00000110 }
5056
}
5157

58+
#[allow(clippy::cast_ptr_alignment)]
5259
fn unsafe_yyjson_get_next_container(val: *mut yyjson_val) -> *mut yyjson_val {
53-
unsafe { ((val as *mut u8).add((*val).uni.ofs)) as *mut yyjson_val }
60+
unsafe { (val.cast::<u8>().add((*val).uni.ofs)).cast::<yyjson_val>() }
5461
}
5562

63+
#[allow(clippy::cast_ptr_alignment)]
5664
fn unsafe_yyjson_get_next_non_container(val: *mut yyjson_val) -> *mut yyjson_val {
57-
unsafe { ((val as *mut u8).add(YYJSON_VAL_SIZE)) as *mut yyjson_val }
65+
unsafe { (val.cast::<u8>().add(YYJSON_VAL_SIZE)).cast::<yyjson_val>() }
5866
}
5967

6068
pub(crate) fn deserialize(
@@ -91,16 +99,16 @@ pub(crate) fn deserialize(
9199
unsafe { yyjson_doc_free(doc) };
92100
Ok(pyval)
93101
} else if is_yyjson_tag!(val, TAG_ARRAY) {
94-
let pyval = nonnull!(ffi!(PyList_New(unsafe_yyjson_get_len(val) as isize)));
102+
let pyval = nonnull!(ffi!(PyList_New(usize_to_isize(unsafe_yyjson_get_len(val)))));
95103
if unsafe_yyjson_get_len(val) > 0 {
96104
populate_yy_array(pyval.as_ptr(), val);
97105
}
98106
unsafe { yyjson_doc_free(doc) };
99107
Ok(pyval)
100108
} else {
101-
let pyval = nonnull!(ffi!(_PyDict_NewPresized(
102-
unsafe_yyjson_get_len(val) as isize
103-
)));
109+
let pyval = nonnull!(ffi!(_PyDict_NewPresized(usize_to_isize(
110+
unsafe_yyjson_get_len(val)
111+
))));
104112
if unsafe_yyjson_get_len(val) > 0 {
105113
populate_yy_object(pyval.as_ptr(), val);
106114
}
@@ -157,7 +165,7 @@ impl ElementType {
157165
#[inline(always)]
158166
fn parse_yy_string(elem: *mut yyjson_val) -> NonNull<pyo3_ffi::PyObject> {
159167
nonnull!(unicode_from_str(str_from_slice!(
160-
(*elem).uni.str_ as *const u8,
168+
(*elem).uni.str_.cast::<u8>(),
161169
unsafe_yyjson_get_len(elem)
162170
)))
163171
}
@@ -192,20 +200,22 @@ fn populate_yy_array(list: *mut pyo3_ffi::PyObject, elem: *mut yyjson_val) {
192200
let len = unsafe_yyjson_get_len(elem);
193201
assume!(len >= 1);
194202
let mut next = unsafe_yyjson_get_first(elem);
195-
let mut dptr = (*(list as *mut pyo3_ffi::PyListObject)).ob_item;
203+
let mut dptr = (*list.cast::<pyo3_ffi::PyListObject>()).ob_item;
196204

197205
for _ in 0..len {
198206
let val = next;
199207
if unlikely!(unsafe_yyjson_is_ctn(val)) {
200208
next = unsafe_yyjson_get_next_container(val);
201209
if is_yyjson_tag!(val, TAG_ARRAY) {
202-
let pyval = ffi!(PyList_New(unsafe_yyjson_get_len(val) as isize));
210+
let pyval = ffi!(PyList_New(usize_to_isize(unsafe_yyjson_get_len(val))));
203211
append_to_list!(dptr, pyval);
204212
if unsafe_yyjson_get_len(val) > 0 {
205213
populate_yy_array(pyval, val);
206214
}
207215
} else {
208-
let pyval = ffi!(_PyDict_NewPresized(unsafe_yyjson_get_len(val) as isize));
216+
let pyval = ffi!(_PyDict_NewPresized(usize_to_isize(unsafe_yyjson_get_len(
217+
val
218+
))));
209219
append_to_list!(dptr, pyval);
210220
if unsafe_yyjson_get_len(val) > 0 {
211221
populate_yy_object(pyval, val);
@@ -241,7 +251,7 @@ fn populate_yy_object(dict: *mut pyo3_ffi::PyObject, elem: *mut yyjson_val) {
241251
let val = next_val;
242252
let pykey = {
243253
let key_str = str_from_slice!(
244-
(*next_key).uni.str_ as *const u8,
254+
(*next_key).uni.str_.cast::<u8>(),
245255
unsafe_yyjson_get_len(next_key)
246256
);
247257
get_unicode_key(key_str)
@@ -250,13 +260,15 @@ fn populate_yy_object(dict: *mut pyo3_ffi::PyObject, elem: *mut yyjson_val) {
250260
next_key = unsafe_yyjson_get_next_container(val);
251261
next_val = next_key.add(1);
252262
if is_yyjson_tag!(val, TAG_ARRAY) {
253-
let pyval = ffi!(PyList_New(unsafe_yyjson_get_len(val) as isize));
263+
let pyval = ffi!(PyList_New(usize_to_isize(unsafe_yyjson_get_len(val))));
254264
pydict_setitem!(dict, pykey, pyval);
255265
if unsafe_yyjson_get_len(val) > 0 {
256266
populate_yy_array(pyval, val);
257267
}
258268
} else {
259-
let pyval = ffi!(_PyDict_NewPresized(unsafe_yyjson_get_len(val) as isize));
269+
let pyval = ffi!(_PyDict_NewPresized(usize_to_isize(unsafe_yyjson_get_len(
270+
val
271+
))));
260272
pydict_setitem!(dict, pykey, pyval);
261273
if unsafe_yyjson_get_len(val) > 0 {
262274
populate_yy_object(pyval, val);

src/deserialize/cache.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ unsafe impl Sync for CachedKey {}
1515
impl CachedKey {
1616
pub fn new(ptr: *mut pyo3_ffi::PyObject) -> CachedKey {
1717
CachedKey {
18-
ptr: ptr as *mut c_void,
18+
ptr: ptr.cast::<c_void>(),
1919
}
2020
}
2121
pub fn get(&mut self) -> *mut pyo3_ffi::PyObject {
22-
let ptr = self.ptr as *mut pyo3_ffi::PyObject;
22+
let ptr = self.ptr.cast::<pyo3_ffi::PyObject>();
2323
debug_assert!(ffi!(Py_REFCNT(ptr)) >= 1);
2424
ffi!(Py_INCREF(ptr));
2525
ptr
@@ -28,17 +28,11 @@ impl CachedKey {
2828

2929
impl Drop for CachedKey {
3030
fn drop(&mut self) {
31-
ffi!(Py_DECREF(self.ptr as *mut pyo3_ffi::PyObject));
31+
ffi!(Py_DECREF(self.ptr.cast::<pyo3_ffi::PyObject>()));
3232
}
3333
}
3434

3535
pub type KeyMap =
3636
AssociativeCache<u64, CachedKey, Capacity2048, HashDirectMapped, RoundRobinReplacement>;
3737

3838
pub static mut KEY_MAP: OnceCell<KeyMap> = OnceCell::new();
39-
40-
#[inline(always)]
41-
pub fn cache_hash(key: &[u8]) -> u64 {
42-
assume!(key.len() <= 64);
43-
xxhash_rust::xxh3::xxh3_64(key)
44-
}

src/deserialize/deserializer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn deserialize(
2121
}
2222
}
2323

24-
let buffer_str = unsafe { std::str::from_utf8_unchecked(buffer) };
24+
let buffer_str = unsafe { core::str::from_utf8_unchecked(buffer) };
2525

2626
crate::deserialize::backend::deserialize(buffer_str)
2727
}

src/deserialize/pyobject.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

3-
use crate::deserialize::cache::*;
3+
use crate::deserialize::cache::{CachedKey, KEY_MAP};
44
use crate::str::{hash_str, unicode_from_str};
55
use crate::typeref::{FALSE, NONE, TRUE};
66
use core::ptr::NonNull;
@@ -12,7 +12,8 @@ pub fn get_unicode_key(key_str: &str) -> *mut pyo3_ffi::PyObject {
1212
hash_str(pyob);
1313
pyob
1414
} else {
15-
let hash = cache_hash(key_str.as_bytes());
15+
assume!(key_str.len() <= 64);
16+
let hash = xxhash_rust::xxh3::xxh3_64(key_str.as_bytes());
1617
unsafe {
1718
let entry = KEY_MAP
1819
.get_mut()

src/deserialize/utf8.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

33
use crate::deserialize::DeserializeError;
4-
use crate::ffi::*;
4+
use crate::ffi::{PyBytes_AS_STRING, PyBytes_GET_SIZE, PyMemoryView_GET_BUFFER};
55
use crate::str::unicode_to_str;
66
use crate::typeref::{BYTEARRAY_TYPE, BYTES_TYPE, MEMORYVIEW_TYPE, STR_TYPE};
7+
use crate::util::isize_to_usize;
78
use crate::util::INVALID_STR;
89
use core::ffi::c_char;
910
use std::borrow::Cow;
@@ -40,8 +41,8 @@ pub fn read_input_to_buf(
4041
if is_type!(obj_type_ptr, BYTES_TYPE) {
4142
buffer = unsafe {
4243
core::slice::from_raw_parts(
43-
PyBytes_AS_STRING(ptr) as *const u8,
44-
PyBytes_GET_SIZE(ptr) as usize,
44+
PyBytes_AS_STRING(ptr).cast::<u8>(),
45+
isize_to_usize(PyBytes_GET_SIZE(ptr)),
4546
)
4647
};
4748
if !is_valid_utf8(buffer) {
@@ -62,7 +63,7 @@ pub fn read_input_to_buf(
6263
)));
6364
}
6465
buffer = unsafe {
65-
core::slice::from_raw_parts((*membuf).buf as *const u8, (*membuf).len as usize)
66+
core::slice::from_raw_parts((*membuf).buf as *const u8, isize_to_usize((*membuf).len))
6667
};
6768
if !is_valid_utf8(buffer) {
6869
return Err(DeserializeError::invalid(Cow::Borrowed(INVALID_STR)));
@@ -71,7 +72,7 @@ pub fn read_input_to_buf(
7172
buffer = unsafe {
7273
core::slice::from_raw_parts(
7374
ffi!(PyByteArray_AsString(ptr)) as *const u8,
74-
ffi!(PyByteArray_Size(ptr)) as usize,
75+
isize_to_usize(ffi!(PyByteArray_Size(ptr))),
7576
)
7677
};
7778
if !is_valid_utf8(buffer) {

src/ffi/buffer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

33
use core::ffi::c_int;
4-
use pyo3_ffi::*;
4+
use pyo3_ffi::{PyObject, PyVarObject, Py_buffer, Py_hash_t, Py_ssize_t};
55

66
#[repr(C)]
77
pub struct _PyManagedBufferObject {

src/ffi/fragment.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ use core::ffi::c_char;
66
use std::sync::atomic::{AtomicIsize, AtomicU32, AtomicU64};
77

88
use core::ptr::null_mut;
9-
use pyo3_ffi::*;
9+
use pyo3_ffi::{
10+
PyErr_SetObject, PyExc_TypeError, PyObject, PyTuple_GET_ITEM, PyTypeObject, PyType_Ready,
11+
PyType_Type, PyUnicode_FromStringAndSize, PyVarObject, Py_DECREF, Py_INCREF, Py_SIZE,
12+
Py_TPFLAGS_DEFAULT,
13+
};
1014

1115
// https://docs.python.org/3/c-api/typeobj.html#typedef-examples
1216

@@ -21,7 +25,7 @@ pub struct Fragment {
2125
#[cfg(Py_GIL_DISABLED)]
2226
pub _padding: u16,
2327
#[cfg(Py_GIL_DISABLED)]
24-
pub ob_mutex: PyMutex,
28+
pub ob_mutex: pyo3_ffi::PyMutex,
2529
#[cfg(Py_GIL_DISABLED)]
2630
pub ob_gc_bits: u8,
2731
#[cfg(Py_GIL_DISABLED)]
@@ -41,7 +45,7 @@ fn raise_args_exception() {
4145
unsafe {
4246
let msg = "orjson.Fragment() takes exactly 1 positional argument";
4347
let err_msg =
44-
PyUnicode_FromStringAndSize(msg.as_ptr() as *const c_char, msg.len() as isize);
48+
PyUnicode_FromStringAndSize(msg.as_ptr().cast::<c_char>(), msg.len() as isize);
4549
PyErr_SetObject(PyExc_TypeError, err_msg);
4650
Py_DECREF(err_msg);
4751
};
@@ -68,7 +72,7 @@ pub unsafe extern "C" fn orjson_fragment_tp_new(
6872
#[cfg(Py_GIL_DISABLED)]
6973
_padding: 0,
7074
#[cfg(Py_GIL_DISABLED)]
71-
ob_mutex: PyMutex::new(),
75+
ob_mutex: pyo3_ffi::PyMutex::new(),
7276
#[cfg(Py_GIL_DISABLED)]
7377
ob_gc_bits: 0,
7478
#[cfg(Py_GIL_DISABLED)]
@@ -80,7 +84,7 @@ pub unsafe extern "C" fn orjson_fragment_tp_new(
8084
ob_type: crate::typeref::FRAGMENT_TYPE,
8185
contents: contents,
8286
});
83-
Box::into_raw(obj) as *mut PyObject
87+
Box::into_raw(obj).cast::<PyObject>()
8488
}
8589
}
8690
}
@@ -90,16 +94,18 @@ pub unsafe extern "C" fn orjson_fragment_tp_new(
9094
#[cfg_attr(feature = "optimize", optimize(size))]
9195
pub unsafe extern "C" fn orjson_fragment_dealloc(object: *mut PyObject) {
9296
unsafe {
93-
Py_DECREF((*(object as *mut Fragment)).contents);
94-
std::alloc::dealloc(object as *mut u8, std::alloc::Layout::new::<Fragment>());
97+
Py_DECREF((*object.cast::<Fragment>()).contents);
98+
std::alloc::dealloc(object.cast::<u8>(), core::alloc::Layout::new::<Fragment>());
9599
}
96100
}
97101

98102
#[cfg(Py_GIL_DISABLED)]
99-
const FRAGMENT_TP_FLAGS: AtomicU64 = AtomicU64::new(Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE);
103+
const FRAGMENT_TP_FLAGS: AtomicU64 =
104+
AtomicU64::new(Py_TPFLAGS_DEFAULT | pyo3_ffi::Py_TPFLAGS_IMMUTABLETYPE);
100105

101106
#[cfg(all(Py_3_10, not(Py_GIL_DISABLED)))]
102-
const FRAGMENT_TP_FLAGS: core::ffi::c_ulong = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE;
107+
const FRAGMENT_TP_FLAGS: core::ffi::c_ulong =
108+
Py_TPFLAGS_DEFAULT | pyo3_ffi::Py_TPFLAGS_IMMUTABLETYPE;
103109

104110
#[cfg(not(Py_3_10))]
105111
const FRAGMENT_TP_FLAGS: core::ffi::c_ulong = Py_TPFLAGS_DEFAULT;
@@ -117,7 +123,7 @@ pub unsafe extern "C" fn orjson_fragmenttype_new() -> *mut PyTypeObject {
117123
#[cfg(Py_GIL_DISABLED)]
118124
_padding: 0,
119125
#[cfg(Py_GIL_DISABLED)]
120-
ob_mutex: PyMutex::new(),
126+
ob_mutex: pyo3_ffi::PyMutex::new(),
121127
#[cfg(Py_GIL_DISABLED)]
122128
ob_gc_bits: 0,
123129
#[cfg(Py_GIL_DISABLED)]
@@ -128,11 +134,11 @@ pub unsafe extern "C" fn orjson_fragmenttype_new() -> *mut PyTypeObject {
128134
ob_refcnt: pyo3_ffi::PyObjectObRefcnt { ob_refcnt: 0 },
129135
#[cfg(not(Py_3_12))]
130136
ob_refcnt: 0,
131-
ob_type: core::ptr::addr_of_mut!(PyType_Type),
137+
ob_type: &raw mut PyType_Type,
132138
},
133139
ob_size: 0,
134140
},
135-
tp_name: "orjson.Fragment\0".as_ptr() as *const c_char,
141+
tp_name: c"orjson.Fragment".as_ptr(),
136142
tp_basicsize: core::mem::size_of::<Fragment>() as isize,
137143
tp_itemsize: 0,
138144
tp_dealloc: Some(orjson_fragment_dealloc),

0 commit comments

Comments
 (0)