Skip to content

Commit 8dac6e3

Browse files
committed
use the same length computation everywhere
1 parent b0500c1 commit 8dac6e3

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

src/libcore/slice/mod.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -2349,20 +2349,9 @@ macro_rules! is_empty {
23492349
// and non-ZST.
23502350
($self: ident) => {$self.ptr == $self.end}
23512351
}
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`.)
23522354
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 {
23662355
($self: ident) => {{
23672356
let start = $self.ptr;
23682357
let diff = ($self.end as usize).wrapping_sub(start as usize);
@@ -2383,7 +2372,7 @@ macro_rules! iterator {
23832372
// Helper function for creating a slice from the iterator.
23842373
#[inline(always)]
23852374
fn make_slice(&self) -> &'a [T] {
2386-
unsafe { from_raw_parts(self.ptr, len2!(self)) }
2375+
unsafe { from_raw_parts(self.ptr, len!(self)) }
23872376
}
23882377

23892378
// Helper function for moving the start of the iterator forwards by `offset` elements,
@@ -2421,7 +2410,7 @@ macro_rules! iterator {
24212410
impl<'a, T> ExactSizeIterator for $name<'a, T> {
24222411
#[inline(always)]
24232412
fn len(&self) -> usize {
2424-
unsafe { len!(self) }
2413+
len!(self)
24252414
}
24262415

24272416
#[inline(always)]
@@ -2452,18 +2441,18 @@ macro_rules! iterator {
24522441

24532442
#[inline]
24542443
fn size_hint(&self) -> (usize, Option<usize>) {
2455-
let exact = unsafe { len!(self) };
2444+
let exact = len!(self);
24562445
(exact, Some(exact))
24572446
}
24582447

24592448
#[inline]
24602449
fn count(self) -> usize {
2461-
self.len()
2450+
len!(self)
24622451
}
24632452

24642453
#[inline]
24652454
fn nth(&mut self, n: usize) -> Option<$elem> {
2466-
if n >= unsafe { len!(self) } {
2455+
if n >= len!(self) {
24672456
// This iterator is now empty.
24682457
if mem::size_of::<T>() == 0 {
24692458
// We have to do it this way as `ptr` may never be 0, but `end`
@@ -2527,7 +2516,7 @@ macro_rules! iterator {
25272516
P: FnMut(Self::Item) -> bool,
25282517
{
25292518
// The addition might panic on overflow.
2530-
let n = len2!(self);
2519+
let n = len!(self);
25312520
self.try_fold(0, move |i, x| {
25322521
if predicate(x) { Err(i) }
25332522
else { Ok(i + 1) }
@@ -2544,7 +2533,7 @@ macro_rules! iterator {
25442533
Self: Sized + ExactSizeIterator + DoubleEndedIterator
25452534
{
25462535
// No need for an overflow check here, because `ExactSizeIterator`
2547-
let n = len2!(self);
2536+
let n = len!(self);
25482537
self.try_rfold(n, move |i, x| {
25492538
let i = i - 1;
25502539
if predicate(x) { Err(i) }
@@ -2789,7 +2778,7 @@ impl<'a, T> IterMut<'a, T> {
27892778
/// ```
27902779
#[stable(feature = "iter_to_slice", since = "1.4.0")]
27912780
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)) }
27932782
}
27942783
}
27952784

0 commit comments

Comments
 (0)