Integrate PyTupleTyped into PyTuple by youknowone · Pull Request #5959 · RustPython/RustPython
vm/src/object/core.rs (1)
657-659: LGTM! Type identification standardization implemented correctly.
The change from TypeId::of::<T>() to T::payload_type_id() properly utilizes the new standardized method from the PyPayload trait. This centralizes type identity checks and aligns with the broader refactoring to unify payload type handling across the VM.
derive-impl/src/pytraverse.rs (2)
108-117: LGTM! Proper trait bounds added for generic traversal.
The automatic addition of Traverse bounds to all generic type parameters ensures that the derived Traverse implementation is valid for generic types. The use of split_for_impl() correctly handles the generics splitting for the macro-generated code.
120-120: Correctly applies generics and bounds to the implementation.
The generated unsafe impl block now properly includes the generics and where clause, ensuring type safety for the derived traversal implementations.
vm/src/object/payload.rs (2)
22-25: LGTM! Standardized type ID method implemented correctly.
The new payload_type_id() method provides a centralized and standardized way to obtain type identifiers for payload types. The inline annotation is appropriate for this frequently-used method.
82-82: Proper trait hierarchy established.
Expanding PyObjectPayload to require PyPayload ensures that all object payloads provide the standardized payload_type_id() method, creating a consistent trait hierarchy across the VM.
vm/src/builtins/type.rs (4)
65-65: LGTM! Reference-counted tuple field type updated correctly.
The change from Option<PyTupleTyped<PyStrRef>> to Option<PyRef<PyTupleTyped<PyStrRef>>> aligns with the broader refactoring to use reference-counted pointers for tuple elements, improving type safety and consistency.
1039-1039: Variable type correctly updated to match field type.
The heaptype_slots variable type is properly updated to Option<PyRef<PyTupleTyped<PyStrRef>>> to match the corresponding field type change.
1042-1046: Conversion logic updated for single string slots.
The PyRef::<PyTupleTyped<PyStrRef>>::try_from_object call correctly handles the conversion for single string slots with the new reference-counted tuple type.
1055-1058: Conversion logic updated for iterable slots.
The PyRef::<PyTupleTyped<PyStrRef>>::try_from_object call correctly handles the conversion for iterable slots with the new reference-counted tuple type.
vm/src/frame.rs (1)
1843-1843: Correctly updates closure type to align with PyRef pattern.
The change from PyTupleTyped<PyCellRef> to PyRef<PyTupleTyped<PyCellRef>> is consistent with the broader refactor described in the summary and properly maintains the reference-counted semantics for tuple elements.
vm/src/builtins/function.rs (4)
14-17: LGTM! Import reorganization aligns with module structure.
The relocation of TryFromObject and ToPyObject to the main import block is appropriate for their usage patterns.
35-35: Good architectural change to use reference-counted tuples.
The change from Option<PyTupleTyped<PyCellRef>> to Option<PyRef<PyTupleTyped<PyCellRef>>> aligns with Python's reference-counting semantics and improves memory efficiency by avoiding unnecessary tuple cloning.
61-61: Constructor signature correctly updated to match field type.
The closure parameter type change is consistent with the field declaration, and the usage at line 356 properly handles the reference semantics.
Also applies to: 356-356
616-617: Conversion correctly updated for PyRef-wrapped typed tuples.
The change to use PyRef::<PyTupleTyped<PyCellRef>>::try_from_object properly converts the closure tuple to the expected reference-counted typed tuple format.
vm/src/vm/mod.rs (4)
602-602: Good use of singleton empty tuple pattern.
Using PyTupleTyped::<PyStrRef>::empty(&self.ctx) leverages the singleton pattern for empty tuples, improving memory efficiency.
612-612: Efficient parameter passing with reference instead of owned value.
The change to accept &Py<PyTupleTyped<PyStrRef>> avoids unnecessary cloning and follows Rust's borrowing best practices.
622-622: Parameter type consistently updated in import_inner.
The reference parameter is properly used throughout the method, including the is_empty() check at line 627.
Also applies to: 627-627
660-660: Correct ownership conversion for function call.
The to_owned().to_pyobject(self) properly converts the borrowed reference to an owned PyObjectRef as required by the import function call.
vm/src/builtins/tuple.rs (5)
503-508: Struct refactoring improves type safety and ownership model.
The changes establish a clearer ownership model:
- Storing
PyTupledirectly instead ofPyTupleRefsimplifies the internal representation - The generic parameter
RwithTransmuteFromObjectbound ensures type safety for transmutation - The safety invariant comment properly documents the transmutation requirements
510-522: Well-designed PyPayload implementation for type identity.
The implementation correctly:
- Restricts PyPayload to
PyTupleTyped<PyRef<T>>ensuring type safety - Returns
PyTuple's type ID for proper type identity checks - Maintains compatibility with the Python type system
524-538: Correct garbage collection traversal implementation.
The Traverse and MaybeTraverse implementations properly handle GC traversal by:
- Delegating to the underlying tuple's traverse method
- Setting
IS_TRACE = trueto ensure typed tuples are always traced - Adding appropriate trait bounds for type safety
554-558: AsRef implementation correctly updated for PyRef elements.
The change to return &[PyRef<T>] aligns with the new type structure. The unsafe cast in as_slice() is justified by the transmutation safety requirements documented in the struct.
Also applies to: 577-577
597-602: Clean conversion trait implementation.
The From trait implementation provides a convenient and safe way to convert typed tuple references back to regular tuple references using the into_untyped() method.