/// 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;
// 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];
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];
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];
}
}
+/// 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]
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]
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]
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
//