From f174547124ab84c699e519033b047ef01435c284 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Sun, 25 Jun 2023 18:09:47 +0200 Subject: [PATCH] Mark the StepBy specialization as unsafe --- library/core/src/iter/adapters/step_by.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/library/core/src/iter/adapters/step_by.rs b/library/core/src/iter/adapters/step_by.rs index 2458a13d987..7f58f7d1775 100644 --- a/library/core/src/iter/adapters/step_by.rs +++ b/library/core/src/iter/adapters/step_by.rs @@ -141,7 +141,7 @@ impl SpecRangeSetup for T { /// Specialization trait to optimize `StepBy>` 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. @@ -149,7 +149,7 @@ impl SpecRangeSetup for T { /// 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 { +unsafe trait StepByImpl { type Item; fn spec_next(&mut self) -> Option; @@ -172,13 +172,13 @@ fn spec_fold(self, acc: Acc, f: F) -> Acc /// /// 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 { +unsafe trait StepByBackImpl { type Item; fn spec_next_back(&mut self) -> Option @@ -201,7 +201,7 @@ fn spec_rfold(self, init: Acc, f: F) -> Acc F: FnMut(Acc, Self::Item) -> Acc; } -impl StepByImpl for StepBy { +unsafe impl StepByImpl for StepBy { type Item = I::Item; #[inline] @@ -319,7 +319,7 @@ fn nth(iter: &mut I, step: usize) -> impl FnMut() -> Option StepByBackImpl for StepBy { +unsafe impl StepByBackImpl for StepBy { type Item = I::Item; #[inline] @@ -416,7 +416,7 @@ fn setup(mut r: Range<$t>, step: usize) -> Range<$t> { } } - impl StepByImpl> for StepBy> { + unsafe impl StepByImpl> for StepBy> { #[inline] fn spec_next(&mut self) -> Option<$t> { // if a step size larger than the type has been specified fall back to @@ -497,7 +497,7 @@ macro_rules! spec_int_ranges_r { ($($t:ty)*) => ($( const _: () = assert!(usize::BITS >= <$t>::BITS); - impl StepByBackImpl> for StepBy> { + unsafe impl StepByBackImpl> for StepBy> { #[inline] fn spec_next_back(&mut self) -> Option