Skip to content

Commit b86a780

Browse files
committed
Add methods to go from a slice iterators to a slice.
A slice iterator is isomorphic to a slice, just with a slightly different form: storing start and end pointers rather than start pointer and length. This patch reflects this by making converting between them as easy as `iter.as_slice()` (or even `iter[]` if the shorter lifetime is ok). That is, `slice.iter().as_slice() == slice`.
1 parent bb2168c commit b86a780

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

src/libcore/slice.rs

+96
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,21 @@ macro_rules! iterator {
10921092
}
10931093
}
10941094

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+
10951110
/// Immutable slice iterator
10961111
#[experimental = "needs review"]
10971112
pub struct Items<'a, T: 'a> {
@@ -1100,6 +1115,36 @@ pub struct Items<'a, T: 'a> {
11001115
marker: marker::ContravariantLifetime<'a>
11011116
}
11021117

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+
11031148
iterator!{struct Items -> *const T, &'a T}
11041149

11051150
#[experimental = "needs review"]
@@ -1144,6 +1189,57 @@ pub struct MutItems<'a, T: 'a> {
11441189
marker2: marker::NoCopy
11451190
}
11461191

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+
11471243
iterator!{struct MutItems -> *mut T, &'a mut T}
11481244

11491245
#[experimental = "needs review"]

src/libcoretest/slice.rs

+49
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,52 @@ fn binary_search_not_found() {
3333
let b = [1i, 2, 4, 5, 6, 8];
3434
assert!(b.binary_search(|v| v.cmp(&9)) == NotFound(6));
3535
}
36+
37+
#[test]
38+
fn iterator_to_slice() {
39+
macro_rules! test {
40+
($data: expr) => {{
41+
let data: &mut [_] = &mut $data;
42+
let other_data: &mut [_] = &mut $data;
43+
44+
{
45+
let mut iter = data.iter();
46+
assert_eq!(iter[], other_data[]);
47+
48+
iter.next();
49+
assert_eq!(iter[], other_data[1..]);
50+
51+
iter.next_back();
52+
assert_eq!(iter[], other_data[1..2]);
53+
54+
let s = iter.as_slice();
55+
iter.next();
56+
assert_eq!(s, other_data[1..2]);
57+
}
58+
{
59+
let mut iter = data.iter_mut();
60+
assert_eq!(iter[], other_data[]);
61+
// mutability:
62+
assert!(iter[mut] == other_data);
63+
64+
iter.next();
65+
assert_eq!(iter[], other_data[1..]);
66+
assert!(iter[mut] == other_data[mut 1..]);
67+
68+
iter.next_back();
69+
70+
assert_eq!(iter[], other_data[1..2]);
71+
assert!(iter[mut] == other_data[mut 1..2]);
72+
73+
let s = iter.into_slice();
74+
assert!(s == other_data[mut 1..2]);
75+
}
76+
}}
77+
}
78+
79+
// try types of a variety of sizes
80+
test!([(1u64, 1u64, 1u8), (2, 2, 2), (3, 3, 3)]);
81+
test!([1u64,2,3]);
82+
test!([1u8,2,3]);
83+
test!([(),(),()]);
84+
}

0 commit comments

Comments
 (0)