Skip to content

Commit 69dd992

Browse files
committed
Add TrustedRandomAccessNoCoerce supertrait without requirements or guarantees about subtype coercions
Update all the TrustedRandomAccess impls to also implement the new supertrait
1 parent 1c7f27f commit 69dd992

File tree

13 files changed

+160
-39
lines changed

13 files changed

+160
-39
lines changed

library/alloc/src/collections/vec_deque/iter.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::fmt;
2-
use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess};
2+
use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
33
use core::ops::Try;
44

55
use super::{count, wrap_index, RingSlices};
@@ -177,6 +177,10 @@ unsafe impl<T> TrustedLen for Iter<'_, T> {}
177177

178178
#[doc(hidden)]
179179
#[unstable(feature = "trusted_random_access", issue = "none")]
180-
unsafe impl<T> TrustedRandomAccess for Iter<'_, T> {
180+
unsafe impl<T> TrustedRandomAccess for Iter<'_, T> {}
181+
182+
#[doc(hidden)]
183+
#[unstable(feature = "trusted_random_access", issue = "none")]
184+
unsafe impl<T> TrustedRandomAccessNoCoerce for Iter<'_, T> {
181185
const MAY_HAVE_SIDE_EFFECT: bool = false;
182186
}

library/alloc/src/collections/vec_deque/iter_mut.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::fmt;
2-
use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess};
2+
use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
33
use core::marker::PhantomData;
44

55
use super::{count, wrap_index, RingSlices};
@@ -146,6 +146,10 @@ unsafe impl<T> TrustedLen for IterMut<'_, T> {}
146146

147147
#[doc(hidden)]
148148
#[unstable(feature = "trusted_random_access", issue = "none")]
149-
unsafe impl<T> TrustedRandomAccess for IterMut<'_, T> {
149+
unsafe impl<T> TrustedRandomAccess for IterMut<'_, T> {}
150+
151+
#[doc(hidden)]
152+
#[unstable(feature = "trusted_random_access", issue = "none")]
153+
unsafe impl<T> TrustedRandomAccessNoCoerce for IterMut<'_, T> {
150154
const MAY_HAVE_SIDE_EFFECT: bool = false;
151155
}

library/core/src/iter/adapters/cloned.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::iter::adapters::{zip::try_get_unchecked, TrustedRandomAccess};
1+
use crate::iter::adapters::{
2+
zip::try_get_unchecked, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
3+
};
24
use crate::iter::{FusedIterator, TrustedLen};
35
use crate::ops::Try;
46

@@ -121,9 +123,13 @@ where
121123

122124
#[doc(hidden)]
123125
#[unstable(feature = "trusted_random_access", issue = "none")]
124-
unsafe impl<I> TrustedRandomAccess for Cloned<I>
126+
unsafe impl<I> TrustedRandomAccess for Cloned<I> where I: TrustedRandomAccess {}
127+
128+
#[doc(hidden)]
129+
#[unstable(feature = "trusted_random_access", issue = "none")]
130+
unsafe impl<I> TrustedRandomAccessNoCoerce for Cloned<I>
125131
where
126-
I: TrustedRandomAccess,
132+
I: TrustedRandomAccessNoCoerce,
127133
{
128134
const MAY_HAVE_SIDE_EFFECT: bool = true;
129135
}

library/core/src/iter/adapters/copied.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::iter::adapters::{zip::try_get_unchecked, TrustedRandomAccess};
1+
use crate::iter::adapters::{
2+
zip::try_get_unchecked, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
3+
};
24
use crate::iter::{FusedIterator, TrustedLen};
35
use crate::ops::Try;
46

@@ -137,9 +139,13 @@ where
137139

138140
#[doc(hidden)]
139141
#[unstable(feature = "trusted_random_access", issue = "none")]
140-
unsafe impl<I> TrustedRandomAccess for Copied<I>
142+
unsafe impl<I> TrustedRandomAccess for Copied<I> where I: TrustedRandomAccess {}
143+
144+
#[doc(hidden)]
145+
#[unstable(feature = "trusted_random_access", issue = "none")]
146+
unsafe impl<I> TrustedRandomAccessNoCoerce for Copied<I>
141147
where
142-
I: TrustedRandomAccess,
148+
I: TrustedRandomAccessNoCoerce,
143149
{
144150
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
145151
}

