Skip to content

Commit 3396034

Browse files
Make pop_ref and mut_pop_ref return Option instead of failing on empty vectors
1 parent d451c15 commit 3396034

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

src/libextra/ringbuf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
325325
&mut self.remaining1
326326
};
327327
self.nelts -= 1;
328-
Some(r.mut_pop_ref().get_mut_ref())
328+
Some(r.mut_pop_ref().unwrap().get_mut_ref())
329329
}
330330
}
331331

src/libstd/vec.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,14 +1017,15 @@ pub trait ImmutableVector<'a, T> {
10171017
* Equivalent to:
10181018
*
10191019
* ```
1020+
* if self.len() == 0 { return None; }
10201021
* let tail = &self[self.len() - 1];
10211022
* *self = self.slice_to(self.len() - 1);
1022-
* tail
1023+
* Some(tail)
10231024
* ```
10241025
*
1025-
* Fails if slice is empty.
1026+
* Returns `None` if slice is empty.
10261027
*/
1027-
fn pop_ref(&mut self) -> &'a T;
1028+
fn pop_ref(&mut self) -> Option<&'a T>;
10281029
}
10291030

10301031
impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
@@ -1191,10 +1192,11 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
11911192
}
11921193
}
11931194

1194-
fn pop_ref(&mut self) -> &'a T {
1195+
fn pop_ref(&mut self) -> Option<&'a T> {
1196+
if self.len() == 0 { return None; }
11951197
unsafe {
11961198
let s: &mut Slice<T> = cast::transmute(self);
1197-
&*raw::pop_ptr(s)
1199+
Some(&*raw::pop_ptr(s))
11981200
}
11991201
}
12001202
}
@@ -2077,14 +2079,15 @@ pub trait MutableVector<'a, T> {
20772079
* Equivalent to:
20782080
*
20792081
* ```
2082+
* if self.len() == 0 { return None; }
20802083
* let tail = &mut self[self.len() - 1];
20812084
* *self = self.mut_slice_to(self.len() - 1);
2082-
* tail
2085+
* Some(tail)
20832086
* ```
20842087
*
2085-
* Fails if slice is empty.
2088+
* Returns `None` if slice is empty.
20862089
*/
2087-
fn mut_pop_ref(&mut self) -> &'a mut T;
2090+
fn mut_pop_ref(&mut self) -> Option<&'a mut T>;
20882091

20892092
/// Swaps two elements in a vector.
20902093
///
@@ -2325,10 +2328,11 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
23252328
}
23262329
}
23272330

2328-
fn mut_pop_ref(&mut self) -> &'a mut T {
2331+
fn mut_pop_ref(&mut self) -> Option<&'a mut T> {
2332+
if self.len() == 0 { return None; }
23292333
unsafe {
23302334
let s: &mut Slice<T> = cast::transmute(self);
2331-
cast::transmute_mut(&*raw::pop_ptr(s))
2335+
Some(cast::transmute_mut(&*raw::pop_ptr(s)))
23322336
}
23332337
}
23342338

@@ -4211,17 +4215,13 @@ mod tests {
42114215
fn test_pop_ref() {
42124216
let mut x: &[int] = [1, 2, 3, 4, 5];
42134217
let h = x.pop_ref();
4214-
assert_eq!(*h, 5);
4218+
assert_eq!(*h.unwrap(), 5);
42154219
assert_eq!(x.len(), 4);
42164220
assert_eq!(x[0], 1);
42174221
assert_eq!(x[3], 4);
4218-
}
42194222

4220-
#[test]
4221-
#[should_fail]
4222-
fn test_pop_ref_empty() {
4223-
let mut x: &[int] = [];
4224-
x.pop_ref();
4223+
let mut y: &[int] = [];
4224+
assert!(y.pop_ref().is_none());
42254225
}
42264226

42274227
#[test]
@@ -4297,17 +4297,13 @@ mod tests {
42974297
fn test_mut_pop_ref() {
42984298
let mut x: &mut [int] = [1, 2, 3, 4, 5];
42994299
let h = x.mut_pop_ref();
4300-
assert_eq!(*h, 5);
4300+
assert_eq!(*h.unwrap(), 5);
43014301
assert_eq!(x.len(), 4);
43024302
assert_eq!(x[0], 1);
43034303
assert_eq!(x[3], 4);
4304-
}
43054304

4306-
#[test]
4307-
#[should_fail]
4308-
fn test_mut_pop_ref_empty() {
4309-
let mut x: &mut [int] = [];
4310-
x.mut_pop_ref();
4305+
let mut y: &mut [int] = [];
4306+
assert!(y.mut_pop_ref().is_none());
43114307
}
43124308
}
43134309

0 commit comments

Comments
 (0)