-
Notifications
You must be signed in to change notification settings - Fork 112
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
Pinned Box #228
Draft
cynecx
wants to merge
4
commits into
fitzgen:main
Choose a base branch
from
cynecx:drop-list-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Pinned Box #228
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,13 +165,6 @@ impl<'a, T> Box<'a, T> { | |
Box(a.alloc(x)) | ||
} | ||
|
||
/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then | ||
/// `x` will be pinned in memory and unable to be moved. | ||
#[inline(always)] | ||
pub fn pin_in(x: T, a: &'a Bump) -> Pin<Box<'a, T>> { | ||
Box(a.alloc(x)).into() | ||
} | ||
|
||
/// Consumes the `Box`, returning the wrapped value. | ||
/// | ||
/// # Examples | ||
|
@@ -452,18 +445,6 @@ impl<'a, T: ?Sized + Hasher> Hasher for Box<'a, T> { | |
} | ||
} | ||
|
||
impl<'a, T: ?Sized> From<Box<'a, T>> for Pin<Box<'a, T>> { | ||
/// Converts a `Box<T>` into a `Pin<Box<T>>`. | ||
/// | ||
/// This conversion does not allocate on the heap and happens in place. | ||
fn from(boxed: Box<'a, T>) -> Self { | ||
// It's not possible to move or replace the insides of a `Pin<Box<T>>` | ||
// when `T: !Unpin`, so it's safe to pin it directly without any | ||
// additional requirements. | ||
unsafe { Pin::new_unchecked(boxed) } | ||
} | ||
} | ||
|
||
Comment on lines
-455
to
-466
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise, this impl could just be restricted to impl<T: ?Sized> From<Box<'static, T>> for Pin<Box<'static, T>> {
/// Converts a `Box<'static, T>` into a `Pin<Box<'static, T>>`.
///
/// This conversion does not allocate and happens in place. This requires borrowing
/// the `Bump` for `'static` to ensure that it can never be reset or dropped.
fn from(boxed: Box<'static, T>) -> Self {
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
// when `T: !Unpin`, and because the lifetime the `Bump` is borrowed
// for is `'static`, the memory can never be re-used, so it's safe to pin
// it directly without any additional requirements.
unsafe { Pin::new_unchecked(boxed) }
}
} |
||
impl<'a> Box<'a, dyn Any> { | ||
#[inline] | ||
/// Attempt to downcast the box to a concrete type. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use core::{ | ||
cell::{Cell, UnsafeCell}, | ||
marker::PhantomPinned, | ||
mem::MaybeUninit, | ||
ptr::NonNull, | ||
}; | ||
|
||
/// A circular doubly linked list. | ||
#[derive(Debug, Default)] | ||
pub struct DropList { | ||
pub link: Link, | ||
} | ||
|
||
impl DropList { | ||
/// Safety: `self` must be pinned. | ||
#[inline] | ||
pub unsafe fn init(&self) { | ||
let link_ptr = Some(NonNull::from(&self.link)); | ||
self.link.prev.set(link_ptr); | ||
self.link.next.set(link_ptr); | ||
} | ||
|
||
pub unsafe fn insert(&self, node: NonNull<Link>) { | ||
insert_after(NonNull::from(&self.link), node) | ||
} | ||
|
||
pub unsafe fn run_drop(&self) { | ||
let mut curr = self.link.next.get().unwrap(); | ||
let end = NonNull::from(&self.link); | ||
while curr != end { | ||
let entry = unsafe { curr.cast::<DropEntry<()>>().as_ref() }; | ||
unsafe { | ||
(entry.drop_fn)(entry.data.assume_init_ref().get()); | ||
} | ||
curr = entry.link.next.get().unwrap(); | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
unsafe fn insert_after(tail: NonNull<Link>, node_ptr: NonNull<Link>) { | ||
let tail = tail.as_ref(); | ||
|
||
let node = node_ptr.as_ref(); | ||
node.prev.set(Some(NonNull::from(tail))); | ||
node.next.set(tail.next.get()); | ||
|
||
tail.next.get().unwrap().as_ref().prev.set(Some(node_ptr)); | ||
tail.next.set(Some(node_ptr)); | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Link { | ||
prev: Cell<Option<NonNull<Link>>>, | ||
next: Cell<Option<NonNull<Link>>>, | ||
_marker: PhantomPinned, | ||
} | ||
|
||
impl Link { | ||
pub unsafe fn unlink(&self) { | ||
let Some(prev) = self.prev.take() else { | ||
return; | ||
}; | ||
let next = self.next.take().unwrap(); | ||
prev.as_ref().next.set(Some(next)); | ||
next.as_ref().prev.set(Some(prev)); | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
#[repr(C)] | ||
pub struct DropEntry<T> { | ||
link: Link, | ||
drop_fn: unsafe fn(*mut ()), | ||
data: MaybeUninit<UnsafeCell<T>>, | ||
} | ||
|
||
impl<T> DropEntry<T> { | ||
#[inline] | ||
pub fn new(val: T) -> Self { | ||
Self { | ||
link: Link::default(), | ||
drop_fn: unsafe { | ||
core::mem::transmute::<_, unsafe fn(*mut ())>( | ||
core::ptr::drop_in_place::<T> as unsafe fn(*mut T), | ||
) | ||
}, | ||
data: MaybeUninit::new(UnsafeCell::new(val)), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn link_and_data(&self) -> (NonNull<Link>, *mut T) { | ||
(NonNull::from(&self.link), self.data.assume_init_ref().get()) | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn ptr_from_data(data: *mut T) -> NonNull<DropEntry<T>> { | ||
NonNull::new_unchecked( | ||
data.byte_sub(memoffset::offset_of!(Self, data)) | ||
.cast::<DropEntry<T>>(), | ||
) | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn link_from_data(data: *mut T) -> NonNull<Link> { | ||
let entry = Self::ptr_from_data(data).as_ptr(); | ||
NonNull::new_unchecked(core::ptr::addr_of_mut!((*entry).link)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of being removed entirely, it would also be sound to restrict it to
'static
, i.e. add a separateimpl
block forBox<'static, T>
with