library/core/src/iter/adapters/enumerate.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::iter::adapters::{zip::try_get_unchecked, SourceIter, TrustedRandomAccess};
1+
use crate::iter::adapters::{
2+
zip::try_get_unchecked, SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
3+
};
24
use crate::iter::{FusedIterator, InPlaceIterable, TrustedLen};
35
use crate::ops::Try;
46

@@ -207,9 +209,13 @@ where
207209

208210
#[doc(hidden)]
209211
#[unstable(feature = "trusted_random_access", issue = "none")]
210-
unsafe impl<I> TrustedRandomAccess for Enumerate<I>
212+
unsafe impl<I> TrustedRandomAccess for Enumerate<I> where I: TrustedRandomAccess {}
213+
214+
#[doc(hidden)]
215+
#[unstable(feature = "trusted_random_access", issue = "none")]
216+
unsafe impl<I> TrustedRandomAccessNoCoerce for Enumerate<I>
211217
where
212-
I: TrustedRandomAccess,
218+
I: TrustedRandomAccessNoCoerce,
213219
{
214220
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
215221
}

library/core/src/iter/adapters/fuse.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::intrinsics;
22
use crate::iter::adapters::zip::try_get_unchecked;
33
use crate::iter::{
44
DoubleEndedIterator, ExactSizeIterator, FusedIterator, TrustedLen, TrustedRandomAccess,
5+
TrustedRandomAccessNoCoerce,
56
};
67
use crate::ops::Try;
78

@@ -221,9 +222,13 @@ unsafe impl<I> TrustedLen for Fuse<I> where I: TrustedLen {}
221222
//
222223
// This is safe to implement as `Fuse` is just forwarding these to the wrapped iterator `I`, which
223224
// preserves these properties.
224-
unsafe impl<I> TrustedRandomAccess for Fuse<I>
225+
unsafe impl<I> TrustedRandomAccess for Fuse<I> where I: TrustedRandomAccess {}
226+
227+
#[doc(hidden)]
228+
#[unstable(feature = "trusted_random_access", issue = "none")]
229+
unsafe impl<I> TrustedRandomAccessNoCoerce for Fuse<I>
225230
where
226-
I: TrustedRandomAccess,
231+
I: TrustedRandomAccessNoCoerce,
227232
{
228233
const MAY_HAVE_SIDE_EFFECT: bool = I::MAY_HAVE_SIDE_EFFECT;
229234
}

library/core/src/iter/adapters/map.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::fmt;
2-
use crate::iter::adapters::{zip::try_get_unchecked, SourceIter, TrustedRandomAccess};
2+
use crate::iter::adapters::{
3+
zip::try_get_unchecked, SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce,
4+
};
35
use crate::iter::{FusedIterator, InPlaceIterable, TrustedLen};
46
use crate::ops::Try;
57

@@ -187,9 +189,13 @@ where
187189

188190
#[doc(hidden)]
189191
#[unstable(feature = "trusted_random_access", issue = "none")]
190-
unsafe impl<I, F> TrustedRandomAccess for Map<I, F>
192+
unsafe impl<I, F> TrustedRandomAccess for Map<I, F> where I: TrustedRandomAccess {}
193+
194+
#[doc(hidden)]
195+
#[unstable(feature = "trusted_random_access", issue = "none")]
196+
unsafe impl<I, F> TrustedRandomAccessNoCoerce for Map<I, F>
191197
where
192-
I: TrustedRandomAccess,
198+
I: TrustedRandomAccessNoCoerce,
193199
{
194200
const MAY_HAVE_SIDE_EFFECT: bool = true;
195201
}

library/core/src/iter/adapters/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ pub use self::map_while::MapWhile;
5151
#[unstable(feature = "trusted_random_access", issue = "none")]
5252
pub use self::zip::TrustedRandomAccess;
5353

54+
#[unstable(feature = "trusted_random_access", issue = "none")]
55+
pub use self::zip::TrustedRandomAccessNoCoerce;
56+
5457
#[unstable(feature = "iter_zip", issue = "83574")]
5558
pub use self::zip::zip;
5659

