@@ -1000,14 +1000,15 @@ pub trait ImmutableVector<'a, T> {
10001000 * Equivalent to:
10011001 *
10021002 * ```
1003+ * if self.len() == 0 { return None }
10031004 * let head = &self[0];
10041005 * *self = self.slice_from(1);
1005- * head
1006+ * Some( head)
10061007 * ```
10071008 *
1008- * Fails if slice is empty.
1009+ * Returns `None` if vector is empty
10091010 */
1010- fn shift_ref ( & mut self ) -> & ' a T ;
1011+ fn shift_ref ( & mut self ) -> Option < & ' a T > ;
10111012
10121013 /**
10131014 * Returns a mutable reference to the last element in this slice
@@ -1017,14 +1018,15 @@ pub trait ImmutableVector<'a, T> {
10171018 * Equivalent to:
10181019 *
10191020 * ```
1021+ * if self.len() == 0 { return None; }
10201022 * let tail = &self[self.len() - 1];
10211023 * *self = self.slice_to(self.len() - 1);
1022- * tail
1024+ * Some( tail)
10231025 * ```
10241026 *
1025- * Fails if slice is empty.
1027+ * Returns `None` if slice is empty.
10261028 */
1027- fn pop_ref ( & mut self ) -> & ' a T ;
1029+ fn pop_ref ( & mut self ) -> Option < & ' a T > ;
10281030}
10291031
10301032impl < ' a , T > ImmutableVector < ' a , T > for & ' a [ T ] {
@@ -1183,17 +1185,19 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
11831185 self . iter ( ) . map ( f) . collect ( )
11841186 }
11851187
1186- fn shift_ref ( & mut self ) -> & ' a T {
1188+ fn shift_ref ( & mut self ) -> Option < & ' a T > {
1189+ if self . len ( ) == 0 { return None ; }
11871190 unsafe {
11881191 let s: & mut Slice < T > = cast:: transmute ( self ) ;
1189- & * raw:: shift_ptr ( s)
1192+ Some ( & * raw:: shift_ptr ( s) )
11901193 }
11911194 }
11921195
1193- fn pop_ref ( & mut self ) -> & ' a T {
1196+ fn pop_ref ( & mut self ) -> Option < & ' a T > {
1197+ if self . len ( ) == 0 { return None ; }
11941198 unsafe {
11951199 let s: & mut Slice < T > = cast:: transmute ( self ) ;
1196- & * raw:: pop_ptr ( s)
1200+ Some ( & * raw:: pop_ptr ( s) )
11971201 }
11981202 }
11991203}
@@ -2028,7 +2032,7 @@ pub trait MutableVector<'a, T> {
20282032 fn mut_iter ( self ) -> MutItems < ' a , T > ;
20292033
20302034 /// Returns a mutable pointer to the last item in the vector.
2031- fn mut_last ( self ) -> & ' a mut T ;
2035+ fn mut_last ( self ) -> Option < & ' a mut T > ;
20322036
20332037 /// Returns a reversed iterator that allows modifying each value
20342038 fn mut_rev_iter ( self ) -> RevMutItems < ' a , T > ;
@@ -2058,14 +2062,15 @@ pub trait MutableVector<'a, T> {
20582062 * Equivalent to:
20592063 *
20602064 * ```
2065+ * if self.len() == 0 { return None; }
20612066 * let head = &mut self[0];
20622067 * *self = self.mut_slice_from(1);
2063- * head
2068+ * Some( head)
20642069 * ```
20652070 *
2066- * Fails if slice is empty.
2071+ * Returns `None` if slice is empty
20672072 */
2068- fn mut_shift_ref ( & mut self ) -> & ' a mut T ;
2073+ fn mut_shift_ref ( & mut self ) -> Option < & ' a mut T > ;
20692074
20702075 /**
20712076 * Returns a mutable reference to the last element in this slice
@@ -2075,14 +2080,15 @@ pub trait MutableVector<'a, T> {
20752080 * Equivalent to:
20762081 *
20772082 * ```
2083+ * if self.len() == 0 { return None; }
20782084 * let tail = &mut self[self.len() - 1];
20792085 * *self = self.mut_slice_to(self.len() - 1);
2080- * tail
2086+ * Some( tail)
20812087 * ```
20822088 *
2083- * Fails if slice is empty.
2089+ * Returns `None` if slice is empty.
20842090 */
2085- fn mut_pop_ref ( & mut self ) -> & ' a mut T ;
2091+ fn mut_pop_ref ( & mut self ) -> Option < & ' a mut T > ;
20862092
20872093 /// Swaps two elements in a vector.
20882094 ///
@@ -2293,10 +2299,10 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
22932299 }
22942300
22952301 #[ inline]
2296- fn mut_last ( self ) -> & ' a mut T {
2302+ fn mut_last ( self ) -> Option < & ' a mut T > {
22972303 let len = self . len ( ) ;
2298- if len == 0 { fail ! ( "mut_last: empty vector" ) }
2299- & mut self [ len - 1 ]
2304+ if len == 0 { return None ; }
2305+ Some ( & mut self [ len - 1 ] )
23002306 }
23012307
23022308 #[ inline]
@@ -2315,17 +2321,19 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
23152321 MutChunks { v : self , chunk_size : chunk_size }
23162322 }
23172323
2318- fn mut_shift_ref ( & mut self ) -> & ' a mut T {
2324+ fn mut_shift_ref ( & mut self ) -> Option < & ' a mut T > {
2325+ if self . len ( ) == 0 { return None ; }
23192326 unsafe {
23202327 let s: & mut Slice < T > = cast:: transmute ( self ) ;
2321- cast:: transmute_mut ( & * raw:: shift_ptr ( s) )
2328+ Some ( cast:: transmute_mut ( & * raw:: shift_ptr ( s) ) )
23222329 }
23232330 }
23242331
2325- fn mut_pop_ref ( & mut self ) -> & ' a mut T {
2332+ fn mut_pop_ref ( & mut self ) -> Option < & ' a mut T > {
2333+ if self . len ( ) == 0 { return None ; }
23262334 unsafe {
23272335 let s: & mut Slice < T > = cast:: transmute ( self ) ;
2328- cast:: transmute_mut ( & * raw:: pop_ptr ( s) )
2336+ Some ( cast:: transmute_mut ( & * raw:: pop_ptr ( s) ) )
23292337 }
23302338 }
23312339
@@ -4195,34 +4203,26 @@ mod tests {
41954203 fn test_shift_ref ( ) {
41964204 let mut x: & [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
41974205 let h = x. shift_ref ( ) ;
4198- assert_eq ! ( * h, 1 ) ;
4206+ assert_eq ! ( * h. unwrap ( ) , 1 ) ;
41994207 assert_eq ! ( x. len( ) , 4 ) ;
42004208 assert_eq ! ( x[ 0 ] , 2 ) ;
42014209 assert_eq ! ( x[ 3 ] , 5 ) ;
4202- }
42034210
4204- #[ test]
4205- #[ should_fail]
4206- fn test_shift_ref_empty ( ) {
4207- let mut x: & [ int ] = [ ] ;
4208- x. shift_ref ( ) ;
4211+ let mut y: & [ int ] = [ ] ;
4212+ assert_eq ! ( y. shift_ref( ) , None ) ;
42094213 }
42104214
42114215 #[ test]
42124216 fn test_pop_ref ( ) {
42134217 let mut x: & [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
42144218 let h = x. pop_ref ( ) ;
4215- assert_eq ! ( * h, 5 ) ;
4219+ assert_eq ! ( * h. unwrap ( ) , 5 ) ;
42164220 assert_eq ! ( x. len( ) , 4 ) ;
42174221 assert_eq ! ( x[ 0 ] , 1 ) ;
42184222 assert_eq ! ( x[ 3 ] , 4 ) ;
4219- }
42204223
4221- #[ test]
4222- #[ should_fail]
4223- fn test_pop_ref_empty ( ) {
4224- let mut x: & [ int ] = [ ] ;
4225- x. pop_ref ( ) ;
4224+ let mut y: & [ int ] = [ ] ;
4225+ assert ! ( y. pop_ref( ) . is_none( ) ) ;
42264226 }
42274227
42284228 #[ test]
@@ -4285,34 +4285,36 @@ mod tests {
42854285 fn test_mut_shift_ref ( ) {
42864286 let mut x: & mut [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
42874287 let h = x. mut_shift_ref ( ) ;
4288- assert_eq ! ( * h, 1 ) ;
4288+ assert_eq ! ( * h. unwrap ( ) , 1 ) ;
42894289 assert_eq ! ( x. len( ) , 4 ) ;
42904290 assert_eq ! ( x[ 0 ] , 2 ) ;
42914291 assert_eq ! ( x[ 3 ] , 5 ) ;
4292- }
42934292
4294- #[ test]
4295- #[ should_fail]
4296- fn test_mut_shift_ref_empty ( ) {
4297- let mut x: & mut [ int ] = [ ] ;
4298- x. mut_shift_ref ( ) ;
4293+ let mut y: & mut [ int ] = [ ] ;
4294+ assert ! ( y. mut_shift_ref( ) . is_none( ) ) ;
42994295 }
43004296
43014297 #[ test]
43024298 fn test_mut_pop_ref ( ) {
43034299 let mut x: & mut [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
43044300 let h = x. mut_pop_ref ( ) ;
4305- assert_eq ! ( * h, 5 ) ;
4301+ assert_eq ! ( * h. unwrap ( ) , 5 ) ;
43064302 assert_eq ! ( x. len( ) , 4 ) ;
43074303 assert_eq ! ( x[ 0 ] , 1 ) ;
43084304 assert_eq ! ( x[ 3 ] , 4 ) ;
4305+
4306+ let mut y: & mut [ int ] = [ ] ;
4307+ assert ! ( y. mut_pop_ref( ) . is_none( ) ) ;
43094308 }
43104309
43114310 #[ test]
4312- #[ should_fail]
4313- fn test_mut_pop_ref_empty ( ) {
4314- let mut x: & mut [ int ] = [ ] ;
4315- x. mut_pop_ref ( ) ;
4311+ fn test_mut_last ( ) {
4312+ let mut x = [ 1 , 2 , 3 , 4 , 5 ] ;
4313+ let h = x. mut_last ( ) ;
4314+ assert_eq ! ( * h. unwrap( ) , 5 ) ;
4315+
4316+ let mut y: & mut [ int ] = [ ] ;
4317+ assert ! ( y. mut_last( ) . is_none( ) ) ;
43164318 }
43174319}
43184320
0 commit comments