Skip to content

Commit

Permalink
Mark the StepBy specialization as unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
the8472 committed Jun 25, 2023
1 parent 8a72f35 commit f174547
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions library/core/src/iter/adapters/step_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ impl<T> SpecRangeSetup<T> for T {

/// Specialization trait to optimize `StepBy<Range<{integer}>>` iteration.
///
/// # Correctness
/// # Safety
///
/// Technically this is safe to implement (look ma, no unsafe!), but in reality
/// a lot of unsafe code relies on ranges over integers being correct.
///
/// For correctness *all* public StepBy methods must be specialized
/// because `setup` drastically alters the meaning of the struct fields so that mixing
/// different implementations would lead to incorrect results.
trait StepByImpl<I> {
unsafe trait StepByImpl<I> {
type Item;

fn spec_next(&mut self) -> Option<Self::Item>;
Expand All @@ -172,13 +172,13 @@ trait StepByImpl<I> {
///
/// See also: `StepByImpl`
///
/// # Correctness
/// # Safety
///
/// The specializations must be implemented together with `StepByImpl`
/// where applicable. I.e. if `StepBy` does support backwards iteration
/// for a given iterator and that is specialized for forward iteration then
/// it must also be specialized for backwards iteration.
trait StepByBackImpl<I> {
unsafe trait StepByBackImpl<I> {
type Item;

fn spec_next_back(&mut self) -> Option<Self::Item>
Expand All @@ -201,7 +201,7 @@ trait StepByBackImpl<I> {
F: FnMut(Acc, Self::Item) -> Acc;
}

impl<I: Iterator> StepByImpl<I> for StepBy<I> {
unsafe impl<I: Iterator> StepByImpl<I> for StepBy<I> {
type Item = I::Item;

#[inline]
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<I: Iterator> StepByImpl<I> for StepBy<I> {
}
}

impl<I: DoubleEndedIterator + ExactSizeIterator> StepByBackImpl<I> for StepBy<I> {
unsafe impl<I: DoubleEndedIterator + ExactSizeIterator> StepByBackImpl<I> for StepBy<I> {
type Item = I::Item;

#[inline]
Expand Down Expand Up @@ -416,7 +416,7 @@ macro_rules! spec_int_ranges {
}
}

impl StepByImpl<Range<$t>> for StepBy<Range<$t>> {
unsafe impl StepByImpl<Range<$t>> for StepBy<Range<$t>> {
#[inline]
fn spec_next(&mut self) -> Option<$t> {
// if a step size larger than the type has been specified fall back to
Expand Down Expand Up @@ -497,7 +497,7 @@ macro_rules! spec_int_ranges_r {
($($t:ty)*) => ($(
const _: () = assert!(usize::BITS >= <$t>::BITS);

impl StepByBackImpl<Range<$t>> for StepBy<Range<$t>> {
unsafe impl StepByBackImpl<Range<$t>> for StepBy<Range<$t>> {

#[inline]
fn spec_next_back(&mut self) -> Option<Self::Item>
Expand Down

0 comments on commit f174547

Please sign in to comment.