@@ -505,7 +505,7 @@ pub trait ImmutableVector<'a, T> {
505
505
fn bsearch ( & self , f: |& T | -> Ordering ) -> Option < uint > ;
506
506
507
507
/**
508
- * Returns a mutable reference to the first element in this slice
508
+ * Returns an immutable reference to the first element in this slice
509
509
* and adjusts the slice in place so that it no longer contains
510
510
* that element. O(1).
511
511
*
@@ -523,7 +523,7 @@ pub trait ImmutableVector<'a, T> {
523
523
fn shift_ref ( & mut self ) -> Option < & ' a T > ;
524
524
525
525
/**
526
- * Returns a mutable reference to the last element in this slice
526
+ * Returns an immutable reference to the last element in this slice
527
527
* and adjusts the slice in place so that it no longer contains
528
528
* that element. O(1).
529
529
*
@@ -693,18 +693,22 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
693
693
}
694
694
695
695
fn shift_ref ( & mut self ) -> Option < & ' a T > {
696
- if self . len ( ) == 0 { return None ; }
697
696
unsafe {
698
697
let s: & mut Slice < T > = transmute ( self ) ;
699
- Some ( & * raw:: shift_ptr ( s) )
698
+ match raw:: shift_ptr ( s) {
699
+ Some ( p) => Some ( & * p) ,
700
+ None => None
701
+ }
700
702
}
701
703
}
702
704
703
705
fn pop_ref ( & mut self ) -> Option < & ' a T > {
704
- if self . len ( ) == 0 { return None ; }
705
706
unsafe {
706
707
let s: & mut Slice < T > = transmute ( self ) ;
707
- Some ( & * raw:: pop_ptr ( s) )
708
+ match raw:: pop_ptr ( s) {
709
+ Some ( p) => Some ( & * p) ,
710
+ None => None
711
+ }
708
712
}
709
713
}
710
714
}
@@ -1059,22 +1063,26 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
1059
1063
}
1060
1064
1061
1065
fn mut_shift_ref ( & mut self ) -> Option < & ' a mut T > {
1062
- if self . len ( ) == 0 { return None ; }
1063
1066
unsafe {
1064
1067
let s: & mut Slice < T > = transmute ( self ) ;
1065
- // FIXME #13933: this `&` -> `&mut` cast is a little
1066
- // dubious
1067
- Some ( & mut * ( raw:: shift_ptr ( s) as * mut _ ) )
1068
+ match raw:: shift_ptr ( s) {
1069
+ // FIXME #13933: this `&` -> `&mut` cast is a little
1070
+ // dubious
1071
+ Some ( p) => Some ( & mut * ( p as * mut _ ) ) ,
1072
+ None => None ,
1073
+ }
1068
1074
}
1069
1075
}
1070
1076
1071
1077
fn mut_pop_ref ( & mut self ) -> Option < & ' a mut T > {
1072
- if self . len ( ) == 0 { return None ; }
1073
1078
unsafe {
1074
1079
let s: & mut Slice < T > = transmute ( self ) ;
1075
- // FIXME #13933: this `&` -> `&mut` cast is a little
1076
- // dubious
1077
- Some ( & mut * ( raw:: pop_ptr ( s) as * mut _ ) )
1080
+ match raw:: pop_ptr ( s) {
1081
+ // FIXME #13933: this `&` -> `&mut` cast is a little
1082
+ // dubious
1083
+ Some ( p) => Some ( & mut * ( p as * mut _ ) ) ,
1084
+ None => None ,
1085
+ }
1078
1086
}
1079
1087
}
1080
1088
@@ -1165,6 +1173,7 @@ pub mod raw {
1165
1173
use iter:: Iterator ;
1166
1174
use ptr:: RawPtr ;
1167
1175
use raw:: Slice ;
1176
+ use option:: { None , Option , Some } ;
1168
1177
1169
1178
/**
1170
1179
* Form a slice from a pointer and length (as a number of units,
@@ -1198,27 +1207,29 @@ pub mod raw {
1198
1207
1199
1208
/**
1200
1209
* Returns a pointer to first element in slice and adjusts
1201
- * slice so it no longer contains that element. Fails if
1202
- * slice is empty. O(1).
1210
+ * slice so it no longer contains that element. Returns None
1211
+ * if the slice is empty. O(1).
1203
1212
*/
1204
- pub unsafe fn shift_ptr < T > ( slice : & mut Slice < T > ) -> * T {
1205
- if slice. len == 0 { fail ! ( "shift on empty slice" ) ; }
1213
+ #[ inline]
1214
+ pub unsafe fn shift_ptr < T > ( slice : & mut Slice < T > ) -> Option < * T > {
1215
+ if slice. len == 0 { return None ; }
1206
1216
let head: * T = slice. data ;
1207
1217
slice. data = slice. data . offset ( 1 ) ;
1208
1218
slice. len -= 1 ;
1209
- head
1219
+ Some ( head)
1210
1220
}
1211
1221
1212
1222
/**
1213
1223
* Returns a pointer to last element in slice and adjusts
1214
- * slice so it no longer contains that element. Fails if
1215
- * slice is empty. O(1).
1224
+ * slice so it no longer contains that element. Returns None
1225
+ * if the slice is empty. O(1).
1216
1226
*/
1217
- pub unsafe fn pop_ptr < T > ( slice : & mut Slice < T > ) -> * T {
1218
- if slice. len == 0 { fail ! ( "pop on empty slice" ) ; }
1227
+ #[ inline]
1228
+ pub unsafe fn pop_ptr < T > ( slice : & mut Slice < T > ) -> Option < * T > {
1229
+ if slice. len == 0 { return None ; }
1219
1230
let tail: * T = slice. data . offset ( ( slice. len - 1 ) as int ) ;
1220
1231
slice. len -= 1 ;
1221
- tail
1232
+ Some ( tail)
1222
1233
}
1223
1234
}
1224
1235
0 commit comments