library/core/src/iter/adapters/zip.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,15 @@ unsafe impl<A, B> TrustedRandomAccess for Zip<A, B>
354354
where
355355
A: TrustedRandomAccess,
356356
B: TrustedRandomAccess,
357+
{
358+
}
359+
360+
#[doc(hidden)]
361+
#[unstable(feature = "trusted_random_access", issue = "none")]
362+
unsafe impl<A, B> TrustedRandomAccessNoCoerce for Zip<A, B>
363+
where
364+
A: TrustedRandomAccessNoCoerce,
365+
B: TrustedRandomAccessNoCoerce,
357366
{
358367
const MAY_HAVE_SIDE_EFFECT: bool = A::MAY_HAVE_SIDE_EFFECT || B::MAY_HAVE_SIDE_EFFECT;
359368
}
@@ -431,7 +440,7 @@ impl<A: Debug + TrustedRandomAccess, B: Debug + TrustedRandomAccess> ZipFmt<A, B
431440
///
432441
/// The iterator's `size_hint` must be exact and cheap to call.
433442
///
434-
/// `size` may not be overridden.
443+
/// `TrustedRandomAccessNoCoerce::size` may not be overridden.
435444
///
436445
/// All subtypes and all supertypes of `Self` must also implement `TrustedRandomAccess`.
437446
/// In particular, this means that types with non-invariant parameters usually can not have
@@ -455,7 +464,7 @@ impl<A: Debug + TrustedRandomAccess, B: Debug + TrustedRandomAccess> ZipFmt<A, B
455464
/// * `std::iter::Iterator::size_hint`
456465
/// * `std::iter::DoubleEndedIterator::next_back`
457466
/// * `std::iter::Iterator::__iterator_get_unchecked`
458-
/// * `std::iter::TrustedRandomAccess::size`
467+
/// * `std::iter::TrustedRandomAccessNoCoerce::size`
459468
/// 5. If `T` is a subtype of `Self`, then `self` is allowed to be coerced
460469
/// to `T`. If `self` is coerced to `T` after `self.__iterator_get_unchecked(idx)` has already
461470
/// been called, then no methods except for the ones listed under 4. are allowed to be called
@@ -474,7 +483,15 @@ impl<A: Debug + TrustedRandomAccess, B: Debug + TrustedRandomAccess> ZipFmt<A, B
474483
#[doc(hidden)]
475484
#[unstable(feature = "trusted_random_access", issue = "none")]
476485
#[rustc_specialization_trait]
477-
pub unsafe trait TrustedRandomAccess: Sized {
486+
pub unsafe trait TrustedRandomAccess: TrustedRandomAccessNoCoerce {}
487+
488+
/// Like [`TrustedRandomAccess`] but without any of the requirements / guarantees around
489+
/// coercions to subtypes after `__iterator_get_unchecked` (they aren’t allowed here!), and
490+
/// without the requirement that subtypes / supertypes implement [`TrustedRandomAccessNoCoerce`].
491+
#[doc(hidden)]
492+
#[unstable(feature = "trusted_random_access", issue = "none")]
493+
#[rustc_specialization_trait]
494+
pub unsafe trait TrustedRandomAccessNoCoerce: Sized {
478495
// Convenience method.
479496
fn size(&self) -> usize
480497
where

library/core/src/iter/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ pub use self::adapters::SourceIter;
407407
pub use self::adapters::StepBy;
408408
#[unstable(feature = "trusted_random_access", issue = "none")]
409409
pub use self::adapters::TrustedRandomAccess;
410+
#[unstable(feature = "trusted_random_access", issue = "none")]
411+
pub use self::adapters::TrustedRandomAccessNoCoerce;
410412
#[stable(feature = "rust1", since = "1.0.0")]
411413
pub use self::adapters::{
412414
Chain, Cycle, Enumerate, Filter, FilterMap, FlatMap, Fuse, Inspect, Map, Peekable, Rev, Scan,

library/core/src/iter/range.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::convert::TryFrom;
33
use crate::mem;
44
use crate::ops::{self, Try};
55

6-
use super::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedStep};
6+
use super::{
7+
FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, TrustedStep,
8+
};
79

810
// Safety: All invariants are upheld.
911
macro_rules! unsafe_impl_trusted_step {
@@ -495,7 +497,11 @@ macro_rules! unsafe_range_trusted_random_access_impl {
495497
($($t:ty)*) => ($(
496498
#[doc(hidden)]
497499
#[unstable(feature = "trusted_random_access", issue = "none")]
498-
unsafe impl TrustedRandomAccess for ops::Range<$t> {
500+
unsafe impl TrustedRandomAccess for ops::Range<$t> {}
501+
502+
#[doc(hidden)]
503+
#[unstable(feature = "trusted_random_access", issue = "none")]
504+
unsafe impl TrustedRandomAccessNoCoerce for ops::Range<$t> {
499505
const MAY_HAVE_SIDE_EFFECT: bool = false;
500506
}
501507
)*)

0 commit comments

Comments
 (0)