From d451c150570a7da02314795abd6777c80f361e93 Mon Sep 17 00:00:00 2001 From: Nathaniel Herman Date: Sat, 25 Jan 2014 12:00:46 -0500 Subject: [PATCH 1/3] Make shift_ref and mut_shift_ref return Option instead of failing --- src/libextra/ringbuf.rs | 2 +- src/libstd/vec.rs | 44 +++++++++++++++++++---------------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/libextra/ringbuf.rs b/src/libextra/ringbuf.rs index ddef2f0a37a8f..4a6e2e1843cf7 100644 --- a/src/libextra/ringbuf.rs +++ b/src/libextra/ringbuf.rs @@ -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] diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 467bcf075f60c..aa02c48f46d1f 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -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 @@ -1182,10 +1183,11 @@ 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 = cast::transmute(self); - &*raw::shift_ptr(s) + Some(&*raw::shift_ptr(s)) } } @@ -2057,14 +2059,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 @@ -2314,10 +2317,11 @@ 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 = cast::transmute(self); - cast::transmute_mut(&*raw::shift_ptr(s)) + Some(cast::transmute_mut(&*raw::shift_ptr(s))) } } @@ -4194,17 +4198,13 @@ 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] @@ -4284,17 +4284,13 @@ 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] From 339603426e65896a06fcebf63f3d751f242ee820 Mon Sep 17 00:00:00 2001 From: Nathaniel Herman Date: Sat, 25 Jan 2014 15:33:31 -0500 Subject: [PATCH 2/3] Make pop_ref and mut_pop_ref return Option instead of failing on empty vectors --- src/libextra/ringbuf.rs | 2 +- src/libstd/vec.rs | 44 +++++++++++++++++++---------------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/libextra/ringbuf.rs b/src/libextra/ringbuf.rs index 4a6e2e1843cf7..a4c5038626232 100644 --- a/src/libextra/ringbuf.rs +++ b/src/libextra/ringbuf.rs @@ -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()) } } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index aa02c48f46d1f..81e3e6c7eda5f 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1017,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] { @@ -1191,10 +1192,11 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { } } - 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 = cast::transmute(self); - &*raw::pop_ptr(s) + Some(&*raw::pop_ptr(s)) } } } @@ -2077,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. /// @@ -2325,10 +2328,11 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { } } - 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 = cast::transmute(self); - cast::transmute_mut(&*raw::pop_ptr(s)) + Some(cast::transmute_mut(&*raw::pop_ptr(s))) } } @@ -4211,17 +4215,13 @@ mod tests { 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] @@ -4297,17 +4297,13 @@ mod tests { 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); - } - #[test] - #[should_fail] - fn test_mut_pop_ref_empty() { - let mut x: &mut [int] = []; - x.mut_pop_ref(); + let mut y: &mut [int] = []; + assert!(y.mut_pop_ref().is_none()); } } From d9fadbc04f5bbd520e4ce8665ac128288e9846c0 Mon Sep 17 00:00:00 2001 From: Nathaniel Herman Date: Sun, 26 Jan 2014 11:24:34 -0500 Subject: [PATCH 3/3] Make mut_last return Option instead of failing on empty vector (and add a test for mut_last) --- src/librustc/middle/trans/cleanup.rs | 2 +- src/libstd/vec.rs | 18 ++++++++++++++---- src/libsyntax/opt_vec.rs | 4 ++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/librustc/middle/trans/cleanup.rs b/src/librustc/middle/trans/cleanup.rs index 6e92ea9f11e51..299cf8f6aeeb4 100644 --- a/src/librustc/middle/trans/cleanup.rs +++ b/src/librustc/middle/trans/cleanup.rs @@ -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 => { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 81e3e6c7eda5f..0b9820e12ebc5 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -2031,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>; @@ -2298,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] @@ -4305,6 +4305,16 @@ mod tests { let mut y: &mut [int] = []; assert!(y.mut_pop_ref().is_none()); } + + #[test] + 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()); + } } #[cfg(test)] diff --git a/src/libsyntax/opt_vec.rs b/src/libsyntax/opt_vec.rs index ce3042cb9cde5..c575e170bef45 100644 --- a/src/libsyntax/opt_vec.rs +++ b/src/libsyntax/opt_vec.rs @@ -62,10 +62,10 @@ impl OptVec { } } - 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 } }