From 520e8b001ef7c49dcf1ad8f1f7817f2b16ad709b Mon Sep 17 00:00:00 2001 From: Clar Fon Date: Wed, 19 Dec 2018 16:33:53 -0500 Subject: [PATCH] Move TrustedRandomAccess into Zip module --- src/libcore/iter/adapters/mod.rs | 2 +- src/libcore/iter/adapters/zip.rs | 18 +++++++++++++++++- src/libcore/iter/mod.rs | 1 + src/libcore/lib.rs | 1 - src/libcore/slice/mod.rs | 1 - src/libcore/str/mod.rs | 3 +-- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/libcore/iter/adapters/mod.rs b/src/libcore/iter/adapters/mod.rs index b0faded887130..f2bedf390e3bc 100644 --- a/src/libcore/iter/adapters/mod.rs +++ b/src/libcore/iter/adapters/mod.rs @@ -1,6 +1,5 @@ use cmp; use fmt; -use iter_private::TrustedRandomAccess; use ops::Try; use usize; use intrinsics; @@ -11,6 +10,7 @@ mod zip; pub use self::zip::Zip; pub(super) use self::zip::ZipImpl; +pub(crate) use self::zip::TrustedRandomAccess; /// A double-ended iterator with the direction inverted. /// diff --git a/src/libcore/iter/adapters/zip.rs b/src/libcore/iter/adapters/zip.rs index d2937f5d3e147..6a0a7d8b12cdf 100644 --- a/src/libcore/iter/adapters/zip.rs +++ b/src/libcore/iter/adapters/zip.rs @@ -1,5 +1,4 @@ use cmp; -use iter_private::TrustedRandomAccess; use super::super::{Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, TrustedLen}; /// An iterator that iterates two other iterators simultaneously. @@ -259,3 +258,20 @@ impl FusedIterator for Zip unsafe impl TrustedLen for Zip where A: TrustedLen, B: TrustedLen, {} + +/// An iterator whose items are random-accessible efficiently +/// +/// # Safety +/// +/// The iterator's .len() and size_hint() must be exact. +/// `.len()` must be cheap to call. +/// +/// .get_unchecked() must return distinct mutable references for distinct +/// indices (if applicable), and must return a valid reference if index is in +/// 0..self.len(). +pub(crate) unsafe trait TrustedRandomAccess : ExactSizeIterator { + unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item; + /// Returns `true` if getting an iterator element may have + /// side effects. Remember to take inner iterators into account. + fn may_have_side_effect() -> bool; +} diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index 6dc7469998c02..1f390d7e0a925 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -354,6 +354,7 @@ pub use self::adapters::Flatten; pub use self::adapters::Copied; use self::adapters::{flatten_compat, ChainState, ZipImpl}; +pub(crate) use self::adapters::TrustedRandomAccess; mod range; mod sources; diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index df32cfa337313..c8cd72371e748 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -219,7 +219,6 @@ pub mod task; pub mod alloc; // note: does not need to be public -mod iter_private; mod tuple; mod unit; diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 9f9515e6d9b7c..d062da0c247ad 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -34,7 +34,6 @@ use result::Result::{Ok, Err}; use ptr; use mem; use marker::{Copy, Send, Sync, Sized, self}; -use iter_private::TrustedRandomAccess; #[unstable(feature = "slice_internals", issue = "0", reason = "exposed from core to be reused in std; use the memchr crate")] diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 1ee8b7735c17d..ac92018563654 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -9,8 +9,7 @@ use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher}; use char; use fmt; -use iter::{Map, Cloned, FusedIterator, TrustedLen, Filter}; -use iter_private::TrustedRandomAccess; +use iter::{Map, Cloned, FusedIterator, TrustedLen, TrustedRandomAccess, Filter}; use slice::{self, SliceIndex, Split as SliceSplit}; use mem;