Skip to content

Commit ef416be

Browse files
coolreader18youknowone
authored andcommitted
Move typeid into vtable
1 parent 7b9f0d9 commit ef416be

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

crates/vm/src/builtins/str.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,9 +1946,7 @@ impl PyPayload for PyUtf8Str {
19461946
ctx.types.str_type
19471947
}
19481948

1949-
fn payload_type_id() -> core::any::TypeId {
1950-
core::any::TypeId::of::<PyStr>()
1951-
}
1949+
const PAYLOAD_TYPE_ID: std::any::TypeId = std::any::TypeId::of::<PyStr>();
19521950

19531951
fn validate_downcastable_from(obj: &PyObject) -> bool {
19541952
// SAFETY: we know the object is a PyStr in this context

crates/vm/src/object/core.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use super::{
1515
ext::{AsObject, PyRefExact, PyResult},
1616
payload::PyPayload,
1717
};
18-
use crate::object::traverse_object::PyObjVTable;
1918
use crate::{
2019
builtins::{PyDictRef, PyType, PyTypeRef},
2120
common::{
@@ -24,6 +23,7 @@ use crate::{
2423
lock::{PyMutex, PyMutexGuard, PyRwLock},
2524
refcount::RefCount,
2625
},
26+
object::{PyObjectPayload, traverse_object::PyObjVTable},
2727
vm::VirtualMachine,
2828
};
2929
use crate::{
@@ -105,8 +105,6 @@ pub(super) unsafe fn try_trace_obj<T: PyPayload>(x: &PyObject, tracer_fn: &mut T
105105
#[repr(C)]
106106
pub(super) struct PyInner<T> {
107107
pub(super) ref_count: RefCount,
108-
// TODO: move typeid into vtable once TypeId::of is const
109-
pub(super) typeid: TypeId,
110108
pub(super) vtable: &'static PyObjVTable,
111109

112110
pub(super) typ: PyAtomicRef<PyType>, // __class__ member
@@ -449,7 +447,6 @@ impl<T: PyPayload + core::fmt::Debug> PyInner<T> {
449447
let member_count = typ.slots.member_count;
450448
Box::new(Self {
451449
ref_count: RefCount::new(),
452-
typeid: T::payload_type_id(),
453450
vtable: PyObjVTable::of::<T>(),
454451
typ: PyAtomicRef::from(typ),
455452
dict: dict.map(InstanceDict::new),
@@ -638,8 +635,8 @@ impl PyObject {
638635

639636
#[deprecated(note = "use downcastable instead")]
640637
#[inline(always)]
641-
pub fn payload_is<T: PyPayload>(&self) -> bool {
642-
self.0.typeid == T::payload_type_id()
638+
pub fn payload_is<T: PyObjectPayload>(&self) -> bool {
639+
self.0.vtable.typeid == T::PAYLOAD_TYPE_ID
643640
}
644641

645642
/// Force to return payload as T.
@@ -657,7 +654,7 @@ impl PyObject {
657654

658655
#[deprecated(note = "use downcast_ref instead")]
659656
#[inline(always)]
660-
pub fn payload<T: PyPayload>(&self) -> Option<&T> {
657+
pub fn payload<T: PyPayload + std::fmt::Debug>(&self) -> Option<&T> {
661658
#[allow(deprecated)]
662659
if self.payload_is::<T>() {
663660
#[allow(deprecated)]
@@ -678,7 +675,10 @@ impl PyObject {
678675

679676
#[deprecated(note = "use downcast_ref_if_exact instead")]
680677
#[inline(always)]
681-
pub fn payload_if_exact<T: PyPayload>(&self, vm: &VirtualMachine) -> Option<&T> {
678+
pub fn payload_if_exact<T: PyPayload + std::fmt::Debug>(
679+
&self,
680+
vm: &VirtualMachine,
681+
) -> Option<&T> {
682682
if self.class().is(T::class(&vm.ctx)) {
683683
#[allow(deprecated)]
684684
self.payload()
@@ -711,7 +711,10 @@ impl PyObject {
711711

712712
#[deprecated(note = "use downcast_ref instead")]
713713
#[inline(always)]
714-
pub fn payload_if_subclass<T: crate::PyPayload>(&self, vm: &VirtualMachine) -> Option<&T> {
714+
pub fn payload_if_subclass<T: crate::PyPayload + std::fmt::Debug>(
715+
&self,
716+
vm: &VirtualMachine,
717+
) -> Option<&T> {
715718
if self.class().fast_issubclass(T::class(&vm.ctx)) {
716719
#[allow(deprecated)]
717720
self.payload()
@@ -722,7 +725,7 @@ impl PyObject {
722725

723726
#[inline]
724727
pub(crate) fn typeid(&self) -> TypeId {
725-
self.0.typeid
728+
self.0.vtable.typeid
726729
}
727730

728731
/// Check if this object can be downcast to T.
@@ -1276,7 +1279,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
12761279
let type_type_ptr = Box::into_raw(Box::new(partially_init!(
12771280
PyInner::<PyType> {
12781281
ref_count: RefCount::new(),
1279-
typeid: TypeId::of::<PyType>(),
12801282
vtable: PyObjVTable::of::<PyType>(),
12811283
dict: None,
12821284
weak_list: WeakRefList::new(),
@@ -1288,7 +1290,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
12881290
let object_type_ptr = Box::into_raw(Box::new(partially_init!(
12891291
PyInner::<PyType> {
12901292
ref_count: RefCount::new(),
1291-
typeid: TypeId::of::<PyType>(),
12921293
vtable: PyObjVTable::of::<PyType>(),
12931294
dict: None,
12941295
weak_list: WeakRefList::new(),

crates/vm/src/object/payload.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ pub trait PyPayload: MaybeTraverse + PyThreadingConstraint + Sized + 'static {
3030
fn payload_type_id() -> core::any::TypeId {
3131
core::any::TypeId::of::<Self>()
3232
}
33+
const PAYLOAD_TYPE_ID: std::any::TypeId = std::any::TypeId::of::<Self>();
3334

3435
/// # Safety: this function should only be called if `payload_type_id` matches the type of `obj`.
3536
#[inline]
3637
fn downcastable_from(obj: &PyObject) -> bool {
37-
obj.typeid() == Self::payload_type_id() && Self::validate_downcastable_from(obj)
38+
obj.typeid() == Self::PAYLOAD_TYPE_ID && Self::validate_downcastable_from(obj)
3839
}
3940

4041
#[inline]

crates/vm/src/object/traverse_object.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use alloc::fmt;
2+
use std::any::TypeId;
23

34
use crate::{
45
PyObject,
@@ -11,6 +12,7 @@ use crate::{
1112
use super::{Traverse, TraverseFn};
1213

1314
pub(in crate::object) struct PyObjVTable {
15+
pub(in crate::object) typeid: TypeId,
1416
pub(in crate::object) drop_dealloc: unsafe fn(*mut PyObject),
1517
pub(in crate::object) debug: unsafe fn(&PyObject, &mut fmt::Formatter<'_>) -> fmt::Result,
1618
pub(in crate::object) trace: Option<unsafe fn(&PyObject, &mut TraverseFn<'_>)>,
@@ -19,6 +21,7 @@ pub(in crate::object) struct PyObjVTable {
1921
impl PyObjVTable {
2022
pub const fn of<T: PyObjectPayload>() -> &'static Self {
2123
&Self {
24+
typeid: T::PAYLOAD_TYPE_ID,
2225
drop_dealloc: drop_dealloc_obj::<T>,
2326
debug: debug_obj::<T>,
2427
trace: const {

0 commit comments

Comments
 (0)