/// Extension methods for slices.
#[unstable(feature = "core_slice_ext",
reason = "stable interface provided by `impl [T]` in later crates",
- issue = "27701")]
+ issue = "32110")]
#[allow(missing_docs)] // documented elsewhere
pub trait SliceExt {
type Item;
#[unstable(feature = "core_slice_ext",
reason = "stable interface provided by `impl [T]` in later crates",
- issue = "27701")]
+ issue = "32110")]
impl<T> SliceExt for [T] {
type Item = T;
#[inline]
unsafe fn get_unchecked(&self, index: usize) -> &T {
- &*(self.repr().data.offset(index as isize))
+ &*(self.as_ptr().offset(index as isize))
}
#[inline]
fn as_ptr(&self) -> *const T {
- self.repr().data
+ self as *const [T] as *const T
}
fn binary_search_by<F>(&self, mut f: F) -> Result<usize, usize> where
#[inline]
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
- &mut *(self.repr().data as *mut T).offset(index as isize)
+ &mut *self.as_mut_ptr().offset(index as isize)
}
#[inline]
fn as_mut_ptr(&mut self) -> *mut T {
- self.repr().data as *mut T
+ self as *mut [T] as *mut T
}
#[inline]
panic!("slice index starts at {} but ends at {}", index, end);
}
+// FIXME implement indexing with inclusive ranges
+
+/// Implements slicing with syntax `&self[begin .. end]`.
+///
+/// Returns a slice of self for the index range [`begin`..`end`).
+///
+/// This operation is `O(1)`.
+///
+/// # Panics
+///
+/// Requires that `begin <= end` and `end <= self.len()`,
+/// otherwise slicing will panic.
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::Range<usize>> for [T] {
type Output = [T];
}
}
}
+
+/// Implements slicing with syntax `&self[.. end]`.
+///
+/// Returns a slice of self from the beginning until but not including
+/// the index `end`.
+///
+/// Equivalent to `&self[0 .. end]`
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeTo<usize>> for [T] {
type Output = [T];
#[inline]
fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
- self.index(ops::Range{ start: 0, end: index.end })
+ self.index(0 .. index.end)
}
}
+
+/// Implements slicing with syntax `&self[begin ..]`.
+///
+/// Returns a slice of self from and including the index `begin` until the end.
+///
+/// Equivalent to `&self[begin .. self.len()]`
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeFrom<usize>> for [T] {
type Output = [T];
#[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
- self.index(ops::Range{ start: index.start, end: self.len() })
+ self.index(index.start .. self.len())
}
}
+
+/// Implements slicing with syntax `&self[..]`.
+///
+/// Returns a slice of the whole slice. This operation can not panic.
+///
+/// Equivalent to `&self[0 .. self.len()]`
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<RangeFull> for [T] {
type Output = [T];
}
}
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+impl<T> ops::Index<ops::RangeInclusive<usize>> for [T] {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: ops::RangeInclusive<usize>) -> &[T] {
+ match index {
+ ops::RangeInclusive::Empty { .. } => &[],
+ ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() =>
+ panic!("attempted to index slice up to maximum usize"),
+ ops::RangeInclusive::NonEmpty { start, end } =>
+ self.index(start .. end+1)
+ }
+ }
+}
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+impl<T> ops::Index<ops::RangeToInclusive<usize>> for [T] {
+ type Output = [T];
+
+ #[inline]
+ fn index(&self, index: ops::RangeToInclusive<usize>) -> &[T] {
+ // SNAP 4d3eebf change this to `0...index.end`
+ self.index(ops::RangeInclusive::NonEmpty { start: 0, end: index.end })
+ }
+}
+
+/// Implements mutable slicing with syntax `&mut self[begin .. end]`.
+///
+/// Returns a slice of self for the index range [`begin`..`end`).
+///
+/// This operation is `O(1)`.
+///
+/// # Panics
+///
+/// Requires that `begin <= end` and `end <= self.len()`,
+/// otherwise slicing will panic.
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::Range<usize>> for [T] {
#[inline]
}
}
}
+
+/// Implements mutable slicing with syntax `&mut self[.. end]`.
+///
+/// Returns a slice of self from the beginning until but not including
+/// the index `end`.
+///
+/// Equivalent to `&mut self[0 .. end]`
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeTo<usize>> for [T] {
#[inline]
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] {
- self.index_mut(ops::Range{ start: 0, end: index.end })
+ self.index_mut(0 .. index.end)
}
}
+
+/// Implements mutable slicing with syntax `&mut self[begin ..]`.
+///
+/// Returns a slice of self from and including the index `begin` until the end.
+///
+/// Equivalent to `&mut self[begin .. self.len()]`
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFrom<usize>> for [T] {
#[inline]
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] {
let len = self.len();
- self.index_mut(ops::Range{ start: index.start, end: len })
+ self.index_mut(index.start .. len)
}
}
+
+/// Implements mutable slicing with syntax `&mut self[..]`.
+///
+/// Returns a slice of the whole slice. This operation can not panic.
+///
+/// Equivalent to `&mut self[0 .. self.len()]`
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<RangeFull> for [T] {
#[inline]
}
}
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+impl<T> ops::IndexMut<ops::RangeInclusive<usize>> for [T] {
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut [T] {
+ match index {
+ ops::RangeInclusive::Empty { .. } => &mut [],
+ ops::RangeInclusive::NonEmpty { end, .. } if end == usize::max_value() =>
+ panic!("attempted to index slice up to maximum usize"),
+ ops::RangeInclusive::NonEmpty { start, end } =>
+ self.index_mut(start .. end+1)
+ }
+ }
+}
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+impl<T> ops::IndexMut<ops::RangeToInclusive<usize>> for [T] {
+ #[inline]
+ fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut [T] {
+ // SNAP 4d3eebf change this to `0...index.end`
+ self.index_mut(ops::RangeInclusive::NonEmpty { start: 0, end: index.end })
+ }
+}
////////////////////////////////////////////////////////////////////////////////
// Common traits
/// An internal abstraction over the splitting iterators, so that
/// splitn, splitn_mut etc can be implemented once.
+#[doc(hidden)]
trait SplitIter: DoubleEndedIterator {
/// Mark the underlying iterator as complete, extracting the remaining
/// portion of the slice.
mem::transmute(RawSlice { data: p, len: len })
}
-//
-// Submodules
-//
-
-/// Operations on `[u8]`.
-#[unstable(feature = "slice_bytes", reason = "needs review",
- issue = "27740")]
-#[rustc_deprecated(reason = "unidiomatic functions not pulling their weight",
- since = "1.6.0")]
-#[allow(deprecated)]
-pub mod bytes {
- use ptr;
- use slice::SliceExt;
-
- /// A trait for operations on mutable `[u8]`s.
- pub trait MutableByteVector {
- /// Sets all bytes of the receiver to the given value.
- fn set_memory(&mut self, value: u8);
- }
-
- impl MutableByteVector for [u8] {
- #[inline]
- fn set_memory(&mut self, value: u8) {
- unsafe { ptr::write_bytes(self.as_mut_ptr(), value, self.len()) };
- }
- }
-
- /// Copies data from `src` to `dst`
- ///
- /// Panics if the length of `dst` is less than the length of `src`.
- #[inline]
- pub fn copy_memory(src: &[u8], dst: &mut [u8]) {
- let len_src = src.len();
- assert!(dst.len() >= len_src);
- // `dst` is unaliasable, so we know statically it doesn't overlap
- // with `src`.
- unsafe {
- ptr::copy_nonoverlapping(src.as_ptr(),
- dst.as_mut_ptr(),
- len_src);
- }
- }
-}
-
-
-
//
// Boilerplate traits
//