]> git.lizzy.rs Git - rust.git/blobdiff - src/libcore/slice.rs
make `IndexMut` a super trait over `Index`
[rust.git] / src / libcore / slice.rs
index b1e9084d210f5c77d5f6258c012ecae0e5b64ce1..fc51920ec6b82de1941863fbb4a48849f69d709e 100644 (file)
@@ -41,9 +41,9 @@
 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;
@@ -502,8 +502,6 @@ fn index(&self, &index: &uint) -> &T {
 
 #[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());
 
@@ -543,17 +541,16 @@ fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
     }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T> ops::Index<ops::FullRange> for [T] {
+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(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);
@@ -568,7 +565,6 @@ fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
 }
 #[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 })
@@ -576,7 +572,6 @@ fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
 }
 #[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();
@@ -584,10 +579,9 @@ fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
     }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T> ops::IndexMut<ops::FullRange> for [T] {
-    type Output = [T];
+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
     }
 }
@@ -633,6 +627,22 @@ 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) => {
@@ -751,10 +761,10 @@ fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
 }
 
 #[unstable(feature = "core")]
-impl<'a, T> ops::Index<ops::FullRange> for Iter<'a, T> {
+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()
     }
 }
@@ -770,8 +780,6 @@ pub fn as_slice(&self) -> &'a [T] {
     }
 }
 
-impl<'a,T> Copy for Iter<'a,T> {}
-
 iterator!{struct Iter -> *const T, &'a T}
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -779,7 +787,7 @@ impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
 
 #[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")]
@@ -821,7 +829,7 @@ 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(feature = "core")]
@@ -829,7 +837,7 @@ 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(feature = "core")]
@@ -837,47 +845,43 @@ 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(feature = "core")]
-impl<'a, T> ops::Index<ops::FullRange> for IterMut<'a, T> {
+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(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(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(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(feature = "core")]
-impl<'a, T> ops::IndexMut<ops::FullRange> for IterMut<'a, T> {
-    type Output = [T];
+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)
     }
 }
@@ -1343,6 +1347,52 @@ pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] {
     }
 }
 
+/// 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
@@ -1369,8 +1419,9 @@ pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] {
 /// }
 /// ```
 #[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 })
 }
@@ -1382,8 +1433,9 @@ pub unsafe fn from_raw_buf<'a, T>(p: &'a *const T, len: uint) -> &'a [T] {
 /// 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 })
 }