@@ -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,77 @@ 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 > =
543+ std:: mem:: transmute :: < Vec < PyRef < T > > , Vec < PyObjectRef > > ( elements) ;
544+ let tuple = PyTuple :: new_ref ( elements, ctx) ;
545+ tuple. into_typed_unchecked :: < T > ( )
546+ }
536547 }
537548}
538549
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 ,
550+ impl < T : PyPayload > PyRef < PyTupleTyped < PyRef < T > > > {
551+ pub fn into_pyobject ( self ) -> PyObjectRef {
552+ self . into_untyped ( ) . into ( )
553+ }
554+
555+ pub fn into_untyped ( self ) -> PyRef < PyTuple > {
556+ // SAFETY: PyTupleTyped is transparent over PyTuple
557+ unsafe { std:: mem:: transmute :: < PyRef < PyTupleTyped < PyRef < T > > > , PyRef < PyTuple > > ( self ) }
558+ }
559+
560+ pub fn try_from_untyped ( tuple : PyTupleRef , vm : & VirtualMachine ) -> PyResult < Self > {
561+ // Check that all elements are of the correct type
562+ for elem in tuple. as_slice ( ) {
563+ <PyRef < T > as TransmuteFromObject >:: check ( vm, elem) ?;
544564 }
565+ // SAFETY: We just verified all elements are of type T, and PyTupleTyped has the same layout as PyTuple
566+ Ok ( unsafe { std:: mem:: transmute :: < PyRef < PyTuple > , PyRef < PyTupleTyped < PyRef < T > > > > ( tuple) } )
545567 }
568+ }
546569
570+ impl < T : PyPayload > Py < PyTupleTyped < PyRef < T > > > {
571+ pub fn as_untyped ( & self ) -> & Py < PyTuple > {
572+ // SAFETY: PyTupleTyped is transparent over PyTuple
573+ unsafe { std:: mem:: transmute :: < & Py < PyTupleTyped < PyRef < T > > > , & Py < PyTuple > > ( self ) }
574+ }
575+ }
576+
577+ impl < T : PyPayload > AsRef < [ PyRef < T > ] > for PyTupleTyped < PyRef < T > > {
578+ fn as_ref ( & self ) -> & [ PyRef < T > ] {
579+ self . as_slice ( )
580+ }
581+ }
582+
583+ impl < T : PyPayload > PyTupleTyped < PyRef < T > > {
547584 #[ inline]
548- pub fn as_slice ( & self ) -> & [ T ] {
549- unsafe { & * ( self . tuple . as_slice ( ) as * const [ PyObjectRef ] as * const [ T ] ) }
585+ pub fn as_slice ( & self ) -> & [ PyRef < T > ] {
586+ unsafe { & * ( self . tuple . as_slice ( ) as * const [ PyObjectRef ] as * const [ PyRef < T > ] ) }
550587 }
551588
552589 #[ inline]
@@ -560,32 +597,16 @@ impl<T: TransmuteFromObject> PyTupleTyped<T> {
560597 }
561598}
562599
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 > {
600+ impl < R : fmt:: Debug > fmt:: Debug for PyTupleTyped < R > {
573601 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
602+ self . tuple . as_slice ( ) . fmt ( f)
582603 }
583604}
584605
585- impl < T : TransmuteFromObject > ToPyObject for PyTupleTyped < T > {
606+ impl < T : PyPayload > From < PyRef < PyTupleTyped < PyRef < T > > > > for PyTupleRef {
586607 #[ inline]
587- fn to_pyobject ( self , _vm : & VirtualMachine ) -> PyObjectRef {
588- self . tuple . into ( )
608+ fn from ( tup : PyRef < PyTupleTyped < PyRef < T > > > ) -> Self {
609+ tup . into_untyped ( )
589610 }
590611}
591612
0 commit comments