@@ -999,14 +999,15 @@ pub trait ImmutableVector<'a, T> {
999
999
* Equivalent to:
1000
1000
*
1001
1001
* ```
1002
+ * if self.len() == 0 { return None }
1002
1003
* let head = &self[0];
1003
1004
* *self = self.slice_from(1);
1004
- * head
1005
+ * Some( head)
1005
1006
* ```
1006
1007
*
1007
- * Fails if slice is empty.
1008
+ * Returns `None` if vector is empty
1008
1009
*/
1009
- fn shift_ref ( & mut self ) -> & ' a T ;
1010
+ fn shift_ref ( & mut self ) -> Option < & ' a T > ;
1010
1011
1011
1012
/**
1012
1013
* Returns a mutable reference to the last element in this slice
@@ -1182,10 +1183,11 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
1182
1183
self . iter ( ) . map ( f) . collect ( )
1183
1184
}
1184
1185
1185
- fn shift_ref ( & mut self ) -> & ' a T {
1186
+ fn shift_ref ( & mut self ) -> Option < & ' a T > {
1187
+ if self . len ( ) == 0 { return None ; }
1186
1188
unsafe {
1187
1189
let s: & mut Slice < T > = cast:: transmute ( self ) ;
1188
- & * raw:: shift_ptr ( s)
1190
+ Some ( & * raw:: shift_ptr ( s) )
1189
1191
}
1190
1192
}
1191
1193
@@ -2057,14 +2059,15 @@ pub trait MutableVector<'a, T> {
2057
2059
* Equivalent to:
2058
2060
*
2059
2061
* ```
2062
+ * if self.len() == 0 { return None; }
2060
2063
* let head = &mut self[0];
2061
2064
* *self = self.mut_slice_from(1);
2062
- * head
2065
+ * Some( head)
2063
2066
* ```
2064
2067
*
2065
- * Fails if slice is empty.
2068
+ * Returns `None` if slice is empty
2066
2069
*/
2067
- fn mut_shift_ref ( & mut self ) -> & ' a mut T ;
2070
+ fn mut_shift_ref ( & mut self ) -> Option < & ' a mut T > ;
2068
2071
2069
2072
/**
2070
2073
* 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] {
2314
2317
MutChunks { v : self , chunk_size : chunk_size }
2315
2318
}
2316
2319
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 ; }
2318
2322
unsafe {
2319
2323
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) ) )
2321
2325
}
2322
2326
}
2323
2327
@@ -4194,17 +4198,13 @@ mod tests {
4194
4198
fn test_shift_ref ( ) {
4195
4199
let mut x: & [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4196
4200
let h = x. shift_ref ( ) ;
4197
- assert_eq ! ( * h, 1 ) ;
4201
+ assert_eq ! ( * h. unwrap ( ) , 1 ) ;
4198
4202
assert_eq ! ( x. len( ) , 4 ) ;
4199
4203
assert_eq ! ( x[ 0 ] , 2 ) ;
4200
4204
assert_eq ! ( x[ 3 ] , 5 ) ;
4201
- }
4202
4205
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 ) ;
4208
4208
}
4209
4209
4210
4210
#[ test]
@@ -4284,17 +4284,13 @@ mod tests {
4284
4284
fn test_mut_shift_ref ( ) {
4285
4285
let mut x: & mut [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4286
4286
let h = x. mut_shift_ref ( ) ;
4287
- assert_eq ! ( * h, 1 ) ;
4287
+ assert_eq ! ( * h. unwrap ( ) , 1 ) ;
4288
4288
assert_eq ! ( x. len( ) , 4 ) ;
4289
4289
assert_eq ! ( x[ 0 ] , 2 ) ;
4290
4290
assert_eq ! ( x[ 3 ] , 5 ) ;
4291
- }
4292
4291
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( ) ) ;
4298
4294
}
4299
4295
4300
4296
#[ test]
0 commit comments