Skip to content

Commit 563dc0f

Browse files
authored
Separate Debug from PyPayload (#6320)
1 parent bf8152b commit 563dc0f

File tree

6 files changed

+96
-93
lines changed

6 files changed

+96
-93
lines changed

crates/vm/src/builtins/dict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ impl ExactSizeIterator for DictIter<'_> {
751751

752752
#[pyclass]
753753
trait DictView: PyPayload + PyClassDef + Iterable + Representable {
754-
type ReverseIter: PyPayload;
754+
type ReverseIter: PyPayload + std::fmt::Debug;
755755

756756
fn dict(&self) -> &PyDictRef;
757757
fn item(vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef) -> PyObjectRef;

crates/vm/src/object/core.rs

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use super::{
1414
PyAtomicRef,
1515
ext::{AsObject, PyRefExact, PyResult},
16-
payload::PyObjectPayload,
16+
payload::PyPayload,
1717
};
1818
use crate::object::traverse::{MaybeTraverse, Traverse, TraverseFn};
1919
use crate::object::traverse_object::PyObjVTable;
@@ -76,10 +76,10 @@ use std::{
7676
#[derive(Debug)]
7777
pub(super) struct Erased;
7878

79-
pub(super) unsafe fn drop_dealloc_obj<T: PyObjectPayload>(x: *mut PyObject) {
79+
pub(super) unsafe fn drop_dealloc_obj<T: PyPayload>(x: *mut PyObject) {
8080
drop(unsafe { Box::from_raw(x as *mut PyInner<T>) });
8181
}
82-
pub(super) unsafe fn debug_obj<T: PyObjectPayload>(
82+
pub(super) unsafe fn debug_obj<T: PyPayload + std::fmt::Debug>(
8383
x: &PyObject,
8484
f: &mut fmt::Formatter<'_>,
8585
) -> fmt::Result {
@@ -88,10 +88,7 @@ pub(super) unsafe fn debug_obj<T: PyObjectPayload>(
8888
}
8989

9090
/// Call `try_trace` on payload
91-
pub(super) unsafe fn try_trace_obj<T: PyObjectPayload>(
92-
x: &PyObject,
93-
tracer_fn: &mut TraverseFn<'_>,
94-
) {
91+
pub(super) unsafe fn try_trace_obj<T: PyPayload>(x: &PyObject, tracer_fn: &mut TraverseFn<'_>) {
9592
let x = unsafe { &*(x as *const PyObject as *const PyInner<T>) };
9693
let payload = &x.payload;
9794
payload.try_traverse(tracer_fn)
@@ -441,7 +438,7 @@ impl InstanceDict {
441438
}
442439
}
443440

444-
impl<T: PyObjectPayload> PyInner<T> {
441+
impl<T: PyPayload + std::fmt::Debug> PyInner<T> {
445442
fn new(payload: T, typ: PyTypeRef, dict: Option<PyDictRef>) -> Box<Self> {
446443
let member_count = typ.slots.member_count;
447444
Box::new(Self {
@@ -531,15 +528,15 @@ impl PyObjectRef {
531528
/// If the downcast fails, the original ref is returned in as `Err` so
532529
/// another downcast can be attempted without unnecessary cloning.
533530
#[inline(always)]
534-
pub fn downcast<T: PyObjectPayload>(self) -> Result<PyRef<T>, Self> {
531+
pub fn downcast<T: PyPayload>(self) -> Result<PyRef<T>, Self> {
535532
if self.downcastable::<T>() {
536533
Ok(unsafe { self.downcast_unchecked() })
537534
} else {
538535
Err(self)
539536
}
540537
}
541538

542-
pub fn try_downcast<T: PyObjectPayload>(self, vm: &VirtualMachine) -> PyResult<PyRef<T>> {
539+
pub fn try_downcast<T: PyPayload>(self, vm: &VirtualMachine) -> PyResult<PyRef<T>> {
543540
T::try_downcast_from(&self, vm)?;
544541
Ok(unsafe { self.downcast_unchecked() })
545542
}
@@ -565,10 +562,7 @@ impl PyObjectRef {
565562
/// If the downcast fails, the original ref is returned in as `Err` so
566563
/// another downcast can be attempted without unnecessary cloning.
567564
#[inline]
568-
pub fn downcast_exact<T: PyObjectPayload + crate::PyPayload>(
569-
self,
570-
vm: &VirtualMachine,
571-
) -> Result<PyRefExact<T>, Self> {
565+
pub fn downcast_exact<T: PyPayload>(self, vm: &VirtualMachine) -> Result<PyRefExact<T>, Self> {
572566
if self.class().is(T::class(&vm.ctx)) {
573567
// TODO: is this always true?
574568
assert!(
@@ -638,7 +632,7 @@ impl PyObject {
638632

639633
#[deprecated(note = "use downcastable instead")]
640634
#[inline(always)]
641-
pub fn payload_is<T: PyObjectPayload>(&self) -> bool {
635+
pub fn payload_is<T: PyPayload>(&self) -> bool {
642636
self.0.typeid == T::payload_type_id()
643637
}
644638

@@ -648,7 +642,7 @@ impl PyObject {
648642
/// The actual payload type must be T.
649643
#[deprecated(note = "use downcast_unchecked_ref instead")]
650644
#[inline(always)]
651-
pub const unsafe fn payload_unchecked<T: PyObjectPayload>(&self) -> &T {
645+
pub const unsafe fn payload_unchecked<T: PyPayload>(&self) -> &T {
652646
// we cast to a PyInner<T> first because we don't know T's exact offset because of
653647
// varying alignment, but once we get a PyInner<T> the compiler can get it for us
654648
let inner = unsafe { &*(&self.0 as *const PyInner<Erased> as *const PyInner<T>) };
@@ -657,7 +651,7 @@ impl PyObject {
657651

658652
#[deprecated(note = "use downcast_ref instead")]
659653
#[inline(always)]
660-
pub fn payload<T: PyObjectPayload>(&self) -> Option<&T> {
654+
pub fn payload<T: PyPayload>(&self) -> Option<&T> {
661655
#[allow(deprecated)]
662656
if self.payload_is::<T>() {
663657
#[allow(deprecated)]
@@ -678,10 +672,7 @@ impl PyObject {
678672

679673
#[deprecated(note = "use downcast_ref_if_exact instead")]
680674
#[inline(always)]
681-
pub fn payload_if_exact<T: PyObjectPayload + crate::PyPayload>(
682-
&self,
683-
vm: &VirtualMachine,
684-
) -> Option<&T> {
675+
pub fn payload_if_exact<T: PyPayload>(&self, vm: &VirtualMachine) -> Option<&T> {
685676
if self.class().is(T::class(&vm.ctx)) {
686677
#[allow(deprecated)]
687678
self.payload()
@@ -730,12 +721,12 @@ impl PyObject {
730721

731722
/// Check if this object can be downcast to T.
732723
#[inline(always)]
733-
pub fn downcastable<T: PyObjectPayload>(&self) -> bool {
724+
pub fn downcastable<T: PyPayload>(&self) -> bool {
734725
T::downcastable_from(self)
735726
}
736727

737728
/// Attempt to downcast this reference to a subclass.
738-
pub fn try_downcast_ref<'a, T: PyObjectPayload>(
729+
pub fn try_downcast_ref<'a, T: PyPayload>(
739730
&'a self,
740731
vm: &VirtualMachine,
741732
) -> PyResult<&'a Py<T>> {
@@ -745,7 +736,7 @@ impl PyObject {
745736

746737
/// Attempt to downcast this reference to a subclass.
747738
#[inline(always)]
748-
pub fn downcast_ref<T: PyObjectPayload>(&self) -> Option<&Py<T>> {
739+
pub fn downcast_ref<T: PyPayload>(&self) -> Option<&Py<T>> {
749740
if self.downcastable::<T>() {
750741
// SAFETY: just checked that the payload is T, and PyRef is repr(transparent) over
751742
// PyObjectRef
@@ -756,10 +747,7 @@ impl PyObject {
756747
}
757748

758749
#[inline(always)]
759-
pub fn downcast_ref_if_exact<T: PyObjectPayload + crate::PyPayload>(
760-
&self,
761-
vm: &VirtualMachine,
762-
) -> Option<&Py<T>> {
750+
pub fn downcast_ref_if_exact<T: PyPayload>(&self, vm: &VirtualMachine) -> Option<&Py<T>> {
763751
self.class()
764752
.is(T::class(&vm.ctx))
765753
.then(|| unsafe { self.downcast_unchecked_ref::<T>() })
@@ -768,7 +756,7 @@ impl PyObject {
768756
/// # Safety
769757
/// T must be the exact payload type
770758
#[inline(always)]
771-
pub unsafe fn downcast_unchecked_ref<T: PyObjectPayload>(&self) -> &Py<T> {
759+
pub unsafe fn downcast_unchecked_ref<T: PyPayload>(&self) -> &Py<T> {
772760
debug_assert!(self.downcastable::<T>());
773761
// SAFETY: requirements forwarded from caller
774762
unsafe { &*(self as *const Self as *const Py<T>) }
@@ -875,7 +863,7 @@ impl AsRef<PyObject> for PyObjectRef {
875863
}
876864
}
877865

878-
impl<'a, T: PyObjectPayload> From<&'a Py<T>> for &'a PyObject {
866+
impl<'a, T: PyPayload> From<&'a Py<T>> for &'a PyObject {
879867
#[inline(always)]
880868
fn from(py_ref: &'a Py<T>) -> Self {
881869
py_ref.as_object()
@@ -908,7 +896,7 @@ impl fmt::Debug for PyObjectRef {
908896
#[repr(transparent)]
909897
pub struct Py<T>(PyInner<T>);
910898

911-
impl<T: PyObjectPayload> Py<T> {
899+
impl<T: PyPayload> Py<T> {
912900
pub fn downgrade(
913901
&self,
914902
callback: Option<PyObjectRef>,
@@ -947,7 +935,7 @@ impl<T> Deref for Py<T> {
947935
}
948936
}
949937

950-
impl<T: PyObjectPayload> Borrow<PyObject> for Py<T> {
938+
impl<T: PyPayload> Borrow<PyObject> for Py<T> {
951939
#[inline(always)]
952940
fn borrow(&self) -> &PyObject {
953941
unsafe { &*(&self.0 as *const PyInner<T> as *const PyObject) }
@@ -956,7 +944,7 @@ impl<T: PyObjectPayload> Borrow<PyObject> for Py<T> {
956944

957945
impl<T> std::hash::Hash for Py<T>
958946
where
959-
T: std::hash::Hash + PyObjectPayload,
947+
T: std::hash::Hash + PyPayload,
960948
{
961949
#[inline]
962950
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
@@ -966,27 +954,27 @@ where
966954

967955
impl<T> PartialEq for Py<T>
968956
where
969-
T: PartialEq + PyObjectPayload,
957+
T: PartialEq + PyPayload,
970958
{
971959
#[inline]
972960
fn eq(&self, other: &Self) -> bool {
973961
self.deref().eq(other.deref())
974962
}
975963
}
976964

977-
impl<T> Eq for Py<T> where T: Eq + PyObjectPayload {}
965+
impl<T> Eq for Py<T> where T: Eq + PyPayload {}
978966

979967
impl<T> AsRef<PyObject> for Py<T>
980968
where
981-
T: PyObjectPayload,
969+
T: PyPayload,
982970
{
983971
#[inline(always)]
984972
fn as_ref(&self) -> &PyObject {
985973
self.borrow()
986974
}
987975
}
988976

989-
impl<T: PyObjectPayload> fmt::Debug for Py<T> {
977+
impl<T: PyPayload + std::fmt::Debug> fmt::Debug for Py<T> {
990978
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
991979
(**self).fmt(f)
992980
}
@@ -1035,7 +1023,7 @@ impl<T> Clone for PyRef<T> {
10351023
}
10361024
}
10371025

1038-
impl<T: PyObjectPayload> PyRef<T> {
1026+
impl<T: PyPayload> PyRef<T> {
10391027
// #[inline(always)]
10401028
// pub(crate) const fn into_non_null(self) -> NonNull<Py<T>> {
10411029
// let ptr = self.ptr;
@@ -1065,24 +1053,26 @@ impl<T: PyObjectPayload> PyRef<T> {
10651053
}
10661054
}
10671055

1056+
pub const fn leak(pyref: Self) -> &'static Py<T> {
1057+
let ptr = pyref.ptr;
1058+
std::mem::forget(pyref);
1059+
unsafe { ptr.as_ref() }
1060+
}
1061+
}
1062+
1063+
impl<T: PyPayload + std::fmt::Debug> PyRef<T> {
10681064
#[inline(always)]
10691065
pub fn new_ref(payload: T, typ: crate::builtins::PyTypeRef, dict: Option<PyDictRef>) -> Self {
10701066
let inner = Box::into_raw(PyInner::new(payload, typ, dict));
10711067
Self {
10721068
ptr: unsafe { NonNull::new_unchecked(inner.cast::<Py<T>>()) },
10731069
}
10741070
}
1075-
1076-
pub const fn leak(pyref: Self) -> &'static Py<T> {
1077-
let ptr = pyref.ptr;
1078-
std::mem::forget(pyref);
1079-
unsafe { ptr.as_ref() }
1080-
}
10811071
}
10821072

10831073
impl<T> Borrow<PyObject> for PyRef<T>
10841074
where
1085-
T: PyObjectPayload,
1075+
T: PyPayload,
10861076
{
10871077
#[inline(always)]
10881078
fn borrow(&self) -> &PyObject {
@@ -1092,7 +1082,7 @@ where
10921082

10931083
impl<T> AsRef<PyObject> for PyRef<T>
10941084
where
1095-
T: PyObjectPayload,
1085+
T: PyPayload,
10961086
{
10971087
#[inline(always)]
10981088
fn as_ref(&self) -> &PyObject {
@@ -1133,7 +1123,7 @@ impl<T> Deref for PyRef<T> {
11331123

11341124
impl<T> std::hash::Hash for PyRef<T>
11351125
where
1136-
T: std::hash::Hash + PyObjectPayload,
1126+
T: std::hash::Hash + PyPayload,
11371127
{
11381128
#[inline]
11391129
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
@@ -1143,23 +1133,23 @@ where
11431133

11441134
impl<T> PartialEq for PyRef<T>
11451135
where
1146-
T: PartialEq + PyObjectPayload,
1136+
T: PartialEq + PyPayload,
11471137
{
11481138
#[inline]
11491139
fn eq(&self, other: &Self) -> bool {
11501140
self.deref().eq(other.deref())
11511141
}
11521142
}
11531143

1154-
impl<T> Eq for PyRef<T> where T: Eq + PyObjectPayload {}
1144+
impl<T> Eq for PyRef<T> where T: Eq + PyPayload {}
11551145

11561146
#[repr(transparent)]
1157-
pub struct PyWeakRef<T: PyObjectPayload> {
1147+
pub struct PyWeakRef<T: PyPayload> {
11581148
weak: PyRef<PyWeak>,
11591149
_marker: PhantomData<T>,
11601150
}
11611151

1162-
impl<T: PyObjectPayload> PyWeakRef<T> {
1152+
impl<T: PyPayload> PyWeakRef<T> {
11631153
pub fn upgrade(&self) -> Option<PyRef<T>> {
11641154
self.weak
11651155
.upgrade()

0 commit comments

Comments
 (0)