use cmp;
use default::Default;
use iter::*;
-use marker::Copy;
use num::Int;
use ops::{FnMut, self, Index};
-#[cfg(stage0)]
-use ops::FullRange as RangeFull;
-#[cfg(not(stage0))]
use ops::RangeFull;
use option::Option;
use option::Option::{None, Some};
#[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(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(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(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();
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<RangeFull> for [T] {
- type Output = [T];
#[inline]
fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
self
// 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) => {
}
}
-#[cfg(stage0)]
-#[unstable(feature = "core")]
-impl<'a, T> ops::Index<ops::FullRange> for Iter<'a, T> {
- type Output = [T];
- #[inline]
- fn index(&self, _index: &ops::FullRange) -> &[T] {
- self.as_slice()
- }
-}
-#[cfg(not(stage0))]
#[unstable(feature = "core")]
impl<'a, T> ops::Index<RangeFull> for Iter<'a, T> {
type Output = [T];
}
}
-impl<'a,T> Copy for Iter<'a,T> {}
-
iterator!{struct Iter -> *const T, &'a T}
#[stable(feature = "rust1", since = "1.0.0")]
#[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(feature = "core", reason = "trait is experimental")]
#[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(&RangeFull).index_mut(index)
}
#[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(&RangeFull).index_mut(index)
}
#[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(&RangeFull).index_mut(index)
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<RangeFull> for IterMut<'a, T> {
- type Output = [T];
#[inline]
fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
make_slice!(T => &mut [T]: self.ptr, self.end)
}
}
+/// 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(feature = "core",
- reason = "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(feature = "core",
- reason = "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 })
}