@@ -1104,6 +1104,21 @@ macro_rules! iterator {
11041104 }
11051105}
11061106
1107+ macro_rules! make_slice {
1108+ ( $t: ty -> $result: ty: $start: expr, $end: expr) => { {
1109+ let diff = $end as uint - $start as uint;
1110+ let len = if mem:: size_of:: <T >( ) == 0 {
1111+ diff
1112+ } else {
1113+ diff / mem:: size_of:: <$t>( )
1114+ } ;
1115+ unsafe {
1116+ transmute:: <_, $result>( RawSlice { data: $start as * const T , len: len } )
1117+ }
1118+ } }
1119+ }
1120+
1121+
11071122/// Immutable slice iterator
11081123#[ experimental = "needs review" ]
11091124pub struct Items < ' a , T : ' a > {
@@ -1112,6 +1127,36 @@ pub struct Items<'a, T: 'a> {
11121127 marker : marker:: ContravariantLifetime < ' a >
11131128}
11141129
1130+ #[ experimental]
1131+ impl < ' a , T > ops:: Slice < uint , [ T ] > for Items < ' a , T > {
1132+ fn as_slice_ ( & self ) -> & [ T ] {
1133+ self . as_slice ( )
1134+ }
1135+ fn slice_from_or_fail < ' b > ( & ' b self , from : & uint ) -> & ' b [ T ] {
1136+ use ops:: Slice ;
1137+ self . as_slice ( ) . slice_from_or_fail ( from)
1138+ }
1139+ fn slice_to_or_fail < ' b > ( & ' b self , to : & uint ) -> & ' b [ T ] {
1140+ use ops:: Slice ;
1141+ self . as_slice ( ) . slice_to_or_fail ( to)
1142+ }
1143+ fn slice_or_fail < ' b > ( & ' b self , from : & uint , to : & uint ) -> & ' b [ T ] {
1144+ use ops:: Slice ;
1145+ self . as_slice ( ) . slice_or_fail ( from, to)
1146+ }
1147+ }
1148+
1149+ impl < ' a , T > Items < ' a , T > {
1150+ /// View the underlying data as a subslice of the original data.
1151+ ///
1152+ /// This has the same lifetime as the original slice, and so the
1153+ /// iterator can continue to be used while this exists.
1154+ #[ experimental]
1155+ pub fn as_slice ( & self ) -> & ' a [ T ] {
1156+ make_slice ! ( T -> & ' a [ T ] : self . ptr, self . end)
1157+ }
1158+ }
1159+
11151160iterator ! { struct Items -> * const T , & ' a T }
11161161
11171162#[ experimental = "needs review" ]
@@ -1156,6 +1201,57 @@ pub struct MutItems<'a, T: 'a> {
11561201 marker2 : marker:: NoCopy
11571202}
11581203
1204+ #[ experimental]
1205+ impl < ' a , T > ops:: Slice < uint , [ T ] > for MutItems < ' a , T > {
1206+ fn as_slice_ < ' b > ( & ' b self ) -> & ' b [ T ] {
1207+ make_slice ! ( T -> & ' b [ T ] : self . ptr, self . end)
1208+ }
1209+ fn slice_from_or_fail < ' b > ( & ' b self , from : & uint ) -> & ' b [ T ] {
1210+ use ops:: Slice ;
1211+ self . as_slice_ ( ) . slice_from_or_fail ( from)
1212+ }
1213+ fn slice_to_or_fail < ' b > ( & ' b self , to : & uint ) -> & ' b [ T ] {
1214+ use ops:: Slice ;
1215+ self . as_slice_ ( ) . slice_to_or_fail ( to)
1216+ }
1217+ fn slice_or_fail < ' b > ( & ' b self , from : & uint , to : & uint ) -> & ' b [ T ] {
1218+ use ops:: Slice ;
1219+ self . as_slice_ ( ) . slice_or_fail ( from, to)
1220+ }
1221+ }
1222+
1223+ #[ experimental]
1224+ impl < ' a , T > ops:: SliceMut < uint , [ T ] > for MutItems < ' a , T > {
1225+ fn as_mut_slice_ < ' b > ( & ' b mut self ) -> & ' b mut [ T ] {
1226+ make_slice ! ( T -> & ' b mut [ T ] : self . ptr, self . end)
1227+ }
1228+ fn slice_from_or_fail_mut < ' b > ( & ' b mut self , from : & uint ) -> & ' b mut [ T ] {
1229+ use ops:: SliceMut ;
1230+ self . as_mut_slice_ ( ) . slice_from_or_fail_mut ( from)
1231+ }
1232+ fn slice_to_or_fail_mut < ' b > ( & ' b mut self , to : & uint ) -> & ' b mut [ T ] {
1233+ use ops:: SliceMut ;
1234+ self . as_mut_slice_ ( ) . slice_to_or_fail_mut ( to)
1235+ }
1236+ fn slice_or_fail_mut < ' b > ( & ' b mut self , from : & uint , to : & uint ) -> & ' b mut [ T ] {
1237+ use ops:: SliceMut ;
1238+ self . as_mut_slice_ ( ) . slice_or_fail_mut ( from, to)
1239+ }
1240+ }
1241+
1242+ impl < ' a , T > MutItems < ' a , T > {
1243+ /// View the underlying data as a subslice of the original data.
1244+ ///
1245+ /// To avoid creating `&mut` references that alias, this is forced
1246+ /// to consume the iterator. Consider using the `Slice` and
1247+ /// `SliceMut` implementations for obtaining slices with more
1248+ /// restricted lifetimes that do not consume the iterator.
1249+ #[ experimental]
1250+ pub fn into_slice ( self ) -> & ' a mut [ T ] {
1251+ make_slice ! ( T -> & ' a mut [ T ] : self . ptr, self . end)
1252+ }
1253+ }
1254+
11591255iterator ! { struct MutItems -> * mut T , & ' a mut T }
11601256
11611257#[ experimental = "needs review" ]
0 commit comments