Skip to content
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

feat(cordyceps): add core::iter trait impls for List #232

Merged
merged 3 commits into from
Jun 19, 2022
Merged
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
27 changes: 26 additions & 1 deletion cordyceps/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::Linked;
use crate::util::FmtOption;
use core::{
cell::UnsafeCell,
fmt,
fmt, iter,
marker::PhantomPinned,
mem,
pin::Pin,
Expand Down Expand Up @@ -746,6 +746,31 @@ impl<T: Linked<Links<T>> + ?Sized> List<T> {
}
}

impl<T> iter::Extend<T::Handle> for List<T>
where
T: Linked<Links<T>> + ?Sized,
{
fn extend<I: IntoIterator<Item = T::Handle>>(&mut self, iter: I) {
for item in iter {
self.push_back(item);
}
}

// TODO(eliza): when `Extend::extend_one` becomes stable, implement that
// as well, so that we can just call `push_back` without looping.
}

impl<T> iter::FromIterator<T::Handle> for List<T>
where
T: Linked<Links<T>> + ?Sized,
{
fn from_iter<I: IntoIterator<Item = T::Handle>>(iter: I) -> Self {
let mut list = Self::new();
list.extend(iter);
list
}
}

unsafe impl<T: Linked<Links<T>> + ?Sized> Send for List<T> where T: Send {}
unsafe impl<T: Linked<Links<T>> + ?Sized> Sync for List<T> where T: Sync {}

Expand Down
26 changes: 12 additions & 14 deletions cordyceps/src/list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ fn push_all<'a>(
list: &mut List<Entry<'a>>,
entries: impl IntoIterator<Item = &'a Pin<Box<Entry<'a>>>>,
) {
for entry in entries.into_iter() {
list.push_back(entry.as_ref());
}
list.extend(entries.into_iter().map(Pin::as_ref))
}

fn list_from_iter<'a>(
entries: impl IntoIterator<Item = &'a Pin<Box<Entry<'a>>>>,
) -> List<Entry<'a>> {
let mut list = List::new();
push_all(&mut list, entries);
list
}

macro_rules! assert_clean {
Expand Down Expand Up @@ -230,10 +236,7 @@ fn push_pop_push_pop() {
#[test]
fn double_ended_iter() {
let entries = [entry(1), entry(2), entry(3)];

let mut list = List::new();

push_all(&mut list, &entries);
let list = list_from_iter(&entries);

let head_to_tail = list.iter().map(|entry| entry.val).collect::<Vec<_>>();
assert_eq!(&head_to_tail, &[1, 2, 3]);
Expand All @@ -250,9 +253,7 @@ fn double_ended_iter() {
fn double_ended_iter_empties() {
let entries = [entry(1), entry(2), entry(3), entry(4)];

let mut list = List::new();

push_all(&mut list, &entries);
let list = list_from_iter(&entries);

let mut iter = list.iter();

Expand All @@ -269,10 +270,7 @@ fn double_ended_iter_empties() {
#[test]
fn drain_filter() {
let entries = [entry(1), entry(2), entry(3), entry(4)];

let mut list = List::new();

push_all(&mut list, &entries);
let mut list = list_from_iter(&entries);

{
// Create a scope so that the mutable borrow on the list is released
Expand Down
6 changes: 2 additions & 4 deletions cordyceps/src/list/tests/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ fn move_peek() {
let _ = super::trace_init();

let entries = [entry(1), entry(2), entry(3), entry(4), entry(5), entry(6)];
let mut list = list_from_iter(&entries);

let mut list = List::new();
push_all(&mut list, &entries);
let mut cursor = list.cursor_front_mut();
assert_eq!(val(cursor.current()), Some(1));
assert_eq!(val(cursor.peek_next()), Some(2));
Expand Down Expand Up @@ -111,8 +110,7 @@ fn cursor_mut_insert() {
let nine = entry(9);
let ten = entry(10);

let mut list = List::<Entry<'_>>::new();
push_all(&mut list, &entries);
let mut list = list_from_iter(&entries);

let mut cursor = list.cursor_front_mut();
cursor.insert_before(seven.as_ref());
Expand Down
35 changes: 15 additions & 20 deletions cordyceps/src/list/tests/remove_by_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ fn first() {

unsafe {
// Remove first
let mut list = List::new();
let mut list = [a.as_ref(), b.as_ref(), c.as_ref()]
.into_iter()
.collect::<List<_>>();

push_all(&mut list, vec![&a, &b, &c]);
assert!(list.remove(ptr(&a)).is_some());
assert_clean!(a);
list.assert_valid();
Expand Down Expand Up @@ -41,9 +42,7 @@ fn first() {

unsafe {
// Remove first of two
let mut list = List::new();

push_all(&mut list, vec![&a, &b]);
let mut list = [a.as_ref(), b.as_ref()].into_iter().collect::<List<_>>();

assert!(list.remove(ptr(&a)).is_some());
assert_clean!(a);
Expand Down Expand Up @@ -73,9 +72,9 @@ fn middle() {
let c = entry(31);

unsafe {
let mut list = List::new();

push_all(&mut list, vec![&a, &b, &c]);
let mut list = [a.as_ref(), b.as_ref(), c.as_ref()]
.into_iter()
.collect::<List<_>>();

assert!(list.remove(ptr(&a)).is_some());
assert_clean!(a);
Expand All @@ -91,9 +90,9 @@ fn middle() {
}

unsafe {
let mut list = List::new();

push_all(&mut list, vec![&a, &b, &c]);
let mut list = [a.as_ref(), b.as_ref(), c.as_ref()]
.into_iter()
.collect::<List<_>>();

assert!(list.remove(ptr(&b)).is_some());
assert_clean!(b);
Expand All @@ -118,9 +117,9 @@ fn last_middle() {
unsafe {
// Remove last
// Remove middle
let mut list = List::new();

push_all(&mut list, vec![&a, &b, &c]);
let mut list = [a.as_ref(), b.as_ref(), c.as_ref()]
.into_iter()
.collect::<List<_>>();

assert!(list.remove(ptr(&c)).is_some());
assert_clean!(c);
Expand All @@ -143,9 +142,7 @@ fn last() {

unsafe {
// Remove last item
let mut list = List::new();

push_all(&mut list, Some(&a));
let mut list = [a.as_ref()].into_iter().collect::<List<_>>();

assert!(list.remove(ptr(&a)).is_some());
assert_clean!(a);
Expand All @@ -159,9 +156,7 @@ fn last() {

unsafe {
// Remove last of two
let mut list = List::new();

push_all(&mut list, vec![&a, &b]);
let mut list = [a.as_ref(), b.as_ref()].into_iter().collect::<List<_>>();

assert!(list.remove(ptr(&b)).is_some());
assert_clean!(b);
Expand Down