8000 Make PyObjectRef::{from,into}_raw() use NonNull · RustPython/RustPython@92e02a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 92e02a7

Browse files
committed
Make PyObjectRef::{from,into}_raw() use NonNull
1 parent 0a8b040 commit 92e02a7

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

stdlib/src/sqlite.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod _sqlite {
7676
ffi::{c_int, c_longlong, c_uint, c_void, CStr},
7777
fmt::Debug,
7878
ops::Deref,
79-
ptr::{null, null_mut},
79+
ptr::{null, null_mut, NonNull},
8080
thread::ThreadId,
8181
};
8282

@@ -381,7 +381,7 @@ mod _sqlite {
381381
}
382382

383383
struct CallbackData {
384-
obj: *const PyObject,
384+
obj: NonNull<PyObject>,
385385
vm: *const VirtualMachine,
386386
}
387387

@@ -394,7 +394,7 @@ mod _sqlite {
394394
}
395395

396396
fn retrieve(&self) -> (&PyObject, &VirtualMachine) {
397-
unsafe { (&*self.obj, &*self.vm) }
397+
unsafe { (self.obj.as_ref(), &*self.vm) }
398398
}
399399

400400
unsafe extern "C" fn destructor(data: *mut c_void) {
@@ -439,7 +439,7 @@ mod _sqlite {
439439
let instance = context.aggregate_context::<*const PyObject>();
440440
if unsafe { (*instance).is_null() } {
441441
match cls.call((), vm) {
442-
Ok(obj) => unsafe { *instance = obj.into_raw() },
442+
Ok(obj) => unsafe { *instance = obj.into_raw().as_ptr() },
443443
Err(exc) => {
444444
return context.result_exception(
445445
vm,

vm/src/object/core.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ impl PyWeak {
354354
if !obj_ptr.as_ref().0.ref_count.safe_inc() {
355355
return None;
356356
}
357-
Some(PyObjectRef::from_raw(obj_ptr.as_ptr()))
357+
Some(PyObjectRef::from_raw(obj_ptr))
358358
}
359359
}
360360

@@ -508,8 +508,8 @@ impl ToOwned for PyObject {
508508

509509
impl PyObjectRef {
510510
#[inline(always)]
511-
pub fn into_raw(self) -> *const PyObject {
512-
let ptr = self.as_raw();
511+
pub fn into_raw(self) -> NonNull<PyObject> {
512+
let ptr = self.ptr;
513513
std::mem::forget(self);
514514
ptr
515515
}
@@ -520,10 +520,8 @@ impl PyObjectRef {
520520
/// dropped more than once due to mishandling the reference count by calling this function
521521
/// too many times.
522522
#[inline(always)]
523-
pub unsafe fn from_raw(ptr: *const PyObject) -> Self {
524-
Self {
525-
ptr: unsafe { NonNull::new_unchecked(ptr as *mut PyObject) },
526-
}
523+
pub unsafe fn from_raw(ptr: NonNull<PyObject>) -> Self {
524+
Self { ptr }
527525
}
528526

529527
/// Attempt to downcast this reference to a subclass.

vm/src/object/ext.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ use crate::{
1212
vm::Context,
1313
VirtualMachine,
1414
};
15-
use std::{borrow::Borrow, fmt, marker::PhantomData, ops::Deref, ptr::null_mut};
15+
use std::{
16+
borrow::Borrow,
17+
fmt,
18+
marker::PhantomData,
19+
ops::Deref,
20+
ptr::{null_mut, NonNull},
21+
};
1622

1723
/* Python objects and references.
1824
@@ -352,7 +358,7 @@ impl From<PyObjectRef> for PyAtomicRef<PyObject> {
352358
fn from(obj: PyObjectRef) -> Self {
353359
let obj = obj.into_raw();
354360
Self {
355-
inner: Radium::new(obj as *mut _),
361+
inner: Radium::new(obj.cast().as_ptr()),
356362
_phantom: Default::default(),
357363
}
358364
}
@@ -379,8 +385,8 @@ impl PyAtomicRef<PyObject> {
379385
#[must_use]
380386
pub unsafe fn swap(&self, obj: PyObjectRef) -> PyObjectRef {
381387
let obj = obj.into_raw();
382-
let old = Radium::swap(&self.inner, obj as *mut _, Ordering::AcqRel);
383-
unsafe { PyObjectRef::from_raw(old as _) }
388+
let old = Radium::swap(&self.inner, obj.cast().as_ptr(), Ordering::AcqRel);
389+
unsafe { PyObjectRef::from_raw(NonNull::new_unchecked(old.cast())) }
384390
}
385391

386392
pub fn swap_to_temporary_refs(&self, obj: PyObjectRef, vm: &VirtualMachine) {
@@ -393,7 +399,9 @@ impl PyAtomicRef<PyObject> {
393399

394400
impl From<Option<PyObjectRef>> for PyAtomicRef<Option<PyObject>> {
395401
fn from(obj: Option<PyObjectRef>) -> Self {
396-
let val = obj.map(|x| x.into_raw() as *mut _).unwrap_or(null_mut());
402+
let val = obj
403+
.map(|x| x.into_raw().as_ptr().cast())
404+
.unwrap_or(null_mut());
397405
Self {
398406
inner: Radium::new(val),
399407
_phantom: Default::default(),
@@ -420,13 +428,11 @@ impl PyAtomicRef<Option<PyObject>> {
420428
/// until no more reference can be used via PyAtomicRef::deref()
421429
#[must_use]
422430
pub unsafe fn swap(&self, obj: Option<PyObjectRef>) -> Option<PyObjectRef> {
423-
let val = obj.map(|x| x.into_raw() as *mut _).unwrap_or(null_mut());
431+
let val = obj
432+
.map(|x| x.into_raw().as_ptr().cast())
433+
.unwrap_or(null_mut());
424434
let old = Radium::swap(&self.inner, val, Ordering::AcqRel);
425-
unsafe {
426-
old.cast::<PyObject>()
427-
.as_ref()
428-
.map(|x| PyObjectRef::from_raw(x))
429-
}
435+
unsafe { NonNull::new(old.cast::<PyObject>()).map(|x| PyObjectRef::from_raw(x)) }
430436
}
431437

432438
pub fn swap_to_temporary_refs(&self, obj: Option<PyObjectRef>, vm: &VirtualMachine) {

0 commit comments

Comments
 (0)
0