diff options
| author | pravic <[email protected]> | 2016-04-29 21:16:15 +0300 |
|---|---|---|
| committer | pravic <[email protected]> | 2016-04-29 21:16:15 +0300 |
| commit | 77e9a3167b4aaadf3583a0c1d1ee0d9e63c9a000 (patch) | |
| tree | 710e445d56a1a582b8eff19b7b4b180276eae122 /libcore/slice.rs | |
| parent | tweak: /driver option specifies /fixed:no implicitly as well (diff) | |
| download | kmd-env-rs-77e9a3167b4aaadf3583a0c1d1ee0d9e63c9a000.tar.xz kmd-env-rs-77e9a3167b4aaadf3583a0c1d1ee0d9e63c9a000.zip | |
update libcore to 2016-04-29 nightly
Diffstat (limited to 'libcore/slice.rs')
| -rw-r--r-- | libcore/slice.rs | 118 |
1 files changed, 109 insertions, 9 deletions
diff --git a/libcore/slice.rs b/libcore/slice.rs index 25082ee..07f76ac 100644 --- a/libcore/slice.rs +++ b/libcore/slice.rs @@ -41,7 +41,7 @@ use default::Default; use fmt; use intrinsics::assume; use iter::*; -use ops::{FnMut, self, Index}; +use ops::{FnMut, self}; use ops::RangeFull; use option::Option; use option::Option::{None, Some}; @@ -50,10 +50,12 @@ use result::Result::{Ok, Err}; use ptr; use mem; use marker::{Copy, Send, Sync, self}; -use raw::Repr; -// Avoid conflicts with *both* the Slice trait (buggy) and the `slice::raw` module. -use raw::Slice as RawSlice; +#[repr(C)] +struct Repr<T> { + pub data: *const T, + pub len: usize, +} // // Extension traits @@ -152,9 +154,14 @@ pub trait SliceExt { fn ends_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq; #[stable(feature = "clone_from_slice", since = "1.7.0")] - fn clone_from_slice(&mut self, &[Self::Item]) where Self::Item: Clone; - #[unstable(feature = "copy_from_slice", issue = "31755")] + fn clone_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Clone; + #[stable(feature = "copy_from_slice", since = "1.9.0")] fn copy_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Copy; + + #[unstable(feature = "slice_binary_search_by_key", reason = "recently added", issue = "33018")] + fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize> + where F: FnMut(&Self::Item) -> B, + B: Ord; } // Use macros to be generic over const/mut @@ -317,7 +324,11 @@ impl<T> SliceExt for [T] { } #[inline] - fn len(&self) -> usize { self.repr().len } + fn len(&self) -> usize { + unsafe { + mem::transmute::<&[T], Repr<T>>(self).len + } + } #[inline] fn get_mut(&mut self, index: usize) -> Option<&mut T> { @@ -501,6 +512,14 @@ impl<T> SliceExt for [T] { src.as_ptr(), self.as_mut_ptr(), self.len()); } } + + #[inline] + fn binary_search_by_key<B, F>(&self, b: &B, mut f: F) -> Result<usize, usize> + where F: FnMut(&Self::Item) -> B, + B: Ord + { + self.binary_search_by(|k| f(k).cmp(b)) + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -871,6 +890,20 @@ macro_rules! make_mut_slice { } /// Immutable slice iterator +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// // First, we declare a type which has `iter` method to get the `Iter` struct (&[usize here]): +/// let slice = &[1, 2, 3]; +/// +/// // Then, we iterate over it: +/// for element in slice.iter() { +/// println!("{}", element); +/// } +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, T: 'a> { ptr: *const T, @@ -897,6 +930,26 @@ impl<'a, T> Iter<'a, T> { /// /// This has the same lifetime as the original slice, and so the /// iterator can continue to be used while this exists. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// // First, we declare a type which has the `iter` method to get the `Iter` + /// // struct (&[usize here]): + /// let slice = &[1, 2, 3]; + /// + /// // Then, we get the iterator: + /// let mut iter = slice.iter(); + /// // So if we print what `as_slice` method returns here, we have "[1, 2, 3]": + /// println!("{:?}", iter.as_slice()); + /// + /// // Next, we move to the second element of the slice: + /// iter.next(); + /// // Now `as_slice` returns "[2, 3]": + /// println!("{:?}", iter.as_slice()); + /// ``` #[stable(feature = "iter_to_slice", since = "1.4.0")] pub fn as_slice(&self) -> &'a [T] { make_slice!(self.ptr, self.end) @@ -928,6 +981,24 @@ impl<'a, T> Clone for Iter<'a, T> { } /// Mutable slice iterator. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// // First, we declare a type which has `iter_mut` method to get the `IterMut` +/// // struct (&[usize here]): +/// let mut slice = &mut [1, 2, 3]; +/// +/// // Then, we iterate over it and increment each element value: +/// for element in slice.iter_mut() { +/// *element += 1; +/// } +/// +/// // We now have "[2, 3, 4]": +/// println!("{:?}", slice); +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub struct IterMut<'a, T: 'a> { ptr: *mut T, @@ -956,6 +1027,35 @@ impl<'a, T> IterMut<'a, T> { /// to consume the iterator. Consider using the `Slice` and /// `SliceMut` implementations for obtaining slices with more /// restricted lifetimes that do not consume the iterator. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// // First, we declare a type which has `iter_mut` method to get the `IterMut` + /// // struct (&[usize here]): + /// let mut slice = &mut [1, 2, 3]; + /// + /// { + /// // Then, we get the iterator: + /// let mut iter = slice.iter_mut(); + /// // We move to next element: + /// iter.next(); + /// // So if we print what `into_slice` method returns here, we have "[2, 3]": + /// println!("{:?}", iter.into_slice()); + /// } + /// + /// // Now let's modify a value of the slice: + /// { + /// // First we get back the iterator: + /// let mut iter = slice.iter_mut(); + /// // We change the value of the first element of the slice returned by the `next` method: + /// *iter.next().unwrap() += 1; + /// } + /// // Now slice is "[2, 2, 3]": + /// println!("{:?}", slice); + /// ``` #[stable(feature = "iter_to_slice", since = "1.4.0")] pub fn into_slice(self) -> &'a mut [T] { make_mut_slice!(self.ptr, self.end) @@ -1614,7 +1714,7 @@ impl<'a, T> ExactSizeIterator for ChunksMut<'a, T> {} #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn from_raw_parts<'a, T>(p: *const T, len: usize) -> &'a [T] { - mem::transmute(RawSlice { data: p, len: len }) + mem::transmute(Repr { data: p, len: len }) } /// Performs the same functionality as `from_raw_parts`, except that a mutable @@ -1626,7 +1726,7 @@ pub unsafe fn from_raw_parts<'a, T>(p: *const T, len: usize) -> &'a [T] { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn from_raw_parts_mut<'a, T>(p: *mut T, len: usize) -> &'a mut [T] { - mem::transmute(RawSlice { data: p, len: len }) + mem::transmute(Repr { data: p, len: len }) } // |