Skip to content

Commit d9fadbc

Browse files
Make mut_last return Option instead of failing on empty vector (and add a test for mut_last)
1 parent 3396034 commit d9fadbc

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/librustc/middle/trans/cleanup.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {
662662
// Check if a landing pad block exists; if not, create one.
663663
{
664664
let mut scopes = self.scopes.borrow_mut();
665-
let last_scope = scopes.get().mut_last();
665+
let last_scope = scopes.get().mut_last().unwrap();
666666
match last_scope.cached_landing_pad {
667667
Some(llbb) => { return llbb; }
668668
None => {

src/libstd/vec.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ pub trait MutableVector<'a, T> {
20312031
fn mut_iter(self) -> MutItems<'a, T>;
20322032

20332033
/// Returns a mutable pointer to the last item in the vector.
2034-
fn mut_last(self) -> &'a mut T;
2034+
fn mut_last(self) -> Option<&'a mut T>;
20352035

20362036
/// Returns a reversed iterator that allows modifying each value
20372037
fn mut_rev_iter(self) -> RevMutItems<'a, T>;
@@ -2298,10 +2298,10 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
22982298
}
22992299

23002300
#[inline]
2301-
fn mut_last(self) -> &'a mut T {
2301+
fn mut_last(self) -> Option<&'a mut T> {
23022302
let len = self.len();
2303-
if len == 0 { fail!("mut_last: empty vector") }
2304-
&mut self[len - 1]
2303+
if len == 0 { return None; }
2304+
Some(&mut self[len - 1])
23052305
}
23062306

23072307
#[inline]
@@ -4305,6 +4305,16 @@ mod tests {
43054305
let mut y: &mut [int] = [];
43064306
assert!(y.mut_pop_ref().is_none());
43074307
}
4308+
4309+
#[test]
4310+
fn test_mut_last() {
4311+
let mut x = [1, 2, 3, 4, 5];
4312+
let h = x.mut_last();
4313+
assert_eq!(*h.unwrap(), 5);
4314+
4315+
let mut y: &mut [int] = [];
4316+
assert!(y.mut_last().is_none());
4317+
}
43084318
}
43094319

43104320
#[cfg(test)]

src/libsyntax/opt_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ impl<T> OptVec<T> {
6262
}
6363
}
6464

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

0 commit comments

Comments
 (0)