auto merge of #11944 : nathanielherman/rust/vec_opt, r=alexcrichton
Closes #11733
This commit is contained in:
commit
df044ea4ac
4 changed files with 57 additions and 55 deletions
|
@ -1000,14 +1000,15 @@ pub trait ImmutableVector<'a, T> {
|
|||
* Equivalent to:
|
||||
*
|
||||
* ```
|
||||
* if self.len() == 0 { return None }
|
||||
* let head = &self[0];
|
||||
* *self = self.slice_from(1);
|
||||
* head
|
||||
* Some(head)
|
||||
* ```
|
||||
*
|
||||
* Fails if slice is empty.
|
||||
* Returns `None` if vector is empty
|
||||
*/
|
||||
fn shift_ref(&mut self) -> &'a T;
|
||||
fn shift_ref(&mut self) -> Option<&'a T>;
|
||||
|
||||
/**
|
||||
* Returns a mutable reference to the last element in this slice
|
||||
|
@ -1017,14 +1018,15 @@ pub trait ImmutableVector<'a, T> {
|
|||
* Equivalent to:
|
||||
*
|
||||
* ```
|
||||
* if self.len() == 0 { return None; }
|
||||
* let tail = &self[self.len() - 1];
|
||||
* *self = self.slice_to(self.len() - 1);
|
||||
* tail
|
||||
* Some(tail)
|
||||
* ```
|
||||
*
|
||||
* Fails if slice is empty.
|
||||
* Returns `None` if slice is empty.
|
||||
*/
|
||||
fn pop_ref(&mut self) -> &'a T;
|
||||
fn pop_ref(&mut self) -> Option<&'a T>;
|
||||
}
|
||||
|
||||
impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
|
||||
|
@ -1183,17 +1185,19 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
|
|||
self.iter().map(f).collect()
|
||||
}
|
||||
|
||||
fn shift_ref(&mut self) -> &'a T {
|
||||
fn shift_ref(&mut self) -> Option<&'a T> {
|
||||
if self.len() == 0 { return None; }
|
||||
unsafe {
|
||||
let s: &mut Slice<T> = cast::transmute(self);
|
||||
&*raw::shift_ptr(s)
|
||||
Some(&*raw::shift_ptr(s))
|
||||
}
|
||||
}
|
||||
|
||||
fn pop_ref(&mut self) -> &'a T {
|
||||
fn pop_ref(&mut self) -> Option<&'a T> {
|
||||
if self.len() == 0 { return None; }
|
||||
unsafe {
|
||||
let s: &mut Slice<T> = cast::transmute(self);
|
||||
&*raw::pop_ptr(s)
|
||||
Some(&*raw::pop_ptr(s))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2028,7 +2032,7 @@ pub trait MutableVector<'a, T> {
|
|||
fn mut_iter(self) -> MutItems<'a, T>;
|
||||
|
||||
/// Returns a mutable pointer to the last item in the vector.
|
||||
fn mut_last(self) -> &'a mut T;
|
||||
fn mut_last(self) -> Option<&'a mut T>;
|
||||
|
||||
/// Returns a reversed iterator that allows modifying each value
|
||||
fn mut_rev_iter(self) -> RevMutItems<'a, T>;
|
||||
|
@ -2058,14 +2062,15 @@ pub trait MutableVector<'a, T> {
|
|||
* Equivalent to:
|
||||
*
|
||||
* ```
|
||||
* if self.len() == 0 { return None; }
|
||||
* let head = &mut self[0];
|
||||
* *self = self.mut_slice_from(1);
|
||||
* head
|
||||
* Some(head)
|
||||
* ```
|
||||
*
|
||||
* Fails if slice is empty.
|
||||
* Returns `None` if slice is empty
|
||||
*/
|
||||
fn mut_shift_ref(&mut self) -> &'a mut T;
|
||||
fn mut_shift_ref(&mut self) -> Option<&'a mut T>;
|
||||
|
||||
/**
|
||||
* Returns a mutable reference to the last element in this slice
|
||||
|
@ -2075,14 +2080,15 @@ pub trait MutableVector<'a, T> {
|
|||
* Equivalent to:
|
||||
*
|
||||
* ```
|
||||
* if self.len() == 0 { return None; }
|
||||
* let tail = &mut self[self.len() - 1];
|
||||
* *self = self.mut_slice_to(self.len() - 1);
|
||||
* tail
|
||||
* Some(tail)
|
||||
* ```
|
||||
*
|
||||
* Fails if slice is empty.
|
||||
* Returns `None` if slice is empty.
|
||||
*/
|
||||
fn mut_pop_ref(&mut self) -> &'a mut T;
|
||||
fn mut_pop_ref(&mut self) -> Option<&'a mut T>;
|
||||
|
||||
/// Swaps two elements in a vector.
|
||||
///
|
||||
|
@ -2293,10 +2299,10 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn mut_last(self) -> &'a mut T {
|
||||
fn mut_last(self) -> Option<&'a mut T> {
|
||||
let len = self.len();
|
||||
if len == 0 { fail!("mut_last: empty vector") }
|
||||
&mut self[len - 1]
|
||||
if len == 0 { return None; }
|
||||
Some(&mut self[len - 1])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -2315,17 +2321,19 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
|
|||
MutChunks { v: self, chunk_size: chunk_size }
|
||||
}
|
||||
|
||||
fn mut_shift_ref(&mut self) -> &'a mut T {
|
||||
fn mut_shift_ref(&mut self) -> Option<&'a mut T> {
|
||||
if self.len() == 0 { return None; }
|
||||
unsafe {
|
||||
let s: &mut Slice<T> = cast::transmute(self);
|
||||
cast::transmute_mut(&*raw::shift_ptr(s))
|
||||
Some(cast::transmute_mut(&*raw::shift_ptr(s)))
|
||||
}
|
||||
}
|
||||
|
||||
fn mut_pop_ref(&mut self) -> &'a mut T {
|
||||
fn mut_pop_ref(&mut self) -> Option<&'a mut T> {
|
||||
if self.len() == 0 { return None; }
|
||||
unsafe {
|
||||
let s: &mut Slice<T> = cast::transmute(self);
|
||||
cast::transmute_mut(&*raw::pop_ptr(s))
|
||||
Some(cast::transmute_mut(&*raw::pop_ptr(s)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4195,34 +4203,26 @@ mod tests {
|
|||
fn test_shift_ref() {
|
||||
let mut x: &[int] = [1, 2, 3, 4, 5];
|
||||
let h = x.shift_ref();
|
||||
assert_eq!(*h, 1);
|
||||
assert_eq!(*h.unwrap(), 1);
|
||||
assert_eq!(x.len(), 4);
|
||||
assert_eq!(x[0], 2);
|
||||
assert_eq!(x[3], 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_shift_ref_empty() {
|
||||
let mut x: &[int] = [];
|
||||
x.shift_ref();
|
||||
let mut y: &[int] = [];
|
||||
assert_eq!(y.shift_ref(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pop_ref() {
|
||||
let mut x: &[int] = [1, 2, 3, 4, 5];
|
||||
let h = x.pop_ref();
|
||||
assert_eq!(*h, 5);
|
||||
assert_eq!(*h.unwrap(), 5);
|
||||
assert_eq!(x.len(), 4);
|
||||
assert_eq!(x[0], 1);
|
||||
assert_eq!(x[3], 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_pop_ref_empty() {
|
||||
let mut x: &[int] = [];
|
||||
x.pop_ref();
|
||||
let mut y: &[int] = [];
|
||||
assert!(y.pop_ref().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -4285,34 +4285,36 @@ mod tests {
|
|||
fn test_mut_shift_ref() {
|
||||
let mut x: &mut [int] = [1, 2, 3, 4, 5];
|
||||
let h = x.mut_shift_ref();
|
||||
assert_eq!(*h, 1);
|
||||
assert_eq!(*h.unwrap(), 1);
|
||||
assert_eq!(x.len(), 4);
|
||||
assert_eq!(x[0], 2);
|
||||
assert_eq!(x[3], 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_mut_shift_ref_empty() {
|
||||
let mut x: &mut [int] = [];
|
||||
x.mut_shift_ref();
|
||||
let mut y: &mut [int] = [];
|
||||
assert!(y.mut_shift_ref().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_pop_ref() {
|
||||
let mut x: &mut [int] = [1, 2, 3, 4, 5];
|
||||
let h = x.mut_pop_ref();
|
||||
assert_eq!(*h, 5);
|
||||
assert_eq!(*h.unwrap(), 5);
|
||||
assert_eq!(x.len(), 4);
|
||||
assert_eq!(x[0], 1);
|
||||
assert_eq!(x[3], 4);
|
||||
|
||||
let mut y: &mut [int] = [];
|
||||
assert!(y.mut_pop_ref().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_mut_pop_ref_empty() {
|
||||
let mut x: &mut [int] = [];
|
||||
x.mut_pop_ref();
|
||||
fn test_mut_last() {
|
||||
let mut x = [1, 2, 3, 4, 5];
|
||||
let h = x.mut_last();
|
||||
assert_eq!(*h.unwrap(), 5);
|
||||
|
||||
let mut y: &mut [int] = [];
|
||||
assert!(y.mut_last().is_none());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue