@@ -1017,14 +1017,15 @@ pub trait ImmutableVector<'a, T> {
1017
1017
* Equivalent to:
1018
1018
*
1019
1019
* ```
1020
+ * if self.len() == 0 { return None; }
1020
1021
* let tail = &self[self.len() - 1];
1021
1022
* *self = self.slice_to(self.len() - 1);
1022
- * tail
1023
+ * Some( tail)
1023
1024
* ```
1024
1025
*
1025
- * Fails if slice is empty.
1026
+ * Returns `None` if slice is empty.
1026
1027
*/
1027
- fn pop_ref ( & mut self ) -> & ' a T ;
1028
+ fn pop_ref ( & mut self ) -> Option < & ' a T > ;
1028
1029
}
1029
1030
1030
1031
impl < ' a , T > ImmutableVector < ' a , T > for & ' a [ T ] {
@@ -1191,10 +1192,11 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
1191
1192
}
1192
1193
}
1193
1194
1194
- fn pop_ref ( & mut self ) -> & ' a T {
1195
+ fn pop_ref ( & mut self ) -> Option < & ' a T > {
1196
+ if self . len ( ) == 0 { return None ; }
1195
1197
unsafe {
1196
1198
let s: & mut Slice < T > = cast:: transmute ( self ) ;
1197
- & * raw:: pop_ptr ( s)
1199
+ Some ( & * raw:: pop_ptr ( s) )
1198
1200
}
1199
1201
}
1200
1202
}
@@ -2077,14 +2079,15 @@ pub trait MutableVector<'a, T> {
2077
2079
* Equivalent to:
2078
2080
*
2079
2081
* ```
2082
+ * if self.len() == 0 { return None; }
2080
2083
* let tail = &mut self[self.len() - 1];
2081
2084
* *self = self.mut_slice_to(self.len() - 1);
2082
- * tail
2085
+ * Some( tail)
2083
2086
* ```
2084
2087
*
2085
- * Fails if slice is empty.
2088
+ * Returns `None` if slice is empty.
2086
2089
*/
2087
- fn mut_pop_ref ( & mut self ) -> & ' a mut T ;
2090
+ fn mut_pop_ref ( & mut self ) -> Option < & ' a mut T > ;
2088
2091
2089
2092
/// Swaps two elements in a vector.
2090
2093
///
@@ -2325,10 +2328,11 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
2325
2328
}
2326
2329
}
2327
2330
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 ; }
2329
2333
unsafe {
2330
2334
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) ) )
2332
2336
}
2333
2337
}
2334
2338
@@ -4211,17 +4215,13 @@ mod tests {
4211
4215
fn test_pop_ref ( ) {
4212
4216
let mut x: & [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4213
4217
let h = x. pop_ref ( ) ;
4214
- assert_eq ! ( * h, 5 ) ;
4218
+ assert_eq ! ( * h. unwrap ( ) , 5 ) ;
4215
4219
assert_eq ! ( x. len( ) , 4 ) ;
4216
4220
assert_eq ! ( x[ 0 ] , 1 ) ;
4217
4221
assert_eq ! ( x[ 3 ] , 4 ) ;
4218
- }
4219
4222
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( ) ) ;
4225
4225
}
4226
4226
4227
4227
#[ test]
@@ -4297,17 +4297,13 @@ mod tests {
4297
4297
fn test_mut_pop_ref ( ) {
4298
4298
let mut x: & mut [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4299
4299
let h = x. mut_pop_ref ( ) ;
4300
- assert_eq ! ( * h, 5 ) ;
4300
+ assert_eq ! ( * h. unwrap ( ) , 5 ) ;
4301
4301
assert_eq ! ( x. len( ) , 4 ) ;
4302
4302
assert_eq ! ( x[ 0 ] , 1 ) ;
4303
4303
assert_eq ! ( x[ 3 ] , 4 ) ;
4304
- }
4305
4304
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( ) ) ;
4311
4307
}
4312
4308
}
4313
4309
0 commit comments