Skip to content

Commit ee3dbad

Browse files
committed
(feat) Move slice iterators to Items and ItemsMut.
1 parent 5d30257 commit ee3dbad

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/impls/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
pub use self::slice::{SliceIntrusiveIter, Items, ItemsMut};
2+
13
mod slice;
24
mod vec;

src/impls/slice.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
use std::{mem, raw};
22
use {IntrusiveIterator};
33

4-
impl<'a, T> IntrusiveIterator<&'a T> for &'a [T] {
4+
pub trait SliceIntrusiveIter<T> for Sized? {
5+
fn intrusive_iter(&self) -> Items<T>;
6+
fn intrusive_iter_mut(&mut self) -> ItemsMut<T>;
7+
}
8+
9+
pub struct Items<'a, T: 'a>(&'a [T]);
10+
pub struct ItemsMut<'a, T: 'a>(&'a mut [T]);
11+
12+
impl<T> SliceIntrusiveIter<T> for [T] {
13+
#[inline]
14+
fn intrusive_iter(&self) -> Items<T> { Items(self) }
15+
16+
#[inline]
17+
fn intrusive_iter_mut(&mut self) -> ItemsMut<T> { ItemsMut(self) }
18+
}
19+
20+
impl<'a, T> IntrusiveIterator<&'a T> for Items<'a, T> {
521
#[inline]
622
fn traverse<F: FnMut(&'a T) -> bool>(self, mut f: F) {
723
unsafe {
8-
let slice = mem::transmute::<&'a [T], raw::Slice<T>>(self);
24+
let slice = mem::transmute::<&'a [T], raw::Slice<T>>(self.0);
925

1026
let is_zero_size = mem::size_of::<T>() == 0;
1127

@@ -26,11 +42,11 @@ impl<'a, T> IntrusiveIterator<&'a T> for &'a [T] {
2642
}
2743
}
2844

29-
impl<'a, T> IntrusiveIterator<&'a mut T> for &'a mut [T] {
45+
impl<'a, T> IntrusiveIterator<&'a mut T> for ItemsMut<'a, T> {
3046
#[inline]
3147
fn traverse<F: FnMut(&'a mut T) -> bool>(self, mut f: F) {
3248
unsafe {
33-
let slice = mem::transmute::<&'a mut [T], raw::Slice<T>>(self);
49+
let slice = mem::transmute::<&'a mut [T], raw::Slice<T>>(self.0);
3450

3551
let is_zero_size = mem::size_of::<T>() == 0;
3652

@@ -59,13 +75,13 @@ mod test {
5975
describe! intrusive_slice_iter {
6076
it "should yield all elements of a slice in order" {
6177
let data = [1u, 2, 5, 4, 6, 7];
62-
let intrusive: Vec<uint> = data.as_slice().map(|&x| x).collect();
78+
let intrusive: Vec<uint> = data.intrusive_iter().map(|&x| x).collect();
6379
assert_eq!(&*intrusive, data.as_slice());
6480
}
6581

6682
it "should work with zero-sized types" {
6783
let data = [(), (), ()];
68-
let intrusive: Vec<()> = data.as_slice().map(|&x| x).collect();
84+
let intrusive: Vec<()> = data.intrusive_iter().map(|&x| x).collect();
6985
assert_eq!(&*intrusive, data.as_slice());
7086
}
7187

@@ -74,7 +90,7 @@ mod test {
7490

7591
let data = Vec::from_fn(10000, |_| random::<uint>());
7692
bench.iter(|| {
77-
data.as_slice().iterate(|&: x| ::test::black_box(x));
93+
data.intrusive_iter().iterate(|&: x| ::test::black_box(x));
7894
});
7995
}
8096

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ extern crate stainless;
99
#[cfg(test)]
1010
extern crate test;
1111

12+
pub use self::impls::{SliceIntrusiveIter, Items, ItemsMut};
13+
pub mod utils;
14+
1215
/// Intrusive Iterators.
1316
pub trait IntrusiveIterator<T> {
1417
/// Run this Iterator using the provided closure.
@@ -180,4 +183,4 @@ pub struct Cloned<I> {
180183

181184
mod ext;
182185
mod impls;
183-
pub mod utils;
186+

0 commit comments

Comments
 (0)