1

rust: kernel: stop using ptr_metadata feature

The `byte_sub` method was stabilized in Rust 1.75.0. By using that
method, we no longer need the unstable `ptr_metadata` feature for
implementing `Arc::from_raw`.

This brings us one step closer towards not using unstable compiler
features.

Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Reviewed-by: Trevor Gross <tmgross@umich.edu>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20240215104601.1267763-1-aliceryhl@google.com
[ Reworded title. ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
Alice Ryhl 2024-02-15 10:46:01 +00:00 committed by Miguel Ojeda
parent e283ee2392
commit 44f2e626cb
2 changed files with 7 additions and 10 deletions

View File

@ -17,7 +17,6 @@
#![feature(dispatch_from_dyn)] #![feature(dispatch_from_dyn)]
#![feature(new_uninit)] #![feature(new_uninit)]
#![feature(offset_of)] #![feature(offset_of)]
#![feature(ptr_metadata)]
#![feature(receiver_trait)] #![feature(receiver_trait)]
#![feature(unsize)] #![feature(unsize)]

View File

@ -30,7 +30,7 @@ use core::{
mem::{ManuallyDrop, MaybeUninit}, mem::{ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
pin::Pin, pin::Pin,
ptr::{NonNull, Pointee}, ptr::NonNull,
}; };
use macros::pin_data; use macros::pin_data;
@ -239,22 +239,20 @@ impl<T: ?Sized> Arc<T> {
// binary, so its layout is not so large that it can trigger arithmetic overflow. // binary, so its layout is not so large that it can trigger arithmetic overflow.
let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 }; let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 };
let metadata: <T as Pointee>::Metadata = core::ptr::metadata(ptr); // Pointer casts leave the metadata unchanged. This is okay because the metadata of `T` and
// SAFETY: The metadata of `T` and `ArcInner<T>` is the same because `ArcInner` is a struct // `ArcInner<T>` is the same since `ArcInner` is a struct with `T` as its last field.
// with `T` as its last field.
// //
// This is documented at: // This is documented at:
// <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>. // <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
let metadata: <ArcInner<T> as Pointee>::Metadata = let ptr = ptr as *const ArcInner<T>;
unsafe { core::mem::transmute_copy(&metadata) };
// SAFETY: The pointer is in-bounds of an allocation both before and after offsetting the // SAFETY: The pointer is in-bounds of an allocation both before and after offsetting the
// pointer, since it originates from a previous call to `Arc::into_raw` and is still valid. // pointer, since it originates from a previous call to `Arc::into_raw` and is still valid.
let ptr = unsafe { (ptr as *mut u8).sub(val_offset) as *mut () }; let ptr = unsafe { ptr.byte_sub(val_offset) };
let ptr = core::ptr::from_raw_parts_mut(ptr, metadata);
// SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the // SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
// reference count held then will be owned by the new `Arc` object. // reference count held then will be owned by the new `Arc` object.
unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) } unsafe { Self::from_inner(NonNull::new_unchecked(ptr.cast_mut())) }
} }
/// Returns an [`ArcBorrow`] from the given [`Arc`]. /// Returns an [`ArcBorrow`] from the given [`Arc`].