@@ -3,7 +3,7 @@ use crate::common::{
33 hash:: { PyHash , PyUHash } ,
44 lock:: PyMutex ,
55} ;
6- use crate :: object:: { Traverse , TraverseFn } ;
6+ use crate :: object:: { MaybeTraverse , Traverse , TraverseFn } ;
77use crate :: {
88 AsObject , Context , Py , PyObject , PyObjectRef , PyPayload , PyRef , PyResult , TryFromObject ,
99 atomic_func,
@@ -449,6 +449,19 @@ impl Representable for PyTuple {
449449 }
450450}
451451
452+ impl PyRef < PyTuple > {
453+ pub fn try_into_typed < T : PyPayload > (
454+ self ,
455+ vm : & VirtualMachine ,
456+ ) -> PyResult < PyRef < PyTupleTyped < PyRef < T > > > > {
457+ PyRef :: < PyTupleTyped < PyRef < T > > > :: try_from_untyped ( self , vm)
458+ }
459+ unsafe fn into_typed_unchecked < T : PyPayload > ( self ) -> PyRef < PyTupleTyped < PyRef < T > > > {
460+ let obj: PyObjectRef = self . into ( ) ;
461+ unsafe { obj. downcast_unchecked :: < PyTupleTyped < PyRef < T > > > ( ) }
462+ }
463+ }
464+
452465#[ pyclass( module = false , name = "tuple_iterator" , traverse) ]
453466#[ derive( Debug ) ]
454467pub ( crate ) struct PyTupleIterator {
@@ -500,53 +513,76 @@ pub(crate) fn init(context: &Context) {
500513 PyTupleIterator :: extend_class ( context, context. types . tuple_iterator_type ) ;
501514}
502515
503- pub struct PyTupleTyped < T : TransmuteFromObject > {
516+ pub struct PyTupleTyped < R > {
504517 // SAFETY INVARIANT: T must be repr(transparent) over PyObjectRef, and the
505518 // elements must be logically valid when transmuted to T
506- tuple : PyTupleRef ,
507- _marker : PhantomData < Vec < T > > ,
519+ tuple : PyTuple ,
520+ _marker : PhantomData < R > ,
508521}
509522
510- unsafe impl < T > Traverse for PyTupleTyped < T >
523+ unsafe impl < R > Traverse for PyTupleTyped < R >
511524where
512- T : TransmuteFromObject + Traverse ,
525+ R : TransmuteFromObject ,
513526{
514527 fn traverse ( & self , tracer_fn : & mut TraverseFn < ' _ > ) {
515528 self . tuple . traverse ( tracer_fn) ;
516529 }
517530}
518531
519- impl < T : TransmuteFromObject > TryFromObject for PyTupleTyped < T > {
520- fn try_from_object ( vm : & VirtualMachine , obj : PyObjectRef ) -> PyResult < Self > {
521- let tuple = PyTupleRef :: try_from_object ( vm, obj) ?;
522- for elem in & * tuple {
523- T :: check ( vm, elem) ?
524- }
525- // SAFETY: the contract of TransmuteFromObject upholds the variant on `tuple`
526- Ok ( Self {
527- tuple,
528- _marker : PhantomData ,
529- } )
532+ impl < R : TransmuteFromObject + Traverse > MaybeTraverse for PyTupleTyped < R > {
533+ const IS_TRACE : bool = true ;
534+ fn try_traverse ( & self , tracer_fn : & mut TraverseFn < ' _ > ) {
535+ self . traverse ( tracer_fn) ;
530536 }
531537}
532538
533- impl < T : TransmuteFromObject > AsRef < [ T ] > for PyTupleTyped < T > {
534- fn as_ref ( & self ) -> & [ T ] {
535- self . as_slice ( )
539+ impl < T : PyPayload > PyTupleTyped < PyRef < T > > {
540+ pub fn new_ref ( elements : Vec < PyRef < T > > , ctx : & Context ) -> PyRef < Self > {
541+ unsafe {
542+ let elements: Vec < PyObjectRef > = std:: mem:: transmute ( elements) ;
543+ let tuple = PyTuple :: new_ref ( elements, ctx) ;
544+ tuple. into_typed_unchecked :: < T > ( )
545+ }
536546 }
537547}
538548
539- impl < T : TransmuteFromObject > PyTupleTyped < T > {
540- pub fn empty ( vm : & VirtualMachine ) -> Self {
541- Self {
542- tuple : vm. ctx . empty_tuple . clone ( ) ,
543- _marker : PhantomData ,
549+ impl < T : PyPayload > PyRef < PyTupleTyped < PyRef < T > > > {
550+ pub fn into_pyobject ( self ) -> PyObjectRef {
551+ self . into_untyped ( ) . into ( )
552+ }
553+
554+ pub fn into_untyped ( self ) -> PyRef < PyTuple > {
555+ // SAFETY: PyTupleTyped is transparent over PyTuple
556+ unsafe { std:: mem:: transmute ( self ) }
557+ }
558+
559+ pub fn try_from_untyped ( tuple : PyTupleRef , vm : & VirtualMachine ) -> PyResult < Self > {
560+ // Check that all elements are of the correct type
561+ for elem in tuple. as_slice ( ) {
562+ <PyRef < T > as TransmuteFromObject >:: check ( vm, elem) ?;
544563 }
564+ // SAFETY: We just verified all elements are of type T, and PyTupleTyped has the same layout as PyTuple
565+ Ok ( unsafe { std:: mem:: transmute ( tuple) } )
545566 }
567+ }
546568
569+ impl < T : PyPayload > Py < PyTupleTyped < PyRef < T > > > {
570+ pub fn as_untyped ( & self ) -> & Py < PyTuple > {
571+ // SAFETY: PyTupleTyped is transparent over PyTuple
572+ unsafe { std:: mem:: transmute ( self ) }
573+ }
574+ }
575+
576+ impl < T : PyPayload > AsRef < [ PyRef < T > ] > for PyTupleTyped < PyRef < T > > {
577+ fn as_ref ( & self ) -> & [ PyRef < T > ] {
578+ self . as_slice ( )
579+ }
580+ }
581+
582+ impl < T : PyPayload > PyTupleTyped < PyRef < T > > {
547583 #[ inline]
548- pub fn as_slice ( & self ) -> & [ T ] {
549- unsafe { & * ( self . tuple . as_slice ( ) as * const [ PyObjectRef ] as * const [ T ] ) }
584+ pub fn as_slice ( & self ) -> & [ PyRef < T > ] {
585+ unsafe { & * ( self . tuple . as_slice ( ) as * const [ PyObjectRef ] as * const [ PyRef < T > ] ) }
550586 }
551587
552588 #[ inline]
@@ -560,32 +596,16 @@ impl<T: TransmuteFromObject> PyTupleTyped<T> {
560596 }
561597}
562598
563- impl < T : TransmuteFromObject > Clone for PyTupleTyped < T > {
564- fn clone ( & self ) -> Self {
565- Self {
566- tuple : self . tuple . clone ( ) ,
567- _marker : PhantomData ,
568- }
569- }
570- }
571-
572- impl < T : TransmuteFromObject + fmt:: Debug > fmt:: Debug for PyTupleTyped < T > {
599+ impl < R : fmt:: Debug > fmt:: Debug for PyTupleTyped < R > {
573600 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
574- self . as_slice ( ) . fmt ( f)
575- }
576- }
577-
578- impl < T : TransmuteFromObject > From < PyTupleTyped < T > > for PyTupleRef {
579- #[ inline]
580- fn from ( tup : PyTupleTyped < T > ) -> Self {
581- tup. tuple
601+ self . tuple . as_slice ( ) . fmt ( f)
582602 }
583603}
584604
585- impl < T : TransmuteFromObject > ToPyObject for PyTupleTyped < T > {
605+ impl < T : PyPayload > From < PyRef < PyTupleTyped < PyRef < T > > > > for PyTupleRef {
586606 #[ inline]
587- fn to_pyobject ( self , _vm : & VirtualMachine ) -> PyObjectRef {
588- self . tuple . into ( )
607+ fn from ( tup : PyRef < PyTupleTyped < PyRef < T > > > ) -> Self {
608+ tup . into_untyped ( )
589609 }
590610}
591611
0 commit comments