Skip to content

Commit d451c15

Browse files
Make shift_ref and mut_shift_ref return Option instead of failing
1 parent 3427137 commit d451c15

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
@@ -303,7 +303,7 @@ impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
303303
&mut self.remaining2
304304
};
305305
self.nelts -= 1;
306-
Some(r.mut_shift_ref().get_mut_ref())
306+
Some(r.mut_shift_ref().unwrap().get_mut_ref())
307307
}
308308

309309
#[inline]

src/libstd/vec.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -999,14 +999,15 @@ pub trait ImmutableVector<'a, T> {
999999
* Equivalent to:
10001000
*
10011001
* ```
1002+
* if self.len() == 0 { return None }
10021003
* let head = &self[0];
10031004
* *self = self.slice_from(1);
1004-
* head
1005+
* Some(head)
10051006
* ```
10061007
*
1007-
* Fails if slice is empty.
1008+
* Returns `None` if vector is empty
10081009
*/
1009-
fn shift_ref(&mut self) -> &'a T;
1010+
fn shift_ref(&mut self) -> Option<&'a T>;
10101011

10111012
/**
10121013
* Returns a mutable reference to the last element in this slice
@@ -1182,10 +1183,11 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
11821183
self.iter().map(f).collect()
11831184
}
11841185

1185-
fn shift_ref(&mut self) -> &'a T {
1186+
fn shift_ref(&mut self) -> Option<&'a T> {
1187+
if self.len() == 0 { return None; }
11861188
unsafe {
11871189
let s: &mut Slice<T> = cast::transmute(self);
1188-
&*raw::shift_ptr(s)
1190+
Some(&*raw::shift_ptr(s))
11891191
}
11901192
}
11911193

@@ -2057,14 +2059,15 @@ pub trait MutableVector<'a, T> {
20572059
* Equivalent to:
20582060
*
20592061
* ```
2062+
* if self.len() == 0 { return None; }
20602063
* let head = &mut self[0];
20612064
* *self = self.mut_slice_from(1);
2062-
* head
2065+
* Some(head)
20632066
* ```
20642067
*
2065-
* Fails if slice is empty.
2068+
* Returns `None` if slice is empty
20662069
*/
2067-
fn mut_shift_ref(&mut self) -> &'a mut T;
2070+
fn mut_shift_ref(&mut self) -> Option<&'a mut T>;
20682071

20692072
/**
20702073
* 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] {
23142317
MutChunks { v: self, chunk_size: chunk_size }
23152318
}
23162319

2317-
fn mut_shift_ref(&mut self) -> &'a mut T {
2320+
fn mut_shift_ref(&mut self) -> Option<&'a mut T> {
2321+
if self.len() == 0 { return None; }
23182322
unsafe {
23192323
let s: &mut Slice<T> = cast::transmute(self);
2320-
cast::transmute_mut(&*raw::shift_ptr(s))
2324+
Some(cast::transmute_mut(&*raw::shift_ptr(s)))
23212325
}
23222326
}
23232327

@@ -4194,17 +4198,13 @@ mod tests {
41944198
fn test_shift_ref() {
41954199
let mut x: &[int] = [1, 2, 3, 4, 5];
41964200
let h = x.shift_ref();
4197-
assert_eq!(*h, 1);
4201+
assert_eq!(*h.unwrap(), 1);
41984202
assert_eq!(x.len(), 4);
41994203
assert_eq!(x[0], 2);
42004204
assert_eq!(x[3], 5);
4201-
}
42024205

4203-
#[test]
4204-
#[should_fail]
4205-
fn test_shift_ref_empty() {
4206-
let mut x: &[int] = [];
4207-
x.shift_ref();
4206+
let mut y: &[int] = [];
4207+
assert_eq!(y.shift_ref(), None);
42084208
}
42094209

42104210
#[test]
@@ -4284,17 +4284,13 @@ mod tests {
42844284
fn test_mut_shift_ref() {
42854285
let mut x: &mut [int] = [1, 2, 3, 4, 5];
42864286
let h = x.mut_shift_ref();
4287-
assert_eq!(*h, 1);
4287+
assert_eq!(*h.unwrap(), 1);
42884288
assert_eq!(x.len(), 4);
42894289
assert_eq!(x[0], 2);
42904290
assert_eq!(x[3], 5);
4291-
}
42924291

4293-
#[test]
4294-
#[should_fail]
4295-
fn test_mut_shift_ref_empty() {
4296-
let mut x: &mut [int] = [];
4297-
x.mut_shift_ref();
4292+
let mut y: &mut [int] = [];
4293+
assert!(y.mut_shift_ref().is_none());
42984294
}
42994295

43004296
#[test]

0 commit comments

Comments
 (0)