@@ -2349,20 +2349,9 @@ macro_rules! is_empty {
2349
2349
// and non-ZST.
2350
2350
( $self: ident) => { $self. ptr == $self. end}
2351
2351
}
2352
+ // To get rid of some bounds checks (see `position`), we compute the length in a somewhat
2353
+ // unexpected way. (Tested by `codegen/slice-position-bounds-check`.)
2352
2354
macro_rules! len {
2353
- ( $self: ident) => { {
2354
- let start = $self. ptr;
2355
- if size_from_ptr( start) == 0 {
2356
- ( $self. end as usize ) . wrapping_sub( start as usize )
2357
- } else {
2358
- $self. end. offset_from( start) as usize
2359
- }
2360
- } }
2361
- }
2362
- // To get rid of some bounds checks (see `position`), for some reason it
2363
- // makes a difference to compute the length in this way.
2364
- // (Tested by `codegen/slice-position-bounds-check`.)
2365
- macro_rules! len2 {
2366
2355
( $self: ident) => { {
2367
2356
let start = $self. ptr;
2368
2357
let diff = ( $self. end as usize ) . wrapping_sub( start as usize ) ;
@@ -2383,7 +2372,7 @@ macro_rules! iterator {
2383
2372
// Helper function for creating a slice from the iterator.
2384
2373
#[ inline( always) ]
2385
2374
fn make_slice( & self ) -> & ' a [ T ] {
2386
- unsafe { from_raw_parts( self . ptr, len2 !( self ) ) }
2375
+ unsafe { from_raw_parts( self . ptr, len !( self ) ) }
2387
2376
}
2388
2377
2389
2378
// Helper function for moving the start of the iterator forwards by `offset` elements,
@@ -2421,7 +2410,7 @@ macro_rules! iterator {
2421
2410
impl <' a, T > ExactSizeIterator for $name<' a, T > {
2422
2411
#[ inline( always) ]
2423
2412
fn len( & self ) -> usize {
2424
- unsafe { len!( self ) }
2413
+ len!( self )
2425
2414
}
2426
2415
2427
2416
#[ inline( always) ]
@@ -2452,18 +2441,18 @@ macro_rules! iterator {
2452
2441
2453
2442
#[ inline]
2454
2443
fn size_hint( & self ) -> ( usize , Option <usize >) {
2455
- let exact = unsafe { len!( self ) } ;
2444
+ let exact = len!( self ) ;
2456
2445
( exact, Some ( exact) )
2457
2446
}
2458
2447
2459
2448
#[ inline]
2460
2449
fn count( self ) -> usize {
2461
- self . len( )
2450
+ len! ( self )
2462
2451
}
2463
2452
2464
2453
#[ inline]
2465
2454
fn nth( & mut self , n: usize ) -> Option <$elem> {
2466
- if n >= unsafe { len!( self ) } {
2455
+ if n >= len!( self ) {
2467
2456
// This iterator is now empty.
2468
2457
if mem:: size_of:: <T >( ) == 0 {
2469
2458
// We have to do it this way as `ptr` may never be 0, but `end`
@@ -2527,7 +2516,7 @@ macro_rules! iterator {
2527
2516
P : FnMut ( Self :: Item ) -> bool ,
2528
2517
{
2529
2518
// The addition might panic on overflow.
2530
- let n = len2 !( self ) ;
2519
+ let n = len !( self ) ;
2531
2520
self . try_fold( 0 , move |i, x| {
2532
2521
if predicate( x) { Err ( i) }
2533
2522
else { Ok ( i + 1 ) }
@@ -2544,7 +2533,7 @@ macro_rules! iterator {
2544
2533
Self : Sized + ExactSizeIterator + DoubleEndedIterator
2545
2534
{
2546
2535
// No need for an overflow check here, because `ExactSizeIterator`
2547
- let n = len2 !( self ) ;
2536
+ let n = len !( self ) ;
2548
2537
self . try_rfold( n, move |i, x| {
2549
2538
let i = i - 1 ;
2550
2539
if predicate( x) { Err ( i) }
@@ -2789,7 +2778,7 @@ impl<'a, T> IterMut<'a, T> {
2789
2778
/// ```
2790
2779
#[ stable( feature = "iter_to_slice" , since = "1.4.0" ) ]
2791
2780
pub fn into_slice ( self ) -> & ' a mut [ T ] {
2792
- unsafe { from_raw_parts_mut ( self . ptr , len2 ! ( self ) ) }
2781
+ unsafe { from_raw_parts_mut ( self . ptr , len ! ( self ) ) }
2793
2782
}
2794
2783
}
2795
2784
0 commit comments