@@ -3,7 +3,6 @@ use crate::common::{
3
3
hash:: { PyHash , PyUHash } ,
4
4
lock:: PyMutex ,
5
5
} ;
6
- use crate :: object:: { Traverse , TraverseFn } ;
7
6
use crate :: {
8
7
AsObject , Context , Py , PyObject , PyObjectRef , PyPayload , PyRef , PyResult , TryFromObject ,
9
8
atomic_func,
@@ -22,14 +21,14 @@ use crate::{
22
21
utils:: collection_repr,
23
22
vm:: VirtualMachine ,
24
23
} ;
25
- use std:: { fmt, marker :: PhantomData , sync:: LazyLock } ;
24
+ use std:: { fmt, sync:: LazyLock } ;
26
25
27
26
#[ pyclass( module = false , name = "tuple" , traverse) ]
28
- pub struct PyTuple {
29
- elements : Box < [ PyObjectRef ] > ,
27
+ pub struct PyTuple < R = PyObjectRef > {
28
+ elements : Box < [ R ] > ,
30
29
}
31
30
32
- impl fmt:: Debug for PyTuple {
31
+ impl < R > fmt:: Debug for PyTuple < R > {
33
32
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
34
33
// TODO: implement more informational, non-recursive Debug formatter
35
34
f. write_str ( "tuple" )
@@ -140,39 +139,60 @@ impl Constructor for PyTuple {
140
139
}
141
140
}
142
141
143
- impl AsRef < [ PyObjectRef ] > for PyTuple {
144
- fn as_ref ( & self ) -> & [ PyObjectRef ] {
145
- self . as_slice ( )
142
+ impl < R > AsRef < [ R ] > for PyTuple < R > {
143
+ fn as_ref ( & self ) -> & [ R ] {
144
+ & self . elements
146
145
}
147
146
}
148
147
149
- impl std:: ops:: Deref for PyTuple {
150
- type Target = [ PyObjectRef ] ;
148
+ impl < R > std:: ops:: Deref for PyTuple < R > {
149
+ type Target = [ R ] ;
151
150
152
- fn deref ( & self ) -> & [ PyObjectRef ] {
153
- self . as_slice ( )
151
+ fn deref ( & self ) -> & [ R ] {
152
+ & self . elements
154
153
}
155
154
}
156
155
157
- impl < ' a > std:: iter:: IntoIterator for & ' a PyTuple {
158
- type Item = & ' a PyObjectRef ;
159
- type IntoIter = std:: slice:: Iter < ' a , PyObjectRef > ;
156
+ impl < ' a , R > std:: iter:: IntoIterator for & ' a PyTuple < R > {
157
+ type Item = & ' a R ;
158
+ type IntoIter = std:: slice:: Iter < ' a , R > ;
160
159
161
160
fn into_iter ( self ) -> Self :: IntoIter {
162
161
self . iter ( )
163
162
}
164
163
}
165
164
166
- impl < ' a > std:: iter:: IntoIterator for & ' a Py < PyTuple > {
167
- type Item = & ' a PyObjectRef ;
168
- type IntoIter = std:: slice:: Iter < ' a , PyObjectRef > ;
165
+ impl < ' a , R > std:: iter:: IntoIterator for & ' a Py < PyTuple < R > > {
166
+ type Item = & ' a R ;
167
+ type IntoIter = std:: slice:: Iter < ' a , R > ;
169
168
170
169
fn into_iter ( self ) -> Self :: IntoIter {
171
170
self . iter ( )
172
171
}
173
172
}
174
173
175
- impl PyTuple {
174
+ impl < R > PyTuple < R > {
175
+ pub const fn as_slice ( & self ) -> & [ R ] {
176
+ & self . elements
177
+ }
178
+
179
+ #[ inline]
180
+ pub fn len ( & self ) -> usize {
181
+ self . elements . len ( )
182
+ }
183
+
184
+ #[ inline]
185
+ pub fn is_empty ( & self ) -> bool {
186
+ self . elements . is_empty ( )
187
+ }
188
+
189
+ #[ inline]
190
+ pub fn iter ( & self ) -> std:: slice:: Iter < ' _ , R > {
191
+ self . elements . iter ( )
192
+ }
193
+ }
194
+
195
+ impl PyTuple < PyObjectRef > {
176
196
pub fn new_ref ( elements : Vec < PyObjectRef > , ctx : & Context ) -> PyRef < Self > {
177
197
if elements. is_empty ( ) {
178
198
ctx. empty_tuple . clone ( )
@@ -189,10 +209,6 @@ impl PyTuple {
189
209
Self { elements }
190
210
}
191
211
192
- pub const fn as_slice ( & self ) -> & [ PyObjectRef ] {
193
- & self . elements
194
- }
195
-
196
212
fn repeat ( zelf : PyRef < Self > , value : isize , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
197
213
Ok ( if zelf. elements . is_empty ( ) || value == 0 {
198
214
vm. ctx . empty_tuple . clone ( )
@@ -214,6 +230,18 @@ impl PyTuple {
214
230
}
215
231
}
216
232
233
+ impl < T > PyTuple < PyRef < T > > {
234
+ pub fn new_ref_typed ( elements : Vec < PyRef < T > > , ctx : & Context ) -> PyRef < PyTuple < PyRef < T > > > {
235
+ // SAFETY: PyRef<T> has the same layout as PyObjectRef
236
+ unsafe {
237
+ let elements: Vec < PyObjectRef > =
238
+ std:: mem:: transmute :: < Vec < PyRef < T > > , Vec < PyObjectRef > > ( elements) ;
239
+ let tuple = PyTuple :: < PyObjectRef > :: new_ref ( elements, ctx) ;
240
+ std:: mem:: transmute :: < PyRef < PyTuple > , PyRef < PyTuple < PyRef < T > > > > ( tuple)
241
+ }
242
+ }
243
+ }
244
+
217
245
#[ pyclass(
218
246
flags( BASETYPE ) ,
219
247
with(
@@ -272,11 +300,6 @@ impl PyTuple {
272
300
self . elements . len ( )
273
301
}
274
302
275
- #[ inline]
276
- pub const fn is_empty ( & self ) -> bool {
277
- self . elements . is_empty ( )
278
- }
279
-
280
303
#[ pymethod( name = "__rmul__" ) ]
281
304
#[ pymethod]
282
305
fn __mul__ ( zelf : PyRef < Self > , value : ArgSize , vm : & VirtualMachine ) -> PyResult < PyRef < Self > > {
@@ -449,6 +472,41 @@ impl Representable for PyTuple {
449
472
}
450
473
}
451
474
475
+ impl PyRef < PyTuple < PyObjectRef > > {
476
+ pub fn try_into_typed < T : PyPayload > (
477
+ self ,
478
+ vm : & VirtualMachine ,
479
+ ) -> PyResult < PyRef < PyTuple < PyRef < T > > > > {
480
+ // Check that all elements are of the correct type
481
+ for elem in self . as_slice ( ) {
482
+ <PyRef < T > as TransmuteFromObject >:: check ( vm, elem) ?;
483
+ }
484
+ // SAFETY: We just verified all elements are of type T
485
+ Ok ( unsafe { std:: mem:: transmute :: < PyRef < PyTuple > , PyRef < PyTuple < PyRef < T > > > > ( self ) } )
486
+ }
487
+ }
488
+
489
+ impl < T : PyPayload > PyRef < PyTuple < PyRef < T > > > {
490
+ pub fn into_untyped ( self ) -> PyRef < PyTuple > {
491
+ // SAFETY: PyTuple<PyRef<T>> has the same layout as PyTuple
492
+ unsafe { std:: mem:: transmute :: < PyRef < PyTuple < PyRef < T > > > , PyRef < PyTuple > > ( self ) }
493
+ }
494
+ }
495
+
496
+ impl < T : PyPayload > Py < PyTuple < PyRef < T > > > {
497
+ pub fn as_untyped ( & self ) -> & Py < PyTuple > {
498
+ // SAFETY: PyTuple<PyRef<T>> has the same layout as PyTuple
499
+ unsafe { std:: mem:: transmute :: < & Py < PyTuple < PyRef < T > > > , & Py < PyTuple > > ( self ) }
500
+ }
501
+ }
502
+
503
+ impl < T : PyPayload > From < PyRef < PyTuple < PyRef < T > > > > for PyTupleRef {
504
+ #[ inline]
505
+ fn from ( tup : PyRef < PyTuple < PyRef < T > > > ) -> Self {
506
+ tup. into_untyped ( )
507
+ }
508
+ }
509
+
452
510
#[ pyclass( module = false , name = "tuple_iterator" , traverse) ]
453
511
#[ derive( Debug ) ]
454
512
pub ( crate ) struct PyTupleIterator {
@@ -500,95 +558,6 @@ pub(crate) fn init(context: &Context) {
500
558
PyTupleIterator :: extend_class ( context, context. types . tuple_iterator_type ) ;
501
559
}
502
560
503
- pub struct PyTupleTyped < T : TransmuteFromObject > {
504
- // SAFETY INVARIANT: T must be repr(transparent) over PyObjectRef, and the
505
- // elements must be logically valid when transmuted to T
506
- tuple : PyTupleRef ,
507
- _marker : PhantomData < Vec < T > > ,
508
- }
509
-
510
- unsafe impl < T > Traverse for PyTupleTyped < T >
511
- where
512
- T : TransmuteFromObject + Traverse ,
513
- {
514
- fn traverse ( & self , tracer_fn : & mut TraverseFn < ' _ > ) {
515
- self . tuple . traverse ( tracer_fn) ;
516
- }
517
- }
518
-
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
- } )
530
- }
531
- }
532
-
533
- impl < T : TransmuteFromObject > AsRef < [ T ] > for PyTupleTyped < T > {
534
- fn as_ref ( & self ) -> & [ T ] {
535
- self . as_slice ( )
536
- }
537
- }
538
-
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 ,
544
- }
545
- }
546
-
547
- #[ inline]
548
- pub fn as_slice ( & self ) -> & [ T ] {
549
- unsafe { & * ( self . tuple . as_slice ( ) as * const [ PyObjectRef ] as * const [ T ] ) }
550
- }
551
-
552
- #[ inline]
553
- pub fn len ( & self ) -> usize {
554
- self . tuple . len ( )
555
- }
556
-
557
- #[ inline]
558
- pub fn is_empty ( & self ) -> bool {
559
- self . tuple . is_empty ( )
560
- }
561
- }
562
-
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 > {
573
- 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
582
- }
583
- }
584
-
585
- impl < T : TransmuteFromObject > ToPyObject for PyTupleTyped < T > {
586
- #[ inline]
587
- fn to_pyobject ( self , _vm : & VirtualMachine ) -> PyObjectRef {
588
- self . tuple . into ( )
589
- }
590
- }
591
-
592
561
pub ( super ) fn tuple_hash ( elements : & [ PyObjectRef ] , vm : & VirtualMachine ) -> PyResult < PyHash > {
593
562
#[ cfg( target_pointer_width = "64" ) ]
594
563
const PRIME1 : PyUHash = 11400714785074694791 ;
0 commit comments