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

fix(cordyceps): pin list::IterMut items #209

Merged
merged 2 commits into from
Jun 8, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cordyceps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ no-cache-pad = []
proptest = "1"
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = ["fmt"] }
pin-project = "1"

[target.'cfg(loom)'.dependencies]
loom = "0.5.5"
Expand Down
21 changes: 17 additions & 4 deletions cordyceps/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use core::{
fmt,
marker::PhantomPinned,
mem,
pin::Pin,
ptr::{self, NonNull},
};

Expand Down Expand Up @@ -569,7 +570,7 @@ impl<'list, T: Linked<Links<T>> + ?Sized> IntoIterator for &'list List<T> {
}

impl<'list, T: Linked<Links<T>> + ?Sized> IntoIterator for &'list mut List<T> {
type Item = &'list mut T;
type Item = Pin<&'list mut T>;
type IntoIter = IterMut<'list, T>;

#[inline]
Expand Down Expand Up @@ -827,7 +828,7 @@ impl<'list, T: Linked<Links<T>> + ?Sized> DoubleEndedIterator for Iter<'list, T>
// === impl IterMut ====

impl<'list, T: Linked<Links<T>> + ?Sized> Iterator for IterMut<'list, T> {
type Item = &'list mut T;
type Item = Pin<&'list mut T>;

fn next(&mut self) -> Option<Self::Item> {
if self.len == 0 {
Expand All @@ -842,7 +843,13 @@ impl<'list, T: Linked<Links<T>> + ?Sized> Iterator for IterMut<'list, T> {
// while the iterator exists. the returned item will not outlive the
// iterator.
self.curr = T::links(curr).as_ref().next();
Some(curr.as_mut())

// safety: pinning the returned element is actually *necessary* to
// uphold safety invariants here. if we returned `&mut T`, the
// element could be `mem::replace`d out of the list, invalidating
// any pointers to it. thus, we *must* pin it before returning it.
let pin = Pin::new_unchecked(curr.as_mut());
Some(pin)
}
}

Expand Down Expand Up @@ -874,7 +881,13 @@ impl<'list, T: Linked<Links<T>> + ?Sized> DoubleEndedIterator for IterMut<'list,
// while the iterator exists. the returned item will not outlive the
// iterator.
self.curr_back = T::links(curr).as_ref().prev();
Some(curr.as_mut())

// safety: pinning the returned element is actually *necessary* to
// uphold safety invariants here. if we returned `&mut T`, the
// element could be `mem::replace`d out of the list, invalidating
// any pointers to it. thus, we *must* pin it before returning it.
let pin = Pin::new_unchecked(curr.as_mut());
Some(pin)
}
}
}
9 changes: 6 additions & 3 deletions cordyceps/src/list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,10 @@ mod owned_entry {

/// An entry type whose ownership is assigned to the list directly.
#[derive(Debug)]
#[pin_project::pin_project]
#[repr(C)]
struct OwnedEntry {
#[pin]
links: Links<OwnedEntry>,
val: i32,
}
Expand Down Expand Up @@ -599,9 +601,10 @@ mod owned_entry {
let a = owned_entry(1);
let b = owned_entry(2);
let c = owned_entry(3);
fn incr_entry(entry: &mut OwnedEntry) -> i32 {
entry.val += 1;
entry.val
fn incr_entry(entry: Pin<&mut OwnedEntry>) -> i32 {
let entry = entry.project();
*entry.val += 1;
*entry.val
}

let mut list = List::new();
Expand Down