rename rcbox in other places as per review comments

This commit is contained in:
Jonathan Dönszelmann 2024-10-13 21:08:47 +02:00
parent 0a9c87b1f5
commit 9e0a7b99b5
No known key found for this signature in database

View File

@ -289,7 +289,7 @@ struct RcInner<T: ?Sized> {
} }
/// Calculate layout for `RcInner<T>` using the inner value's layout /// Calculate layout for `RcInner<T>` using the inner value's layout
fn rcbox_layout_for_value_layout(layout: Layout) -> Layout { fn rc_inner_layout_for_value_layout(layout: Layout) -> Layout {
// Calculate layout using the given value layout. // Calculate layout using the given value layout.
// Previously, layout was calculated on the expression // Previously, layout was calculated on the expression
// `&*(ptr as *const RcInner<T>)`, but this created a misaligned // `&*(ptr as *const RcInner<T>)`, but this created a misaligned
@ -2009,17 +2009,17 @@ impl<T: ?Sized> Rc<T> {
/// Allocates an `RcInner<T>` with sufficient space for /// Allocates an `RcInner<T>` with sufficient space for
/// a possibly-unsized inner value where the value has the layout provided. /// a possibly-unsized inner value where the value has the layout provided.
/// ///
/// The function `mem_to_rcbox` is called with the data pointer /// The function `mem_to_rc_inner` is called with the data pointer
/// and must return back a (potentially fat)-pointer for the `RcInner<T>`. /// and must return back a (potentially fat)-pointer for the `RcInner<T>`.
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
unsafe fn allocate_for_layout( unsafe fn allocate_for_layout(
value_layout: Layout, value_layout: Layout,
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>, allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
mem_to_rcbox: impl FnOnce(*mut u8) -> *mut RcInner<T>, mem_to_rc_inner: impl FnOnce(*mut u8) -> *mut RcInner<T>,
) -> *mut RcInner<T> { ) -> *mut RcInner<T> {
let layout = rcbox_layout_for_value_layout(value_layout); let layout = rc_inner_layout_for_value_layout(value_layout);
unsafe { unsafe {
Rc::try_allocate_for_layout(value_layout, allocate, mem_to_rcbox) Rc::try_allocate_for_layout(value_layout, allocate, mem_to_rc_inner)
.unwrap_or_else(|_| handle_alloc_error(layout)) .unwrap_or_else(|_| handle_alloc_error(layout))
} }
} }
@ -2028,21 +2028,21 @@ unsafe fn allocate_for_layout(
/// a possibly-unsized inner value where the value has the layout provided, /// a possibly-unsized inner value where the value has the layout provided,
/// returning an error if allocation fails. /// returning an error if allocation fails.
/// ///
/// The function `mem_to_rcbox` is called with the data pointer /// The function `mem_to_rc_inner` is called with the data pointer
/// and must return back a (potentially fat)-pointer for the `RcInner<T>`. /// and must return back a (potentially fat)-pointer for the `RcInner<T>`.
#[inline] #[inline]
unsafe fn try_allocate_for_layout( unsafe fn try_allocate_for_layout(
value_layout: Layout, value_layout: Layout,
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>, allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
mem_to_rcbox: impl FnOnce(*mut u8) -> *mut RcInner<T>, mem_to_rc_inner: impl FnOnce(*mut u8) -> *mut RcInner<T>,
) -> Result<*mut RcInner<T>, AllocError> { ) -> Result<*mut RcInner<T>, AllocError> {
let layout = rcbox_layout_for_value_layout(value_layout); let layout = rc_inner_layout_for_value_layout(value_layout);
// Allocate for the layout. // Allocate for the layout.
let ptr = allocate(layout)?; let ptr = allocate(layout)?;
// Initialize the RcInner // Initialize the RcInner
let inner = mem_to_rcbox(ptr.as_non_null_ptr().as_ptr()); let inner = mem_to_rc_inner(ptr.as_non_null_ptr().as_ptr());
unsafe { unsafe {
debug_assert_eq!(Layout::for_value_raw(inner), layout); debug_assert_eq!(Layout::for_value_raw(inner), layout);
@ -3784,7 +3784,7 @@ fn new(for_value: &T, alloc: A) -> UniqueRcUninit<T, A> {
let ptr = unsafe { let ptr = unsafe {
Rc::allocate_for_layout( Rc::allocate_for_layout(
layout, layout,
|layout_for_rcbox| alloc.allocate(layout_for_rcbox), |layout_for_rc_inner| alloc.allocate(layout_for_rc_inner),
|mem| mem.with_metadata_of(ptr::from_ref(for_value) as *const RcInner<T>), |mem| mem.with_metadata_of(ptr::from_ref(for_value) as *const RcInner<T>),
) )
}; };
@ -3820,10 +3820,10 @@ fn drop(&mut self) {
// * new() produced a pointer safe to deallocate. // * new() produced a pointer safe to deallocate.
// * We own the pointer unless into_rc() was called, which forgets us. // * We own the pointer unless into_rc() was called, which forgets us.
unsafe { unsafe {
self.alloc self.alloc.take().unwrap().deallocate(
.take() self.ptr.cast(),
.unwrap() rc_inner_layout_for_value_layout(self.layout_for_value),
.deallocate(self.ptr.cast(), rcbox_layout_for_value_layout(self.layout_for_value)); );
} }
} }
} }