Skip to content

Commit 09bde28

Browse files
authored
Move typeid into vtable (#6231)
* Move typeid into vtable * Bump rust-version
1 parent 42cc59a commit 09bde28

File tree

7 files changed

+11
-24
lines changed

7 files changed

+11
-24
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ members = [
131131
version = "0.4.0"
132132
authors = ["RustPython Team"]
133133
edition = "2024"
134-
rust-version = "1.89.0"
134+
rust-version = "1.91.0"
135135
repository = "https://github.com/RustPython/RustPython"
136136
license = "MIT"
137137

crates/derive-impl/src/pyclass.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,7 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result<Tok
635635
quote! {
636636
// static_assertions::const_assert!(std::mem::size_of::<#base_type>() <= std::mem::size_of::<#ident>());
637637
impl ::rustpython_vm::PyPayload for #ident {
638-
#[inline]
639-
fn payload_type_id() -> ::std::any::TypeId {
640-
<#base_type as ::rustpython_vm::PyPayload>::payload_type_id()
641-
}
638+
const PAYLOAD_TYPE_ID: ::core::any::TypeId = <#base_type as ::rustpython_vm::PyPayload>::PAYLOAD_TYPE_ID;
642639

643640
#[inline]
644641
fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {

crates/derive-impl/src/pystructseq.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,7 @@ pub(crate) fn impl_pystruct_sequence(
592592

593593
// Subtype uses base type's payload_type_id
594594
impl ::rustpython_vm::PyPayload for #pytype_ident {
595-
#[inline]
596-
fn payload_type_id() -> ::std::any::TypeId {
597-
<::rustpython_vm::builtins::PyTuple as ::rustpython_vm::PyPayload>::payload_type_id()
598-
}
595+
const PAYLOAD_TYPE_ID: ::core::any::TypeId = <::rustpython_vm::builtins::PyTuple as ::rustpython_vm::PyPayload>::PAYLOAD_TYPE_ID;
599596

600597
#[inline]
601598
fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {

crates/vm/src/builtins/str.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,9 +1935,7 @@ impl PyPayload for PyUtf8Str {
19351935
ctx.types.str_type
19361936
}
19371937

1938-
fn payload_type_id() -> core::any::TypeId {
1939-
core::any::TypeId::of::<PyStr>()
1940-
}
1938+
const PAYLOAD_TYPE_ID: core::any::TypeId = core::any::TypeId::of::<PyStr>();
19411939

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

crates/vm/src/object/core.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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),
@@ -639,7 +636,7 @@ impl PyObject {
639636
#[deprecated(note = "use downcastable instead")]
640637
#[inline(always)]
641638
pub fn payload_is<T: PyPayload>(&self) -> bool {
642-
self.0.typeid == T::payload_type_id()
639+
self.0.vtable.typeid == T::PAYLOAD_TYPE_ID
643640
}
644641

645642
/// Force to return payload as T.
@@ -722,7 +719,7 @@ impl PyObject {
722719

723720
#[inline]
724721
pub(crate) fn typeid(&self) -> TypeId {
725-
self.0.typeid
722+
self.0.vtable.typeid
726723
}
727724

728725
/// Check if this object can be downcast to T.
@@ -1276,7 +1273,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
12761273
let type_type_ptr = Box::into_raw(Box::new(partially_init!(
12771274
PyInner::<PyType> {
12781275
ref_count: RefCount::new(),
1279-
typeid: TypeId::of::<PyType>(),
12801276
vtable: PyObjVTable::of::<PyType>(),
12811277
dict: None,
12821278
weak_list: WeakRefList::new(),
@@ -1288,7 +1284,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
12881284
let object_type_ptr = Box::into_raw(Box::new(partially_init!(
12891285
PyInner::<PyType> {
12901286
ref_count: RefCount::new(),
1291-
typeid: TypeId::of::<PyType>(),
12921287
vtable: PyObjVTable::of::<PyType>(),
12931288
dict: None,
12941289
weak_list: WeakRefList::new(),

crates/vm/src/object/payload.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@ pub(crate) fn cold_downcast_type_error(
2626
}
2727

2828
pub trait PyPayload: MaybeTraverse + PyThreadingConstraint + Sized + 'static {
29-
#[inline]
30-
fn payload_type_id() -> core::any::TypeId {
31-
core::any::TypeId::of::<Self>()
32-
}
29+
const PAYLOAD_TYPE_ID: core::any::TypeId = core::any::TypeId::of::<Self>();
3330

3431
/// # Safety: this function should only be called if `payload_type_id` matches the type of `obj`.
3532
#[inline]
3633
fn downcastable_from(obj: &PyObject) -> bool {
37-
obj.typeid() == Self::payload_type_id() && Self::validate_downcastable_from(obj)
34+
obj.typeid() == Self::PAYLOAD_TYPE_ID && Self::validate_downcastable_from(obj)
3835
}
3936

4037
#[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 core::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)