Skip to content

Move Slice Iterators #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod slice;
mod vec;
40 changes: 24 additions & 16 deletions src/impls.rs → src/impls/slice.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
use std::{mem, raw};
use {IntrusiveIterator, FromIntrusiveIterator};
use {IntrusiveIterator};

impl<'a, T> IntrusiveIterator<&'a T> for &'a [T] {
pub trait SliceIntrusiveIter<T> for Sized? {
fn intrusive_iter(&self) -> Items<T>;
fn intrusive_iter_mut(&mut self) -> ItemsMut<T>;
}

pub struct Items<'a, T: 'a>(&'a [T]);
pub struct ItemsMut<'a, T: 'a>(&'a mut [T]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless of what names we settle on, I definitely wouldn't use the same name as the "real" iterators.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though they are namespaced under intrusive::slice?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should be the same name as an official impl would use. e.g. BList already has Items for the iterator.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're saying we shouldn't use these names because it would make it harder to move this into std? That makes sense.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah more or less.


impl<T> SliceIntrusiveIter<T> for [T] {
#[inline]
fn intrusive_iter(&self) -> Items<T> { Items(self) }

#[inline]
fn intrusive_iter_mut(&mut self) -> ItemsMut<T> { ItemsMut(self) }
}

impl<'a, T> IntrusiveIterator<&'a T> for Items<'a, T> {
#[inline]
fn traverse<F: FnMut(&'a T) -> bool>(self, mut f: F) {
unsafe {
let slice = mem::transmute::<&'a [T], raw::Slice<T>>(self);
let slice = mem::transmute::<&'a [T], raw::Slice<T>>(self.0);

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

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

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

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

Expand All @@ -51,14 +67,6 @@ impl<'a, T> IntrusiveIterator<&'a mut T> for &'a mut [T] {
}
}

impl<T> FromIntrusiveIterator<T> for Vec<T> {
fn collect<I: IntrusiveIterator<T>>(iter: I) -> Vec<T> {
let mut vec = Vec::new();
iter.iterate(|&mut: elem| vec.push(elem));
vec
}
}

#[cfg(test)]
mod test {
pub use super::*;
Expand All @@ -67,13 +75,13 @@ mod test {
describe! intrusive_slice_iter {
it "should yield all elements of a slice in order" {
let data = [1u, 2, 5, 4, 6, 7];
let intrusive: Vec<uint> = data.as_slice().map(|&x| x).collect();
let intrusive: Vec<uint> = data.intrusive_iter().map(|&x| x).collect();
assert_eq!(&*intrusive, data.as_slice());
}

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

Expand All @@ -82,7 +90,7 @@ mod test {

let data = Vec::from_fn(10000, |_| random::<uint>());
bench.iter(|| {
data.as_slice().iterate(|&: x| ::test::black_box(x));
data.intrusive_iter().iterate(|&: x| ::test::black_box(x));
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/impls/vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use {IntrusiveIterator, FromIntrusiveIterator};

impl<T> FromIntrusiveIterator<T> for Vec<T> {
fn collect<I: IntrusiveIterator<T>>(iter: I) -> Vec<T> {
let mut vec = Vec::new();
iter.iterate(|&mut: elem| vec.push(elem));
vec
}
}

5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ extern crate stainless;
#[cfg(test)]
extern crate test;

pub use self::impls::slice;
pub mod utils;

/// Intrusive Iterators.
pub trait IntrusiveIterator<T> {
/// Run this Iterator using the provided closure.
Expand Down Expand Up @@ -180,4 +183,4 @@ pub struct Cloned<I> {

mod ext;
mod impls;
pub mod utils;