Skip to content

Commit 44f2e62

Browse files
Darksonnojeda
authored andcommitted
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 <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: Trevor Gross <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Reworded title. ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent e283ee2 commit 44f2e62

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

rust/kernel/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#![feature(dispatch_from_dyn)]
1818
#![feature(new_uninit)]
1919
#![feature(offset_of)]
20-
#![feature(ptr_metadata)]
2120
#![feature(receiver_trait)]
2221
#![feature(unsize)]
2322

rust/kernel/sync/arc.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use core::{
3030
mem::{ManuallyDrop, MaybeUninit},
3131
ops::{Deref, DerefMut},
3232
pin::Pin,
33-
ptr::{NonNull, Pointee},
33+
ptr::NonNull,
3434
};
3535
use macros::pin_data;
3636

@@ -239,22 +239,20 @@ impl<T: ?Sized> Arc<T> {
239239
// binary, so its layout is not so large that it can trigger arithmetic overflow.
240240
let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 };
241241

242-
let metadata: <T as Pointee>::Metadata = core::ptr::metadata(ptr);
243-
// SAFETY: The metadata of `T` and `ArcInner<T>` is the same because `ArcInner` is a struct
244-
// with `T` as its last field.
242+
// Pointer casts leave the metadata unchanged. This is okay because the metadata of `T` and
243+
// `ArcInner<T>` is the same since `ArcInner` is a struct with `T` as its last field.
245244
//
246245
// This is documented at:
247246
// <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
248-
let metadata: <ArcInner<T> as Pointee>::Metadata =
249-
unsafe { core::mem::transmute_copy(&metadata) };
247+
let ptr = ptr as *const ArcInner<T>;
248+
250249
// SAFETY: The pointer is in-bounds of an allocation both before and after offsetting the
251250
// pointer, since it originates from a previous call to `Arc::into_raw` and is still valid.
252-
let ptr = unsafe { (ptr as *mut u8).sub(val_offset) as *mut () };
253-
let ptr = core::ptr::from_raw_parts_mut(ptr, metadata);
251+
let ptr = unsafe { ptr.byte_sub(val_offset) };
254252

255253
// SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
256254
// reference count held then will be owned by the new `Arc` object.
257-
unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) }
255+
unsafe { Self::from_inner(NonNull::new_unchecked(ptr.cast_mut())) }
258256
}
259257

260258
/// Returns an [`ArcBorrow`] from the given [`Arc`].

0 commit comments

Comments
 (0)