Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ members = [
version = "0.4.0"
authors = ["RustPython Team"]
edition = "2024"
rust-version = "1.89.0"
rust-version = "1.91.0"
repository = "https://github.com/RustPython/RustPython"
license = "MIT"

Expand Down
5 changes: 1 addition & 4 deletions crates/derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,7 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result<Tok
quote! {
// static_assertions::const_assert!(std::mem::size_of::<#base_type>() <= std::mem::size_of::<#ident>());
impl ::rustpython_vm::PyPayload for #ident {
#[inline]
fn payload_type_id() -> ::std::any::TypeId {
<#base_type as ::rustpython_vm::PyPayload>::payload_type_id()
}
const PAYLOAD_TYPE_ID: ::core::any::TypeId = <#base_type as ::rustpython_vm::PyPayload>::PAYLOAD_TYPE_ID;

#[inline]
fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {
Expand Down
5 changes: 1 addition & 4 deletions crates/derive-impl/src/pystructseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,7 @@ pub(crate) fn impl_pystruct_sequence(

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

#[inline]
fn validate_downcastable_from(obj: &::rustpython_vm::PyObject) -> bool {
Expand Down
4 changes: 1 addition & 3 deletions crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1935,9 +1935,7 @@ impl PyPayload for PyUtf8Str {
ctx.types.str_type
}

fn payload_type_id() -> core::any::TypeId {
core::any::TypeId::of::<PyStr>()
}
const PAYLOAD_TYPE_ID: core::any::TypeId = core::any::TypeId::of::<PyStr>();

fn validate_downcastable_from(obj: &PyObject) -> bool {
// SAFETY: we know the object is a PyStr in this context
Expand Down
9 changes: 2 additions & 7 deletions crates/vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ pub(super) unsafe fn try_trace_obj<T: PyPayload>(x: &PyObject, tracer_fn: &mut T
#[repr(C)]
pub(super) struct PyInner<T> {
pub(super) ref_count: RefCount,
// TODO: move typeid into vtable once TypeId::of is const
pub(super) typeid: TypeId,
pub(super) vtable: &'static PyObjVTable,

pub(super) typ: PyAtomicRef<PyType>, // __class__ member
Expand Down Expand Up @@ -449,7 +447,6 @@ impl<T: PyPayload + core::fmt::Debug> PyInner<T> {
let member_count = typ.slots.member_count;
Box::new(Self {
ref_count: RefCount::new(),
typeid: T::payload_type_id(),
vtable: PyObjVTable::of::<T>(),
typ: PyAtomicRef::from(typ),
dict: dict.map(InstanceDict::new),
Expand Down Expand Up @@ -639,7 +636,7 @@ impl PyObject {
#[deprecated(note = "use downcastable instead")]
#[inline(always)]
pub fn payload_is<T: PyPayload>(&self) -> bool {
self.0.typeid == T::payload_type_id()
self.0.vtable.typeid == T::PAYLOAD_TYPE_ID
}

/// Force to return payload as T.
Expand Down Expand Up @@ -722,7 +719,7 @@ impl PyObject {

#[inline]
pub(crate) fn typeid(&self) -> TypeId {
self.0.typeid
self.0.vtable.typeid
}

/// Check if this object can be downcast to T.
Expand Down Expand Up @@ -1276,7 +1273,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
let type_type_ptr = Box::into_raw(Box::new(partially_init!(
PyInner::<PyType> {
ref_count: RefCount::new(),
typeid: TypeId::of::<PyType>(),
vtable: PyObjVTable::of::<PyType>(),
dict: None,
weak_list: WeakRefList::new(),
Expand All @@ -1288,7 +1284,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
let object_type_ptr = Box::into_raw(Box::new(partially_init!(
PyInner::<PyType> {
ref_count: RefCount::new(),
typeid: TypeId::of::<PyType>(),
vtable: PyObjVTable::of::<PyType>(),
dict: None,
weak_list: WeakRefList::new(),
Expand Down
7 changes: 2 additions & 5 deletions crates/vm/src/object/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ pub(crate) fn cold_downcast_type_error(
}

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

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

#[inline]
Expand Down
3 changes: 3 additions & 0 deletions crates/vm/src/object/traverse_object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::fmt;
use core::any::TypeId;

use crate::{
PyObject,
Expand All @@ -11,6 +12,7 @@ use crate::{
use super::{Traverse, TraverseFn};

pub(in crate::object) struct PyObjVTable {
pub(in crate::object) typeid: TypeId,
pub(in crate::object) drop_dealloc: unsafe fn(*mut PyObject),
pub(in crate::object) debug: unsafe fn(&PyObject, &mut fmt::Formatter<'_>) -> fmt::Result,
pub(in crate::object) trace: Option<unsafe fn(&PyObject, &mut TraverseFn<'_>)>,
Expand All @@ -19,6 +21,7 @@ pub(in crate::object) struct PyObjVTable {
impl PyObjVTable {
pub const fn of<T: PyObjectPayload>() -> &'static Self {
&Self {
typeid: T::PAYLOAD_TYPE_ID,
drop_dealloc: drop_dealloc_obj::<T>,
debug: debug_obj::<T>,
trace: const {
Expand Down