Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Option rather than fail for shift_ref, mut_shift_ref, pop_ref, mut_pop_ref, and mut_last #11944

Merged
merged 3 commits into from
Feb 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
&mut self.remaining2
};
self.nelts -= 1;
Some(r.mut_shift_ref().get_mut_ref())
Some(r.mut_shift_ref().unwrap().get_mut_ref())
}

#[inline]
Expand All @@ -325,7 +325,7 @@ impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
&mut self.remaining1
};
self.nelts -= 1;
Some(r.mut_pop_ref().get_mut_ref())
Some(r.mut_pop_ref().unwrap().get_mut_ref())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {
// Check if a landing pad block exists; if not, create one.
{
let mut scopes = self.scopes.borrow_mut();
let last_scope = scopes.get().mut_last();
let last_scope = scopes.get().mut_last().unwrap();
match last_scope.cached_landing_pad {
Some(llbb) => { return llbb; }
None => {
Expand Down
102 changes: 52 additions & 50 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,14 +999,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
Expand All @@ -1016,14 +1017,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] {
Expand Down Expand Up @@ -1182,17 +1184,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))
}
}
}
Expand Down Expand Up @@ -2027,7 +2031,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>;
Expand Down Expand Up @@ -2057,14 +2061,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
Expand All @@ -2074,14 +2079,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.
///
Expand Down Expand Up @@ -2292,10 +2298,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]
Expand All @@ -2314,17 +2320,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)))
}
}

Expand Down Expand Up @@ -4194,34 +4202,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]
Expand Down Expand Up @@ -4284,34 +4284,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());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/opt_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ impl<T> OptVec<T> {
}
}

pub fn mut_last<'a>(&'a mut self) -> &'a mut T {
pub fn mut_last<'a>(&'a mut self) -> Option<&'a mut T> {
match *self {
Vec(ref mut v) => v.mut_last(),
Empty => fail!("mut_last on empty opt_vec")
Empty => None
}
}

Expand Down