@@ -1000,14 +1000,15 @@ pub trait ImmutableVector<'a, T> {
1000
1000
* Equivalent to:
1001
1001
*
1002
1002
* ```
1003
+ * if self.len() == 0 { return None }
1003
1004
* let head = &self[0];
1004
1005
* *self = self.slice_from(1);
1005
- * head
1006
+ * Some( head)
1006
1007
* ```
1007
1008
*
1008
- * Fails if slice is empty.
1009
+ * Returns `None` if vector is empty
1009
1010
*/
1010
- fn shift_ref ( & mut self ) -> & ' a T ;
1011
+ fn shift_ref ( & mut self ) -> Option < & ' a T > ;
1011
1012
1012
1013
/**
1013
1014
* Returns a mutable reference to the last element in this slice
@@ -1017,14 +1018,15 @@ pub trait ImmutableVector<'a, T> {
1017
1018
* Equivalent to:
1018
1019
*
1019
1020
* ```
1021
+ * if self.len() == 0 { return None; }
1020
1022
* let tail = &self[self.len() - 1];
1021
1023
* *self = self.slice_to(self.len() - 1);
1022
- * tail
1024
+ * Some( tail)
1023
1025
* ```
1024
1026
*
1025
- * Fails if slice is empty.
1027
+ * Returns `None` if slice is empty.
1026
1028
*/
1027
- fn pop_ref ( & mut self ) -> & ' a T ;
1029
+ fn pop_ref ( & mut self ) -> Option < & ' a T > ;
1028
1030
}
1029
1031
1030
1032
impl < ' a , T > ImmutableVector < ' a , T > for & ' a [ T ] {
@@ -1183,17 +1185,19 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
1183
1185
self . iter ( ) . map ( f) . collect ( )
1184
1186
}
1185
1187
1186
- fn shift_ref ( & mut self ) -> & ' a T {
1188
+ fn shift_ref ( & mut self ) -> Option < & ' a T > {
1189
+ if self . len ( ) == 0 { return None ; }
1187
1190
unsafe {
1188
1191
let s: & mut Slice < T > = cast:: transmute ( self ) ;
1189
- & * raw:: shift_ptr ( s)
1192
+ Some ( & * raw:: shift_ptr ( s) )
1190
1193
}
1191
1194
}
1192
1195
1193
- fn pop_ref ( & mut self ) -> & ' a T {
1196
+ fn pop_ref ( & mut self ) -> Option < & ' a T > {
1197
+ if self . len ( ) == 0 { return None ; }
1194
1198
unsafe {
1195
1199
let s: & mut Slice < T > = cast:: transmute ( self ) ;
1196
- & * raw:: pop_ptr ( s)
1200
+ Some ( & * raw:: pop_ptr ( s) )
1197
1201
}
1198
1202
}
1199
1203
}
@@ -2028,7 +2032,7 @@ pub trait MutableVector<'a, T> {
2028
2032
fn mut_iter ( self ) -> MutItems < ' a , T > ;
2029
2033
2030
2034
/// 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 > ;
2032
2036
2033
2037
/// Returns a reversed iterator that allows modifying each value
2034
2038
fn mut_rev_iter ( self ) -> RevMutItems < ' a , T > ;
@@ -2058,14 +2062,15 @@ pub trait MutableVector<'a, T> {
2058
2062
* Equivalent to:
2059
2063
*
2060
2064
* ```
2065
+ * if self.len() == 0 { return None; }
2061
2066
* let head = &mut self[0];
2062
2067
* *self = self.mut_slice_from(1);
2063
- * head
2068
+ * Some( head)
2064
2069
* ```
2065
2070
*
2066
- * Fails if slice is empty.
2071
+ * Returns `None` if slice is empty
2067
2072
*/
2068
- fn mut_shift_ref ( & mut self ) -> & ' a mut T ;
2073
+ fn mut_shift_ref ( & mut self ) -> Option < & ' a mut T > ;
2069
2074
2070
2075
/**
2071
2076
* Returns a mutable reference to the last element in this slice
@@ -2075,14 +2080,15 @@ pub trait MutableVector<'a, T> {
2075
2080
* Equivalent to:
2076
2081
*
2077
2082
* ```
2083
+ * if self.len() == 0 { return None; }
2078
2084
* let tail = &mut self[self.len() - 1];
2079
2085
* *self = self.mut_slice_to(self.len() - 1);
2080
- * tail
2086
+ * Some( tail)
2081
2087
* ```
2082
2088
*
2083
- * Fails if slice is empty.
2089
+ * Returns `None` if slice is empty.
2084
2090
*/
2085
- fn mut_pop_ref ( & mut self ) -> & ' a mut T ;
2091
+ fn mut_pop_ref ( & mut self ) -> Option < & ' a mut T > ;
2086
2092
2087
2093
/// Swaps two elements in a vector.
2088
2094
///
@@ -2293,10 +2299,10 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
2293
2299
}
2294
2300
2295
2301
#[ inline]
2296
- fn mut_last ( self ) -> & ' a mut T {
2302
+ fn mut_last ( self ) -> Option < & ' a mut T > {
2297
2303
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 ] )
2300
2306
}
2301
2307
2302
2308
#[ inline]
@@ -2315,17 +2321,19 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
2315
2321
MutChunks { v : self , chunk_size : chunk_size }
2316
2322
}
2317
2323
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 ; }
2319
2326
unsafe {
2320
2327
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) ) )
2322
2329
}
2323
2330
}
2324
2331
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 ; }
2326
2334
unsafe {
2327
2335
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) ) )
2329
2337
}
2330
2338
}
2331
2339
@@ -4195,34 +4203,26 @@ mod tests {
4195
4203
fn test_shift_ref ( ) {
4196
4204
let mut x: & [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4197
4205
let h = x. shift_ref ( ) ;
4198
- assert_eq ! ( * h, 1 ) ;
4206
+ assert_eq ! ( * h. unwrap ( ) , 1 ) ;
4199
4207
assert_eq ! ( x. len( ) , 4 ) ;
4200
4208
assert_eq ! ( x[ 0 ] , 2 ) ;
4201
4209
assert_eq ! ( x[ 3 ] , 5 ) ;
4202
- }
4203
4210
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 ) ;
4209
4213
}
4210
4214
4211
4215
#[ test]
4212
4216
fn test_pop_ref ( ) {
4213
4217
let mut x: & [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4214
4218
let h = x. pop_ref ( ) ;
4215
- assert_eq ! ( * h, 5 ) ;
4219
+ assert_eq ! ( * h. unwrap ( ) , 5 ) ;
4216
4220
assert_eq ! ( x. len( ) , 4 ) ;
4217
4221
assert_eq ! ( x[ 0 ] , 1 ) ;
4218
4222
assert_eq ! ( x[ 3 ] , 4 ) ;
4219
- }
4220
4223
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( ) ) ;
4226
4226
}
4227
4227
4228
4228
#[ test]
@@ -4285,34 +4285,36 @@ mod tests {
4285
4285
fn test_mut_shift_ref ( ) {
4286
4286
let mut x: & mut [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4287
4287
let h = x. mut_shift_ref ( ) ;
4288
- assert_eq ! ( * h, 1 ) ;
4288
+ assert_eq ! ( * h. unwrap ( ) , 1 ) ;
4289
4289
assert_eq ! ( x. len( ) , 4 ) ;
4290
4290
assert_eq ! ( x[ 0 ] , 2 ) ;
4291
4291
assert_eq ! ( x[ 3 ] , 5 ) ;
4292
- }
4293
4292
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( ) ) ;
4299
4295
}
4300
4296
4301
4297
#[ test]
4302
4298
fn test_mut_pop_ref ( ) {
4303
4299
let mut x: & mut [ int ] = [ 1 , 2 , 3 , 4 , 5 ] ;
4304
4300
let h = x. mut_pop_ref ( ) ;
4305
- assert_eq ! ( * h, 5 ) ;
4301
+ assert_eq ! ( * h. unwrap ( ) , 5 ) ;
4306
4302
assert_eq ! ( x. len( ) , 4 ) ;
4307
4303
assert_eq ! ( x[ 0 ] , 1 ) ;
4308
4304
assert_eq ! ( x[ 3 ] , 4 ) ;
4305
+
4306
+ let mut y: & mut [ int ] = [ ] ;
4307
+ assert ! ( y. mut_pop_ref( ) . is_none( ) ) ;
4309
4308
}
4310
4309
4311
4310
#[ 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( ) ) ;
4316
4318
}
4317
4319
}
4318
4320
0 commit comments