@@ -1092,6 +1092,21 @@ macro_rules! iterator {
1092
1092
}
1093
1093
}
1094
1094
1095
+ macro_rules! make_slice {
1096
+ ( $t: ty -> $result: ty: $start: expr, $end: expr) => { {
1097
+ let diff = $end as uint - $start as uint;
1098
+ let len = if mem:: size_of:: <T >( ) == 0 {
1099
+ diff
1100
+ } else {
1101
+ diff / mem:: size_of:: <$t>( )
1102
+ } ;
1103
+ unsafe {
1104
+ transmute:: <_, $result>( RawSlice { data: $start as * const T , len: len } )
1105
+ }
1106
+ } }
1107
+ }
1108
+
1109
+
1095
1110
/// Immutable slice iterator
1096
1111
#[ experimental = "needs review" ]
1097
1112
pub struct Items < ' a , T : ' a > {
@@ -1100,6 +1115,36 @@ pub struct Items<'a, T: 'a> {
1100
1115
marker : marker:: ContravariantLifetime < ' a >
1101
1116
}
1102
1117
1118
+ #[ experimental]
1119
+ impl < ' a , T > ops:: Slice < uint , [ T ] > for Items < ' a , T > {
1120
+ fn as_slice_ ( & self ) -> & [ T ] {
1121
+ self . as_slice ( )
1122
+ }
1123
+ fn slice_from_or_fail < ' b > ( & ' b self , from : & uint ) -> & ' b [ T ] {
1124
+ use ops:: Slice ;
1125
+ self . as_slice ( ) . slice_from_or_fail ( from)
1126
+ }
1127
+ fn slice_to_or_fail < ' b > ( & ' b self , to : & uint ) -> & ' b [ T ] {
1128
+ use ops:: Slice ;
1129
+ self . as_slice ( ) . slice_to_or_fail ( to)
1130
+ }
1131
+ fn slice_or_fail < ' b > ( & ' b self , from : & uint , to : & uint ) -> & ' b [ T ] {
1132
+ use ops:: Slice ;
1133
+ self . as_slice ( ) . slice_or_fail ( from, to)
1134
+ }
1135
+ }
1136
+
1137
+ impl < ' a , T > Items < ' a , T > {
1138
+ /// View the underlying data as a subslice of the original data.
1139
+ ///
1140
+ /// This has the same lifetime as the original slice, and so the
1141
+ /// iterator can continue to be used while this exists.
1142
+ #[ experimental]
1143
+ pub fn as_slice ( & self ) -> & ' a [ T ] {
1144
+ make_slice ! ( T -> & ' a [ T ] : self . ptr, self . end)
1145
+ }
1146
+ }
1147
+
1103
1148
iterator ! { struct Items -> * const T , & ' a T }
1104
1149
1105
1150
#[ experimental = "needs review" ]
@@ -1144,6 +1189,57 @@ pub struct MutItems<'a, T: 'a> {
1144
1189
marker2 : marker:: NoCopy
1145
1190
}
1146
1191
1192
+ #[ experimental]
1193
+ impl < ' a , T > ops:: Slice < uint , [ T ] > for MutItems < ' a , T > {
1194
+ fn as_slice_ < ' b > ( & ' b self ) -> & ' b [ T ] {
1195
+ make_slice ! ( T -> & ' b [ T ] : self . ptr, self . end)
1196
+ }
1197
+ fn slice_from_or_fail < ' b > ( & ' b self , from : & uint ) -> & ' b [ T ] {
1198
+ use ops:: Slice ;
1199
+ self . as_slice_ ( ) . slice_from_or_fail ( from)
1200
+ }
1201
+ fn slice_to_or_fail < ' b > ( & ' b self , to : & uint ) -> & ' b [ T ] {
1202
+ use ops:: Slice ;
1203
+ self . as_slice_ ( ) . slice_to_or_fail ( to)
1204
+ }
1205
+ fn slice_or_fail < ' b > ( & ' b self , from : & uint , to : & uint ) -> & ' b [ T ] {
1206
+ use ops:: Slice ;
1207
+ self . as_slice_ ( ) . slice_or_fail ( from, to)
1208
+ }
1209
+ }
1210
+
1211
+ #[ experimental]
1212
+ impl < ' a , T > ops:: SliceMut < uint , [ T ] > for MutItems < ' a , T > {
1213
+ fn as_mut_slice_ < ' b > ( & ' b mut self ) -> & ' b mut [ T ] {
1214
+ make_slice ! ( T -> & ' b mut [ T ] : self . ptr, self . end)
1215
+ }
1216
+ fn slice_from_or_fail_mut < ' b > ( & ' b mut self , from : & uint ) -> & ' b mut [ T ] {
1217
+ use ops:: SliceMut ;
1218
+ self . as_mut_slice_ ( ) . slice_from_or_fail_mut ( from)
1219
+ }
1220
+ fn slice_to_or_fail_mut < ' b > ( & ' b mut self , to : & uint ) -> & ' b mut [ T ] {
1221
+ use ops:: SliceMut ;
1222
+ self . as_mut_slice_ ( ) . slice_to_or_fail_mut ( to)
1223
+ }
1224
+ fn slice_or_fail_mut < ' b > ( & ' b mut self , from : & uint , to : & uint ) -> & ' b mut [ T ] {
1225
+ use ops:: SliceMut ;
1226
+ self . as_mut_slice_ ( ) . slice_or_fail_mut ( from, to)
1227
+ }
1228
+ }
1229
+
1230
+ impl < ' a , T > MutItems < ' a , T > {
1231
+ /// View the underlying data as a subslice of the original data.
1232
+ ///
1233
+ /// To avoid creating `&mut` references that alias, this is forced
1234
+ /// to consume the iterator. Consider using the `Slice` and
1235
+ /// `SliceMut` implementations for obtaining slices with more
1236
+ /// restricted lifetimes that do not consume the iterator.
1237
+ #[ experimental]
1238
+ pub fn into_slice ( self ) -> & ' a mut [ T ] {
1239
+ make_slice ! ( T -> & ' a mut [ T ] : self . ptr, self . end)
1240
+ }
1241
+ }
1242
+
1147
1243
iterator ! { struct MutItems -> * mut T , & ' a mut T }
1148
1244
1149
1245
#[ experimental = "needs review" ]
0 commit comments