Skip to content

Commit af6ea3f

Browse files
committed
Use std::ptr::eq where relevant
1 parent 561c699 commit af6ea3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+204
-129
lines changed

pyo3-ffi/src/abstract_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ extern "C" {
202202
#[inline]
203203
pub unsafe fn PyIndex_Check(o: *mut PyObject) -> c_int {
204204
let tp_as_number = (*Py_TYPE(o)).tp_as_number;
205-
(!tp_as_number.is_null() && (*tp_as_number).nb_index.is_some()) as c_int
205+
(!tp_as_number.is_null() && (*tp_as_number).nb_index.is_some()).into()
206206
}
207207

208208
extern "C" {

pyo3-ffi/src/boolobject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
use crate::longobject::PyLongObject;
33
use crate::object::*;
44
use std::os::raw::{c_int, c_long};
5-
use std::ptr::addr_of_mut;
5+
use std::ptr;
6+
use std::ptr::{addr_of, addr_of_mut};
67

78
#[inline]
89
pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int {
9-
(Py_TYPE(op) == addr_of_mut!(PyBool_Type)) as c_int
10+
ptr::eq(Py_TYPE(op), addr_of!(PyBool_Type)).into()
1011
}
1112

1213
#[cfg_attr(windows, link(name = "pythonXY"))]

pyo3-ffi/src/bytearrayobject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::object::*;
22
use crate::pyport::Py_ssize_t;
33
use std::os::raw::{c_char, c_int};
4-
use std::ptr::addr_of_mut;
4+
use std::ptr;
5+
use std::ptr::{addr_of, addr_of_mut};
56

67
#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
78
#[repr(C)]
@@ -34,7 +35,7 @@ pub unsafe fn PyByteArray_Check(op: *mut PyObject) -> c_int {
3435

3536
#[inline]
3637
pub unsafe fn PyByteArray_CheckExact(op: *mut PyObject) -> c_int {
37-
(Py_TYPE(op) == addr_of_mut!(PyByteArray_Type)) as c_int
38+
ptr::eq(Py_TYPE(op), addr_of!(PyByteArray_Type)).into()
3839
}
3940

4041
extern "C" {

pyo3-ffi/src/bytesobject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::object::*;
22
use crate::pyport::Py_ssize_t;
33
use std::os::raw::{c_char, c_int};
4-
use std::ptr::addr_of_mut;
4+
use std::ptr;
5+
use std::ptr::addr_of;
56

67
#[cfg_attr(windows, link(name = "pythonXY"))]
78
extern "C" {
@@ -17,7 +18,7 @@ pub unsafe fn PyBytes_Check(op: *mut PyObject) -> c_int {
1718

1819
#[inline]
1920
pub unsafe fn PyBytes_CheckExact(op: *mut PyObject) -> c_int {
20-
(Py_TYPE(op) == addr_of_mut!(PyBytes_Type)) as c_int
21+
ptr::eq(Py_TYPE(op), addr_of!(PyBytes_Type)).into()
2122
}
2223

2324
extern "C" {

pyo3-ffi/src/compat/py_3_13.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ compat_function!(
7575
*pobj = std::ptr::null_mut();
7676
return -1;
7777
}
78-
if obj == Py_None() {
78+
if std::ptr::eq(obj, Py_None()) {
7979
*pobj = std::ptr::null_mut();
8080
return 0;
8181
}

pyo3-ffi/src/context.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::object::{PyObject, PyTypeObject, Py_TYPE};
22
use std::os::raw::{c_char, c_int};
3-
use std::ptr::addr_of_mut;
3+
use std::ptr;
4+
use std::ptr::addr_of;
45

56
extern "C" {
67
pub static mut PyContext_Type: PyTypeObject;
@@ -13,17 +14,17 @@ extern "C" {
1314

1415
#[inline]
1516
pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int {
16-
(Py_TYPE(op) == addr_of_mut!(PyContext_Type)) as c_int
17+
ptr::eq(Py_TYPE(op), addr_of!(PyContext_Type)).into()
1718
}
1819

1920
#[inline]
2021
pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int {
21-
(Py_TYPE(op) == addr_of_mut!(PyContextVar_Type)) as c_int
22+
ptr::eq(Py_TYPE(op), addr_of!(PyContextVar_Type)).into()
2223
}
2324

2425
#[inline]
2526
pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int {
26-
(Py_TYPE(op) == addr_of_mut!(PyContextToken_Type)) as c_int
27+
ptr::eq(Py_TYPE(op), addr_of!(PyContextToken_Type)).into()
2728
}
2829

2930
extern "C" {

pyo3-ffi/src/cpython/abstract_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ extern "C" {
227227
#[inline]
228228
pub unsafe fn PyObject_CheckBuffer(o: *mut PyObject) -> c_int {
229229
let tp_as_buffer = (*crate::Py_TYPE(o)).tp_as_buffer;
230-
(!tp_as_buffer.is_null() && (*tp_as_buffer).bf_getbuffer.is_some()) as c_int
230+
(!tp_as_buffer.is_null() && (*tp_as_buffer).bf_getbuffer.is_some()).into()
231231
}
232232

233233
#[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11

pyo3-ffi/src/cpython/code.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::object::*;
22
use crate::pyport::Py_ssize_t;
3-
43
#[allow(unused_imports)]
54
use std::os::raw::{c_char, c_int, c_short, c_uchar, c_void};
65
#[cfg(not(any(PyPy, GraalPy)))]
7-
use std::ptr::addr_of_mut;
6+
use std::ptr;
7+
#[cfg(not(any(PyPy, GraalPy)))]
8+
use std::ptr::addr_of;
89

910
#[cfg(all(Py_3_8, not(any(PyPy, GraalPy)), not(Py_3_11)))]
1011
opaque_struct!(_PyOpcache);
@@ -244,7 +245,7 @@ extern "C" {
244245
#[inline]
245246
#[cfg(not(any(PyPy, GraalPy)))]
246247
pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int {
247-
(Py_TYPE(op) == addr_of_mut!(PyCode_Type)) as c_int
248+
ptr::eq(Py_TYPE(op), addr_of!(PyCode_Type)).into()
248249
}
249250

250251
#[inline]

pyo3-ffi/src/cpython/frameobject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use crate::pystate::PyThreadState;
66
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
77
use std::os::raw::c_char;
88
use std::os::raw::c_int;
9-
use std::ptr::addr_of_mut;
9+
use std::ptr;
10+
use std::ptr::addr_of;
1011

1112
#[cfg(not(any(PyPy, GraalPy, Py_3_11)))]
1213
pub type PyFrameState = c_char;
@@ -66,7 +67,7 @@ extern "C" {
6667

6768
#[inline]
6869
pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int {
69-
(Py_TYPE(op) == addr_of_mut!(PyFrame_Type)) as c_int
70+
ptr::eq(Py_TYPE(op), addr_of!(PyFrame_Type)).into()
7071
}
7172

7273
extern "C" {

pyo3-ffi/src/cpython/funcobject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::os::raw::c_int;
2+
use std::ptr;
23
#[cfg(not(all(PyPy, not(Py_3_8))))]
3-
use std::ptr::addr_of_mut;
4+
use std::ptr::addr_of;
45

56
use crate::PyObject;
67

@@ -70,7 +71,7 @@ extern "C" {
7071
#[cfg(not(all(PyPy, not(Py_3_8))))]
7172
#[inline]
7273
pub unsafe fn PyFunction_Check(op: *mut PyObject) -> c_int {
73-
(crate::Py_TYPE(op) == addr_of_mut!(PyFunction_Type)) as c_int
74+
ptr::eq(crate::Py_TYPE(op), addr_of!(PyFunction_Type)).into()
7475
}
7576

7677
extern "C" {

pyo3-ffi/src/cpython/genobject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::_PyErr_StackItem;
55
#[cfg(all(Py_3_11, not(any(PyPy, GraalPy))))]
66
use std::os::raw::c_char;
77
use std::os::raw::c_int;
8-
use std::ptr::addr_of_mut;
8+
use std::ptr;
9+
use std::ptr::{addr_of, addr_of_mut};
910

1011
#[cfg(not(any(PyPy, GraalPy)))]
1112
#[repr(C)]
@@ -47,7 +48,7 @@ pub unsafe fn PyGen_Check(op: *mut PyObject) -> c_int {
4748

4849
#[inline]
4950
pub unsafe fn PyGen_CheckExact(op: *mut PyObject) -> c_int {
50-
(Py_TYPE(op) == addr_of_mut!(PyGen_Type)) as c_int
51+
ptr::eq(Py_TYPE(op), addr_of!(PyGen_Type)).into()
5152
}
5253

5354
extern "C" {

pyo3-ffi/src/cpython/methodobject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::object::*;
22
#[cfg(not(GraalPy))]
33
use crate::{PyCFunctionObject, PyMethodDefPointer, METH_METHOD, METH_STATIC};
44
use std::os::raw::c_int;
5-
use std::ptr::addr_of_mut;
5+
use std::ptr;
6+
use std::ptr::{addr_of, addr_of_mut};
67

78
#[cfg(not(GraalPy))]
89
pub struct PyCMethodObject {
@@ -17,7 +18,7 @@ extern "C" {
1718

1819
#[inline]
1920
pub unsafe fn PyCMethod_CheckExact(op: *mut PyObject) -> c_int {
20-
(Py_TYPE(op) == addr_of_mut!(PyCMethod_Type)) as c_int
21+
ptr::eq(Py_TYPE(op), addr_of!(PyCMethod_Type)).into()
2122
}
2223

2324
#[inline]

pyo3-ffi/src/cpython/objimpl.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int {
4949
&& match (*Py_TYPE(o)).tp_is_gc {
5050
Some(tp_is_gc) => tp_is_gc(o) != 0,
5151
None => true,
52-
}) as c_int
52+
})
53+
.into()
5354
}
5455

5556
#[cfg(not(Py_3_11))]
@@ -60,7 +61,7 @@ extern "C" {
6061

6162
#[inline]
6263
pub unsafe fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int {
63-
((*t).tp_weaklistoffset > 0) as c_int
64+
((*t).tp_weaklistoffset > 0).into()
6465
}
6566

6667
#[inline]

pyo3-ffi/src/datetime.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ pub unsafe fn _get_attr(obj: *mut PyObject, field: &std::ffi::CStr) -> c_int {
349349
let result = PyObject_GetAttrString(obj, field.as_ptr());
350350
Py_DecRef(result); // the original macros are borrowing
351351
if PyLong_Check(result) == 1 {
352-
PyLong_AsLong(result) as c_int
352+
PyLong_AsLong(result).into()
353353
} else {
354354
0
355355
}
@@ -636,61 +636,61 @@ pub unsafe fn PyDateTime_TimeZone_UTC() -> *mut PyObject {
636636
#[inline]
637637
/// Check if `op` is a `PyDateTimeAPI.DateType` or subtype.
638638
pub unsafe fn PyDate_Check(op: *mut PyObject) -> c_int {
639-
PyObject_TypeCheck(op, (*PyDateTimeAPI()).DateType) as c_int
639+
PyObject_TypeCheck(op, (*PyDateTimeAPI()).DateType)
640640
}
641641

642642
#[inline]
643643
/// Check if `op`'s type is exactly `PyDateTimeAPI.DateType`.
644644
pub unsafe fn PyDate_CheckExact(op: *mut PyObject) -> c_int {
645-
(Py_TYPE(op) == (*PyDateTimeAPI()).DateType) as c_int
645+
ptr::eq(Py_TYPE(op), (*PyDateTimeAPI()).DateType).into()
646646
}
647647

648648
#[inline]
649649
/// Check if `op` is a `PyDateTimeAPI.DateTimeType` or subtype.
650650
pub unsafe fn PyDateTime_Check(op: *mut PyObject) -> c_int {
651-
PyObject_TypeCheck(op, (*PyDateTimeAPI()).DateTimeType) as c_int
651+
PyObject_TypeCheck(op, (*PyDateTimeAPI()).DateTimeType)
652652
}
653653

654654
#[inline]
655655
/// Check if `op`'s type is exactly `PyDateTimeAPI.DateTimeType`.
656656
pub unsafe fn PyDateTime_CheckExact(op: *mut PyObject) -> c_int {
657-
(Py_TYPE(op) == (*PyDateTimeAPI()).DateTimeType) as c_int
657+
ptr::eq(Py_TYPE(op), (*PyDateTimeAPI()).DateTimeType).into()
658658
}
659659

660660
#[inline]
661661
/// Check if `op` is a `PyDateTimeAPI.TimeType` or subtype.
662662
pub unsafe fn PyTime_Check(op: *mut PyObject) -> c_int {
663-
PyObject_TypeCheck(op, (*PyDateTimeAPI()).TimeType) as c_int
663+
PyObject_TypeCheck(op, (*PyDateTimeAPI()).TimeType)
664664
}
665665

666666
#[inline]
667667
/// Check if `op`'s type is exactly `PyDateTimeAPI.TimeType`.
668668
pub unsafe fn PyTime_CheckExact(op: *mut PyObject) -> c_int {
669-
(Py_TYPE(op) == (*PyDateTimeAPI()).TimeType) as c_int
669+
ptr::eq(Py_TYPE(op), (*PyDateTimeAPI()).TimeType).into()
670670
}
671671

672672
#[inline]
673673
/// Check if `op` is a `PyDateTimeAPI.DetaType` or subtype.
674674
pub unsafe fn PyDelta_Check(op: *mut PyObject) -> c_int {
675-
PyObject_TypeCheck(op, (*PyDateTimeAPI()).DeltaType) as c_int
675+
PyObject_TypeCheck(op, (*PyDateTimeAPI()).DeltaType)
676676
}
677677

678678
#[inline]
679679
/// Check if `op`'s type is exactly `PyDateTimeAPI.DeltaType`.
680680
pub unsafe fn PyDelta_CheckExact(op: *mut PyObject) -> c_int {
681-
(Py_TYPE(op) == (*PyDateTimeAPI()).DeltaType) as c_int
681+
ptr::eq(Py_TYPE(op), (*PyDateTimeAPI()).DeltaType).into()
682682
}
683683

684684
#[inline]
685685
/// Check if `op` is a `PyDateTimeAPI.TZInfoType` or subtype.
686686
pub unsafe fn PyTZInfo_Check(op: *mut PyObject) -> c_int {
687-
PyObject_TypeCheck(op, (*PyDateTimeAPI()).TZInfoType) as c_int
687+
PyObject_TypeCheck(op, (*PyDateTimeAPI()).TZInfoType)
688688
}
689689

690690
#[inline]
691691
/// Check if `op`'s type is exactly `PyDateTimeAPI.TZInfoType`.
692692
pub unsafe fn PyTZInfo_CheckExact(op: *mut PyObject) -> c_int {
693-
(Py_TYPE(op) == (*PyDateTimeAPI()).TZInfoType) as c_int
693+
ptr::eq(Py_TYPE(op), (*PyDateTimeAPI()).TZInfoType).into()
694694
}
695695

696696
// skipped non-limited PyDate_FromDate

pyo3-ffi/src/dictobject.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::object::*;
22
use crate::pyport::Py_ssize_t;
33
use std::os::raw::{c_char, c_int};
4-
use std::ptr::addr_of_mut;
4+
use std::ptr;
5+
use std::ptr::addr_of;
56

67
#[cfg_attr(windows, link(name = "pythonXY"))]
78
extern "C" {
@@ -16,7 +17,7 @@ pub unsafe fn PyDict_Check(op: *mut PyObject) -> c_int {
1617

1718
#[inline]
1819
pub unsafe fn PyDict_CheckExact(op: *mut PyObject) -> c_int {
19-
(Py_TYPE(op) == addr_of_mut!(PyDict_Type)) as c_int
20+
ptr::eq(Py_TYPE(op), addr_of!(PyDict_Type)).into()
2021
}
2122

2223
extern "C" {
@@ -85,22 +86,22 @@ extern "C" {
8586

8687
#[inline]
8788
pub unsafe fn PyDictKeys_Check(op: *mut PyObject) -> c_int {
88-
(Py_TYPE(op) == addr_of_mut!(PyDictKeys_Type)) as c_int
89+
ptr::eq(Py_TYPE(op), addr_of!(PyDictKeys_Type)).into()
8990
}
9091

9192
#[inline]
9293
pub unsafe fn PyDictValues_Check(op: *mut PyObject) -> c_int {
93-
(Py_TYPE(op) == addr_of_mut!(PyDictValues_Type)) as c_int
94+
ptr::eq(Py_TYPE(op), addr_of!(PyDictValues_Type)).into()
9495
}
9596

9697
#[inline]
9798
pub unsafe fn PyDictItems_Check(op: *mut PyObject) -> c_int {
98-
(Py_TYPE(op) == addr_of_mut!(PyDictItems_Type)) as c_int
99+
ptr::eq(Py_TYPE(op), addr_of!(PyDictItems_Type)).into()
99100
}
100101

101102
#[inline]
102103
pub unsafe fn PyDictViewSet_Check(op: *mut PyObject) -> c_int {
103-
(PyDictKeys_Check(op) != 0 || PyDictItems_Check(op) != 0) as c_int
104+
(PyDictKeys_Check(op) != 0 || PyDictItems_Check(op) != 0).into()
104105
}
105106

106107
#[cfg_attr(windows, link(name = "pythonXY"))]

pyo3-ffi/src/floatobject.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::object::*;
22
use std::os::raw::{c_double, c_int};
3-
use std::ptr::addr_of_mut;
3+
use std::ptr;
4+
use std::ptr::{addr_of, addr_of_mut};
45

56
#[cfg(Py_LIMITED_API)]
67
// TODO: remove (see https://github.com/PyO3/pyo3/pull/1341#issuecomment-751515985)
@@ -19,7 +20,7 @@ pub unsafe fn PyFloat_Check(op: *mut PyObject) -> c_int {
1920

2021
#[inline]
2122
pub unsafe fn PyFloat_CheckExact(op: *mut PyObject) -> c_int {
22-
(Py_TYPE(op) == addr_of_mut!(PyFloat_Type)) as c_int
23+
ptr::eq(Py_TYPE(op), addr_of!(PyFloat_Type)).into()
2324
}
2425

2526
// skipped Py_RETURN_NAN

pyo3-ffi/src/iterobject.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::object::*;
22
use std::os::raw::c_int;
3-
use std::ptr::addr_of_mut;
3+
use std::ptr;
4+
use std::ptr::addr_of;
45

56
#[cfg_attr(windows, link(name = "pythonXY"))]
67
extern "C" {
@@ -10,7 +11,7 @@ extern "C" {
1011

1112
#[inline]
1213
pub unsafe fn PySeqIter_Check(op: *mut PyObject) -> c_int {
13-
(Py_TYPE(op) == addr_of_mut!(PySeqIter_Type)) as c_int
14+
ptr::eq(Py_TYPE(op), addr_of!(PySeqIter_Type)).into()
1415
}
1516

1617
extern "C" {
@@ -20,7 +21,7 @@ extern "C" {
2021

2122
#[inline]
2223
pub unsafe fn PyCallIter_Check(op: *mut PyObject) -> c_int {
23-
(Py_TYPE(op) == addr_of_mut!(PyCallIter_Type)) as c_int
24+
ptr::eq(Py_TYPE(op), addr_of!(PyCallIter_Type)).into()
2425
}
2526

2627
extern "C" {

0 commit comments

Comments
 (0)