//!
//! For more details `std::slice`.
-#![stable]
+#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "slice")]
// How this module is organized.
use cmp;
use default::Default;
use iter::*;
-use marker::Copy;
use num::Int;
use ops::{FnMut, self, Index};
+use ops::RangeFull;
use option::Option;
use option::Option::{None, Some};
use result::Result;
fn clone_from_slice(&mut self, &[Self::Item]) -> uint where Self::Item: Clone;
}
-#[unstable]
+#[unstable(feature = "core")]
impl<T> SliceExt for [T] {
type Item = T;
self.repr().data
}
- #[unstable]
+ #[unstable(feature = "core")]
fn binary_search_by<F>(&self, mut f: F) -> Result<uint, uint> where
F: FnMut(&T) -> Ordering
{
m >= n && needle == &self[m-n..]
}
- #[unstable]
+ #[unstable(feature = "core")]
fn binary_search(&self, x: &T) -> Result<uint, uint> where T: Ord {
self.binary_search_by(|p| p.cmp(x))
}
- #[unstable]
+ #[unstable(feature = "core")]
fn next_permutation(&mut self) -> bool where T: Ord {
// These cases only have 1 permutation each, so we can't do anything.
if self.len() < 2 { return false; }
true
}
- #[unstable]
+ #[unstable(feature = "core")]
fn prev_permutation(&mut self) -> bool where T: Ord {
// These cases only have 1 permutation each, so we can't do anything.
if self.len() < 2 { return false; }
let min = cmp::min(self.len(), src.len());
let dst = &mut self[.. min];
let src = &src[.. min];
- for i in range(0, min) {
+ for i in 0..min {
dst[i].clone_from(&src[i]);
}
min
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<uint> for [T] {
type Output = T;
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<uint> for [T] {
- type Output = T;
-
fn index_mut(&mut self, &index: &uint) -> &mut T {
assert!(index < self.len());
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::Range<uint>> for [T] {
type Output = [T];
#[inline]
}
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeTo<uint>> for [T] {
type Output = [T];
#[inline]
self.index(&ops::Range{ start: 0, end: index.end })
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeFrom<uint>> for [T] {
type Output = [T];
#[inline]
self.index(&ops::Range{ start: index.start, end: self.len() })
}
}
-#[stable]
-impl<T> ops::Index<ops::FullRange> for [T] {
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> ops::Index<RangeFull> for [T] {
type Output = [T];
#[inline]
- fn index(&self, _index: &ops::FullRange) -> &[T] {
+ fn index(&self, _index: &RangeFull) -> &[T] {
self
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::Range<uint>> for [T] {
- type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
assert!(index.start <= index.end);
}
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeTo<uint>> for [T] {
- type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
self.index_mut(&ops::Range{ start: 0, end: index.end })
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFrom<uint>> for [T] {
- type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
let len = self.len();
self.index_mut(&ops::Range{ start: index.start, end: len })
}
}
-#[stable]
-impl<T> ops::IndexMut<ops::FullRange> for [T] {
- type Output = [T];
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> ops::IndexMut<RangeFull> for [T] {
#[inline]
- fn index_mut(&mut self, _index: &ops::FullRange) -> &mut [T] {
+ fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
self
}
}
////////////////////////////////////////////////////////////////////////////////
/// Data that is viewable as a slice.
-#[unstable = "will be replaced by slice syntax"]
+#[unstable(feature = "core",
+ reason = "will be replaced by slice syntax")]
pub trait AsSlice<T> {
/// Work with `self` as a slice.
fn as_slice<'a>(&'a self) -> &'a [T];
}
-#[unstable = "trait is experimental"]
+#[unstable(feature = "core", reason = "trait is experimental")]
impl<T> AsSlice<T> for [T] {
#[inline(always)]
fn as_slice<'a>(&'a self) -> &'a [T] { self }
}
-#[unstable = "trait is experimental"]
+#[unstable(feature = "core", reason = "trait is experimental")]
impl<'a, T, U: ?Sized + AsSlice<T>> AsSlice<T> for &'a U {
#[inline(always)]
fn as_slice(&self) -> &[T] { AsSlice::as_slice(*self) }
}
-#[unstable = "trait is experimental"]
+#[unstable(feature = "core", reason = "trait is experimental")]
impl<'a, T, U: ?Sized + AsSlice<T>> AsSlice<T> for &'a mut U {
#[inline(always)]
fn as_slice(&self) -> &[T] { AsSlice::as_slice(*self) }
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Default for &'a [T] {
- #[stable]
+ #[stable(feature = "rust1", since = "1.0.0")]
fn default() -> &'a [T] { &[] }
}
// Iterators
//
+impl<'a, T> IntoIterator for &'a [T] {
+ type Iter = Iter<'a, T>;
+
+ fn into_iter(self) -> Iter<'a, T> {
+ self.iter()
+ }
+}
+
+impl<'a, T> IntoIterator for &'a mut [T] {
+ type Iter = IterMut<'a, T>;
+
+ fn into_iter(self) -> IterMut<'a, T> {
+ self.iter_mut()
+ }
+}
+
// The shared definition of the `Iter` and `IterMut` iterators
macro_rules! iterator {
(struct $name:ident -> $ptr:ty, $elem:ty) => {
- #[stable]
+ #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Iterator for $name<'a, T> {
type Item = $elem;
}
}
- #[stable]
+ #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> DoubleEndedIterator for $name<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<$elem> {
}
/// Immutable slice iterator
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T: 'a> {
ptr: *const T,
end: *const T,
marker: marker::ContravariantLifetime<'a>
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::Range<uint>> for Iter<'a, T> {
type Output = [T];
#[inline]
}
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeTo<uint>> for Iter<'a, T> {
type Output = [T];
#[inline]
}
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeFrom<uint>> for Iter<'a, T> {
type Output = [T];
#[inline]
}
}
-#[unstable]
-impl<'a, T> ops::Index<ops::FullRange> for Iter<'a, T> {
+#[unstable(feature = "core")]
+impl<'a, T> ops::Index<RangeFull> for Iter<'a, T> {
type Output = [T];
#[inline]
- fn index(&self, _index: &ops::FullRange) -> &[T] {
+ fn index(&self, _index: &RangeFull) -> &[T] {
self.as_slice()
}
}
///
/// This has the same lifetime as the original slice, and so the
/// iterator can continue to be used while this exists.
- #[unstable]
+ #[unstable(feature = "core")]
pub fn as_slice(&self) -> &'a [T] {
make_slice!(T => &'a [T]: self.ptr, self.end)
}
}
-impl<'a,T> Copy for Iter<'a,T> {}
-
iterator!{struct Iter -> *const T, &'a T}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Clone for Iter<'a, T> {
- fn clone(&self) -> Iter<'a, T> { *self }
+ fn clone(&self) -> Iter<'a, T> { Iter { ptr: self.ptr, end: self.end, marker: self.marker } }
}
-#[unstable = "trait is experimental"]
+#[unstable(feature = "core", reason = "trait is experimental")]
impl<'a, T> RandomAccessIterator for Iter<'a, T> {
#[inline]
fn indexable(&self) -> uint {
}
/// Mutable slice iterator.
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct IterMut<'a, T: 'a> {
ptr: *mut T,
end: *mut T,
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::Range<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index(&self, index: &ops::Range<uint>) -> &[T] {
- self.index(&ops::FullRange).index(index)
+ self.index(&RangeFull).index(index)
}
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeTo<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index(&self, index: &ops::RangeTo<uint>) -> &[T] {
- self.index(&ops::FullRange).index(index)
+ self.index(&RangeFull).index(index)
}
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeFrom<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
- self.index(&ops::FullRange).index(index)
+ self.index(&RangeFull).index(index)
}
}
-#[unstable]
-impl<'a, T> ops::Index<ops::FullRange> for IterMut<'a, T> {
+#[unstable(feature = "core")]
+impl<'a, T> ops::Index<RangeFull> for IterMut<'a, T> {
type Output = [T];
#[inline]
- fn index(&self, _index: &ops::FullRange) -> &[T] {
+ fn index(&self, _index: &RangeFull) -> &[T] {
make_slice!(T => &[T]: self.ptr, self.end)
}
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::Range<uint>> for IterMut<'a, T> {
- type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
- self.index_mut(&ops::FullRange).index_mut(index)
+ self.index_mut(&RangeFull).index_mut(index)
}
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::RangeTo<uint>> for IterMut<'a, T> {
- type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
- self.index_mut(&ops::FullRange).index_mut(index)
+ self.index_mut(&RangeFull).index_mut(index)
}
}
-#[unstable]
+#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::RangeFrom<uint>> for IterMut<'a, T> {
- type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
- self.index_mut(&ops::FullRange).index_mut(index)
+ self.index_mut(&RangeFull).index_mut(index)
}
}
-#[unstable]
-impl<'a, T> ops::IndexMut<ops::FullRange> for IterMut<'a, T> {
- type Output = [T];
+#[unstable(feature = "core")]
+impl<'a, T> ops::IndexMut<RangeFull> for IterMut<'a, T> {
#[inline]
- fn index_mut(&mut self, _index: &ops::FullRange) -> &mut [T] {
+ fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
make_slice!(T => &mut [T]: self.ptr, self.end)
}
}
/// to consume the iterator. Consider using the `Slice` and
/// `SliceMut` implementations for obtaining slices with more
/// restricted lifetimes that do not consume the iterator.
- #[unstable]
+ #[unstable(feature = "core")]
pub fn into_slice(self) -> &'a mut [T] {
make_slice!(T => &'a mut [T]: self.ptr, self.end)
}
iterator!{struct IterMut -> *mut T, &'a mut T}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
/// An internal abstraction over the splitting iterators, so that
/// An iterator over subslices separated by elements that match a predicate
/// function.
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct Split<'a, T:'a, P> where P: FnMut(&T) -> bool {
v: &'a [T],
pred: P,
}
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, P> Clone for Split<'a, T, P> where P: Clone + FnMut(&T) -> bool {
fn clone(&self) -> Split<'a, T, P> {
Split {
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, P> Iterator for Split<'a, T, P> where P: FnMut(&T) -> bool {
type Item = &'a [T];
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, P> DoubleEndedIterator for Split<'a, T, P> where P: FnMut(&T) -> bool {
#[inline]
fn next_back(&mut self) -> Option<&'a [T]> {
/// An iterator over the subslices of the vector which are separated
/// by elements that match `pred`.
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct SplitMut<'a, T:'a, P> where P: FnMut(&T) -> bool {
v: &'a mut [T],
pred: P,
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, P> Iterator for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {
type Item = &'a mut [T];
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T, P> DoubleEndedIterator for SplitMut<'a, T, P> where
P: FnMut(&T) -> bool,
{
/// An iterator over subslices separated by elements that match a predicate
/// function, limited to a given number of splits.
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct SplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool {
inner: GenericSplitN<Split<'a, T, P>>
}
/// An iterator over subslices separated by elements that match a
/// predicate function, limited to a given number of splits, starting
/// from the end of the slice.
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct RSplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool {
inner: GenericSplitN<Split<'a, T, P>>
}
/// An iterator over subslices separated by elements that match a predicate
/// function, limited to a given number of splits.
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct SplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool {
inner: GenericSplitN<SplitMut<'a, T, P>>
}
/// An iterator over subslices separated by elements that match a
/// predicate function, limited to a given number of splits, starting
/// from the end of the slice.
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct RSplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool {
inner: GenericSplitN<SplitMut<'a, T, P>>
}
macro_rules! forward_iterator {
($name:ident: $elem:ident, $iter_of:ty) => {
- #[stable]
+ #[stable(feature = "rust1", since = "1.0.0")]
impl<'a, $elem, P> Iterator for $name<'a, $elem, P> where
P: FnMut(&T) -> bool
{
/// An iterator over overlapping subslices of length `size`.
#[derive(Clone)]
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct Windows<'a, T:'a> {
v: &'a [T],
size: uint
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Iterator for Windows<'a, T> {
type Item = &'a [T];
/// When the slice len is not evenly divided by the chunk size, the last slice
/// of the iteration will be the remainder.
#[derive(Clone)]
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct Chunks<'a, T:'a> {
v: &'a [T],
size: uint
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Iterator for Chunks<'a, T> {
type Item = &'a [T];
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<&'a [T]> {
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> ExactSizeIterator for Chunks<'a, T> {}
-#[unstable = "trait is experimental"]
+#[unstable(feature = "core", reason = "trait is experimental")]
impl<'a, T> RandomAccessIterator for Chunks<'a, T> {
#[inline]
fn indexable(&self) -> uint {
/// An iterator over a slice in (non-overlapping) mutable chunks (`size`
/// elements at a time). When the slice len is not evenly divided by the chunk
/// size, the last slice of the iteration will be the remainder.
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
pub struct ChunksMut<'a, T:'a> {
v: &'a mut [T],
chunk_size: uint
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Iterator for ChunksMut<'a, T> {
type Item = &'a mut [T];
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<&'a mut [T]> {
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> ExactSizeIterator for ChunksMut<'a, T> {}
//
//
/// Converts a pointer to A into a slice of length 1 (without copying).
-#[unstable]
+#[unstable(feature = "core")]
pub fn ref_slice<'a, A>(s: &'a A) -> &'a [A] {
unsafe {
transmute(RawSlice { data: s, len: 1 })
}
/// Converts a pointer to A into a slice of length 1 (without copying).
-#[unstable]
+#[unstable(feature = "core")]
pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] {
unsafe {
let ptr: *const A = transmute(s);
}
}
+/// Forms a slice from a pointer and a length.
+///
+/// The `len` argument is the number of **elements**, not the number of bytes.
+///
+/// This function is unsafe as there is no guarantee that the given pointer is
+/// valid for `len` elements, nor whether the lifetime inferred is a suitable
+/// lifetime for the returned slice.
+///
+/// # Caveat
+///
+/// The lifetime for the returned slice is inferred from its usage. To
+/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
+/// source lifetime is safe in the context, such as by providing a helper
+/// function taking the lifetime of a host value for the slice, or by explicit
+/// annotation.
+///
+/// # Example
+///
+/// ```rust
+/// use std::slice;
+///
+/// // manifest a slice out of thin air!
+/// let ptr = 0x1234 as *const uint;
+/// let amt = 10;
+/// unsafe {
+/// let slice = slice::from_raw_parts(ptr, amt);
+/// }
+/// ```
+#[inline]
+#[unstable(feature = "core")]
+pub unsafe fn from_raw_parts<'a, T>(p: *const T, len: uint) -> &'a [T] {
+ transmute(RawSlice { data: p, len: len })
+}
+
+/// Performs the same functionality as `from_raw_parts`, except that a mutable
+/// slice is returned.
+///
+/// This function is unsafe for the same reasons as `from_raw_parts`, as well
+/// as not being able to provide a non-aliasing guarantee of the returned
+/// mutable slice.
+#[inline]
+#[unstable(feature = "core")]
+pub unsafe fn from_raw_parts_mut<'a, T>(p: *mut T, len: uint) -> &'a mut [T] {
+ transmute(RawSlice { data: p, len: len })
+}
+
/// Forms a slice from a pointer and a length.
///
/// The pointer given is actually a reference to the base of the slice. This
/// }
/// ```
#[inline]
-#[unstable = "should be renamed to from_raw_parts"]
+#[unstable(feature = "core")]
+#[deprecated(since = "1.0.0",
+ reason = "use from_raw_parts")]
pub unsafe fn from_raw_buf<'a, T>(p: &'a *const T, len: uint) -> &'a [T] {
transmute(RawSlice { data: *p, len: len })
}
/// not being able to provide a non-aliasing guarantee of the returned mutable
/// slice.
#[inline]
-#[unstable = "should be renamed to from_raw_parts_mut"]
+#[unstable(feature = "core")]
+#[deprecated(since = "1.0.0",
+ reason = "use from_raw_parts_mut")]
pub unsafe fn from_raw_mut_buf<'a, T>(p: &'a *mut T, len: uint) -> &'a mut [T] {
transmute(RawSlice { data: *p, len: len })
}
//
/// Operations on `[u8]`.
-#[unstable = "needs review"]
+#[unstable(feature = "core", reason = "needs review")]
pub mod bytes {
use ptr;
use slice::SliceExt;
impl MutableByteVector for [u8] {
#[inline]
- #[allow(unstable)]
fn set_memory(&mut self, value: u8) {
unsafe { ptr::set_memory(self.as_mut_ptr(), value, self.len()) };
}
// Boilerplate traits
//
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<A, B> PartialEq<[B]> for [A] where A: PartialEq<B> {
fn eq(&self, other: &[B]) -> bool {
self.len() == other.len() &&
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Eq> Eq for [T] {}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Ord> Ord for [T] {
fn cmp(&self, other: &[T]) -> Ordering {
order::cmp(self.iter(), other.iter())
}
}
-#[stable]
+#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PartialOrd> PartialOrd for [T] {
#[inline]
fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
}
/// Extension methods for slices containing integers.
-#[unstable]
+#[unstable(feature = "core")]
pub trait IntSliceExt<U, S> {
/// Converts the slice to an immutable slice of unsigned integers with the same width.
fn as_unsigned<'a>(&'a self) -> &'a [U];
macro_rules! impl_int_slice {
($u:ty, $s:ty, $t:ty) => {
- #[unstable]
+ #[unstable(feature = "core")]
impl IntSliceExt<$u, $s> for [$t] {
#[inline]
fn as_unsigned(&self) -> &[$u] { unsafe { transmute(self) } }