-
Notifications
You must be signed in to change notification settings - Fork 60
Update slice DST ops, use &raw mut, and improve documentation #128
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -356,79 +356,65 @@ impl<T: ?Sized> Arc<T> { | |||||
| /// The function `mem_to_arcinner` is called with the data pointer | ||||||
| /// and must return back a (potentially fat)-pointer for the `ArcInner<T>`. | ||||||
| /// | ||||||
| /// This function initializes the reference count, but the caller is | ||||||
| /// responsible for initializing `inner_ptr.data` after `inner_ptr` is | ||||||
| /// returned from this function. | ||||||
| /// | ||||||
| /// ## Safety | ||||||
| /// | ||||||
| /// `mem_to_arcinner` must return the same pointer, the only things that can change are | ||||||
| /// - its type | ||||||
| /// - its metadata | ||||||
| /// | ||||||
| /// `value_layout` must be correct for `T`. | ||||||
| #[allow(unused_unsafe)] | ||||||
| pub(super) unsafe fn allocate_for_layout( | ||||||
| value_layout: Layout, | ||||||
| mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>, | ||||||
| ) -> NonNull<ArcInner<T>> { | ||||||
| let layout = Layout::new::<ArcInner<()>>() | ||||||
| .extend(value_layout) | ||||||
| .unwrap() | ||||||
| .0 | ||||||
| .pad_to_align(); | ||||||
|
|
||||||
| // Safety: we propagate safety requirements to the caller | ||||||
| unsafe { | ||||||
| Arc::try_allocate_for_layout(value_layout, mem_to_arcinner) | ||||||
| .unwrap_or_else(|_| handle_alloc_error(layout)) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Allocates an `ArcInner<T>` with sufficient space for | ||||||
| /// a possibly-unsized inner value where the value has the layout provided, | ||||||
| /// returning an error if allocation fails. | ||||||
| /// | ||||||
| /// The function `mem_to_arcinner` is called with the data pointer | ||||||
| /// and must return back a (potentially fat)-pointer for the `ArcInner<T>`. | ||||||
| /// | ||||||
| /// ## Safety | ||||||
| /// | ||||||
| /// `mem_to_arcinner` must return the same pointer, the only things that can change are | ||||||
| /// - its type | ||||||
| /// - its metadata | ||||||
| /// | ||||||
| /// `value_layout` must be correct for `T`. | ||||||
| #[allow(unused_unsafe)] | ||||||
| unsafe fn try_allocate_for_layout( | ||||||
| value_layout: Layout, | ||||||
| mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>, | ||||||
| ) -> Result<NonNull<ArcInner<T>>, ()> { | ||||||
| let layout = Layout::new::<ArcInner<()>>() | ||||||
| .extend(value_layout) | ||||||
| .unwrap() | ||||||
| .0 | ||||||
| .pad_to_align(); | ||||||
|
|
||||||
| let ptr = NonNull::new(alloc::alloc::alloc(layout)).ok_or(())?; | ||||||
|
|
||||||
| // Initialize the ArcInner | ||||||
| let inner = mem_to_arcinner(ptr.as_ptr()); | ||||||
| debug_assert_eq!(unsafe { Layout::for_value(&*inner) }, layout); | ||||||
|
|
||||||
| unsafe { | ||||||
| ptr::write(&mut (*inner).count, atomic::AtomicUsize::new(1)); | ||||||
| // Safety | ||||||
|
|
||||||
| // 1. Caller ensures that value_layout is the layout of T | ||||||
| // 2. ArcInner is repr(C) | ||||||
| // 3. Thus, full_layout is layout of ArcInner<T> | ||||||
| let full_layout = Layout::new::<ArcInner<()>>() | ||||||
| .extend(value_layout) | ||||||
| .expect("layout too big") | ||||||
| .0 | ||||||
| .pad_to_align(); | ||||||
|
|
||||||
| // ArcInner never has a zero size | ||||||
| let ptr = alloc::alloc::alloc(full_layout); | ||||||
|
||||||
| let ptr = alloc::alloc::alloc(full_layout); | |
| let ptr = alloc::alloc::alloc(full_layout); |
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.
Probably do fix this.
(I should make sure this crate has rustfmt CI, but I'll do that later)
Uh oh!
There was an error while loading. Please reload this page.