@@ -15,6 +15,7 @@ pub use self::chain::Chain;
15
15
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
16
16
pub use self :: flatten:: { FlatMap , Flatten } ;
17
17
pub use self :: fuse:: Fuse ;
18
+ use self :: zip:: try_get_unchecked;
18
19
pub ( crate ) use self :: zip:: TrustedRandomAccess ;
19
20
pub use self :: zip:: Zip ;
20
21
@@ -213,6 +214,15 @@ where
213
214
fn count ( self ) -> usize {
214
215
self . it . count ( )
215
216
}
217
+
218
+ unsafe fn get_unchecked ( & mut self , idx : usize ) -> T
219
+ where
220
+ Self : TrustedRandomAccess ,
221
+ {
222
+ // SAFETY: the caller must uphold the contract for
223
+ // `Iterator::get_unchecked`.
224
+ * unsafe { try_get_unchecked ( & mut self . it , idx) }
225
+ }
216
226
}
217
227
218
228
#[ stable( feature = "iter_copied" , since = "1.36.0" ) ]
@@ -266,16 +276,11 @@ where
266
276
}
267
277
268
278
#[ doc( hidden) ]
269
- unsafe impl < ' a , I , T : ' a > TrustedRandomAccess for Copied < I >
279
+ #[ unstable( feature = "trusted_random_access" , issue = "none" ) ]
280
+ unsafe impl < I > TrustedRandomAccess for Copied < I >
270
281
where
271
- I : TrustedRandomAccess < Item = & ' a T > ,
272
- T : Copy ,
282
+ I : TrustedRandomAccess ,
273
283
{
274
- unsafe fn get_unchecked ( & mut self , i : usize ) -> Self :: Item {
275
- // SAFETY: the caller must uphold the contract for `TrustedRandomAccess::get_unchecked`.
276
- unsafe { * self . it . get_unchecked ( i) }
277
- }
278
-
279
284
#[ inline]
280
285
fn may_have_side_effect ( ) -> bool {
281
286
I :: may_have_side_effect ( )
@@ -344,6 +349,15 @@ where
344
349
{
345
350
self . it . map ( T :: clone) . fold ( init, f)
346
351
}
352
+
353
+ unsafe fn get_unchecked ( & mut self , idx : usize ) -> T
354
+ where
355
+ Self : TrustedRandomAccess ,
356
+ {
357
+ // SAFETY: the caller must uphold the contract for
358
+ // `Iterator::get_unchecked`.
359
+ unsafe { try_get_unchecked ( & mut self . it , idx) . clone ( ) }
360
+ }
347
361
}
348
362
349
363
#[ stable( feature = "iter_cloned" , since = "1.1.0" ) ]
@@ -397,36 +411,14 @@ where
397
411
}
398
412
399
413
#[ doc( hidden) ]
400
- unsafe impl < ' a , I , T : ' a > TrustedRandomAccess for Cloned < I >
414
+ #[ unstable( feature = "trusted_random_access" , issue = "none" ) ]
415
+ unsafe impl < I > TrustedRandomAccess for Cloned < I >
401
416
where
402
- I : TrustedRandomAccess < Item = & ' a T > ,
403
- T : Clone ,
404
- {
405
- default unsafe fn get_unchecked ( & mut self , i : usize ) -> Self :: Item {
406
- // SAFETY: the caller must uphold the contract for `TrustedRandomAccess::get_unchecked`.
407
- unsafe { self . it . get_unchecked ( i) } . clone ( )
408
- }
409
-
410
- #[ inline]
411
- default fn may_have_side_effect ( ) -> bool {
412
- true
413
- }
414
- }
415
-
416
- #[ doc( hidden) ]
417
- unsafe impl < ' a , I , T : ' a > TrustedRandomAccess for Cloned < I >
418
- where
419
- I : TrustedRandomAccess < Item = & ' a T > ,
420
- T : Copy ,
417
+ I : TrustedRandomAccess ,
421
418
{
422
- unsafe fn get_unchecked ( & mut self , i : usize ) -> Self :: Item {
423
- // SAFETY: the caller must uphold the contract for `TrustedRandomAccess::get_unchecked`.
424
- unsafe { * self . it . get_unchecked ( i) }
425
- }
426
-
427
419
#[ inline]
428
420
fn may_have_side_effect ( ) -> bool {
429
- I :: may_have_side_effect ( )
421
+ true
430
422
}
431
423
}
432
424
@@ -872,6 +864,15 @@ where
872
864
{
873
865
self . iter . fold ( init, map_fold ( self . f , g) )
874
866
}
867
+
868
+ unsafe fn get_unchecked ( & mut self , idx : usize ) -> B
869
+ where
870
+ Self : TrustedRandomAccess ,
871
+ {
872
+ // SAFETY: the caller must uphold the contract for
873
+ // `Iterator::get_unchecked`.
874
+ unsafe { ( self . f ) ( try_get_unchecked ( & mut self . iter , idx) ) }
875
+ }
875
876
}
876
877
877
878
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -927,15 +928,11 @@ where
927
928
}
928
929
929
930
#[ doc( hidden) ]
930
- unsafe impl < B , I , F > TrustedRandomAccess for Map < I , F >
931
+ #[ unstable( feature = "trusted_random_access" , issue = "none" ) ]
932
+ unsafe impl < I , F > TrustedRandomAccess for Map < I , F >
931
933
where
932
934
I : TrustedRandomAccess ,
933
- F : FnMut ( I :: Item ) -> B ,
934
935
{
935
- unsafe fn get_unchecked ( & mut self , i : usize ) -> Self :: Item {
936
- // SAFETY: the caller must uphold the contract for `TrustedRandomAccess::get_unchecked`.
937
- ( self . f ) ( unsafe { self . iter . get_unchecked ( i) } )
938
- }
939
936
#[ inline]
940
937
fn may_have_side_effect ( ) -> bool {
941
938
true
@@ -1306,6 +1303,16 @@ where
1306
1303
1307
1304
self . iter . fold ( init, enumerate ( self . count , fold) )
1308
1305
}
1306
+
1307
+ unsafe fn get_unchecked ( & mut self , idx : usize ) -> <Self as Iterator >:: Item
1308
+ where
1309
+ Self : TrustedRandomAccess ,
1310
+ {
1311
+ // SAFETY: the caller must uphold the contract for
1312
+ // `Iterator::get_unchecked`.
1313
+ let value = unsafe { try_get_unchecked ( & mut self . iter , idx) } ;
1314
+ ( Add :: add ( self . count , idx) , value)
1315
+ }
1309
1316
}
1310
1317
1311
1318
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1391,15 +1398,11 @@ where
1391
1398
}
1392
1399
1393
1400
#[ doc( hidden) ]
1401
+ #[ unstable( feature = "trusted_random_access" , issue = "none" ) ]
1394
1402
unsafe impl < I > TrustedRandomAccess for Enumerate < I >
1395
1403
where
1396
1404
I : TrustedRandomAccess ,
1397
1405
{
1398
- unsafe fn get_unchecked ( & mut self , i : usize ) -> ( usize , I :: Item ) {
1399
- // SAFETY: the caller must uphold the contract for `TrustedRandomAccess::get_unchecked`.
1400
- ( self . count + i, unsafe { self . iter . get_unchecked ( i) } )
1401
- }
1402
-
1403
1406
fn may_have_side_effect ( ) -> bool {
1404
1407
I :: may_have_side_effect ( )
1405
1408
}
0 commit comments