Skip to content

Commit

Permalink
Simplify slice::Iter::next enough that it inlines
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm authored and gitbot committed Feb 22, 2025
1 parent 74bf98c commit d8f13b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::iter::{
use crate::marker::PhantomData;
use crate::mem::{self, SizedTypeProperties};
use crate::num::NonZero;
use crate::ptr::{NonNull, without_provenance, without_provenance_mut};
use crate::ptr::{NonNull, null, without_provenance, without_provenance_mut};
use crate::{cmp, fmt};

#[stable(feature = "boxed_slice_into_iter", since = "1.80.0")]
Expand Down
22 changes: 16 additions & 6 deletions core/src/slice/iter/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,26 @@ macro_rules! iterator {

#[inline]
fn next(&mut self) -> Option<$elem> {
// could be implemented with slices, but this avoids bounds checks
// intentionally not using the helpers because this is
// one of the most mono'd things in the library.

// SAFETY: The call to `next_unchecked` is
// safe since we check if the iterator is empty first.
let ptr = self.ptr;
let end_or_len = self.end_or_len;
// SAFETY: Type invariants.
unsafe {
if is_empty!(self) {
None
if T::IS_ZST {
let byte_end = end_or_len as *const u8;
if byte_end == null() {
return None;
}
self.end_or_len = byte_end.wrapping_sub(1) as _;
} else {
Some(self.next_unchecked())
if ptr == crate::intrinsics::transmute::<*const T, NonNull<T>>(end_or_len) {
return None;
}
self.ptr = ptr.add(1);
}
crate::intrinsics::transmute::<NonNull<T>, Option<$elem>>(ptr)
}
}

Expand Down

0 comments on commit d8f13b3

Please sign in to comment.