Skip to content

Commit e85cfa4

Browse files
committed
impl TrustedRandomAccess for vec::IntoIter
1 parent e115184 commit e85cfa4

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

library/alloc/src/vec.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ use core::cmp::{self, Ordering};
5858
use core::fmt;
5959
use core::hash::{Hash, Hasher};
6060
use core::intrinsics::{arith_offset, assume};
61-
use core::iter::{FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen};
61+
use core::iter::{
62+
FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen, TrustedRandomAccess,
63+
};
6264
use core::marker::PhantomData;
6365
use core::mem::{self, ManuallyDrop, MaybeUninit};
6466
use core::ops::Bound::{Excluded, Included, Unbounded};
@@ -2936,6 +2938,25 @@ impl<T> FusedIterator for IntoIter<T> {}
29362938
#[unstable(feature = "trusted_len", issue = "37572")]
29372939
unsafe impl<T> TrustedLen for IntoIter<T> {}
29382940

2941+
#[doc(hidden)]
2942+
#[unstable(issue = "0", feature = "std_internals")]
2943+
// T: Copy as approximation for !Drop since get_unchecked does not advance self.ptr
2944+
// and thus we can't implement drop-handling
2945+
unsafe impl<T> TrustedRandomAccess for IntoIter<T>
2946+
where
2947+
T: Copy,
2948+
{
2949+
unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item {
2950+
unsafe {
2951+
if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
2952+
}
2953+
}
2954+
2955+
fn may_have_side_effect() -> bool {
2956+
false
2957+
}
2958+
}
2959+
29392960
#[stable(feature = "vec_into_iter_clone", since = "1.8.0")]
29402961
impl<T: Clone> Clone for IntoIter<T> {
29412962
fn clone(&self) -> IntoIter<T> {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub use self::chain::Chain;
1818
pub use self::flatten::{FlatMap, Flatten};
1919
pub use self::fuse::Fuse;
2020
use self::zip::try_get_unchecked;
21-
pub(crate) use self::zip::TrustedRandomAccess;
21+
#[unstable(feature = "trusted_random_access", issue = "none")]
22+
pub use self::zip::TrustedRandomAccess;
2223
pub use self::zip::Zip;
2324

2425
/// This trait provides transitive access to source-stages in an interator-adapter pipeline
@@ -480,6 +481,7 @@ where
480481
unsafe impl<I> TrustedRandomAccess for Cloned<I>
481482
where
482483
I: TrustedRandomAccess,
484+
483485
{
484486
#[inline]
485487
fn may_have_side_effect() -> bool {

library/core/src/iter/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,16 @@ pub use self::adapters::MapWhile;
357357
pub use self::adapters::SourceIter;
358358
#[stable(feature = "iterator_step_by", since = "1.28.0")]
359359
pub use self::adapters::StepBy;
360+
#[unstable(feature = "trusted_random_access", issue = "none")]
361+
pub use self::adapters::TrustedRandomAccess;
360362
#[stable(feature = "rust1", since = "1.0.0")]
361363
pub use self::adapters::{Chain, Cycle, Enumerate, Filter, FilterMap, Map, Rev, Zip};
362364
#[stable(feature = "rust1", since = "1.0.0")]
363365
pub use self::adapters::{FlatMap, Peekable, Scan, Skip, SkipWhile, Take, TakeWhile};
364366
#[stable(feature = "rust1", since = "1.0.0")]
365367
pub use self::adapters::{Fuse, Inspect};
366368

367-
pub(crate) use self::adapters::{process_results, TrustedRandomAccess};
369+
pub(crate) use self::adapters::process_results;
368370

369371
mod adapters;
370372
mod range;

0 commit comments

Comments
 (0)