Skip to content

Commit 2a8024d

Browse files
committed
Feat: genericize ptr::from_raw_parts (rust-lang/rust#125701)
1 parent cc7a899 commit 2a8024d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

ptr_meta/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ unsafe impl Pointee for std::ffi::OsStr {
167167
type Metadata = usize;
168168
}
169169

170+
/// Pointers to types implementing this trait alias are “thin”.
171+
///
172+
/// This includes statically-Sized types and extern types.
173+
pub trait Thin: Pointee<Metadata = ()> {}
174+
impl<T: Pointee<Metadata = ()>> Thin for T {}
175+
170176
/// Returns the metadata of the given pointer.
171177
///
172178
/// `*mut T`, `&T`, and `&mut T` can all be passed directly to this function as
@@ -227,7 +233,7 @@ pub const fn to_raw_parts_mut<T: Pointee + ?Sized>(
227233
/// [`slice::from_raw_parts`]: core::slice::from_raw_parts
228234
#[inline]
229235
pub const fn from_raw_parts<T: Pointee + ?Sized>(
230-
data_address: *const (),
236+
data_address: *const impl Thin,
231237
metadata: <T as Pointee>::Metadata,
232238
) -> *const T {
233239
// SAFETY: Accessing the value from the `PtrRepr` union is safe since
@@ -236,7 +242,7 @@ pub const fn from_raw_parts<T: Pointee + ?Sized>(
236242
unsafe {
237243
PtrRepr {
238244
components: PtrComponents {
239-
data_address,
245+
data_address: data_address.cast(),
240246
metadata,
241247
},
242248
}
@@ -249,7 +255,7 @@ pub const fn from_raw_parts<T: Pointee + ?Sized>(
249255
/// See [`from_raw_parts`] for more details.
250256
#[inline]
251257
pub const fn from_raw_parts_mut<T: Pointee + ?Sized>(
252-
data_address: *mut (),
258+
data_address: *mut impl Thin,
253259
metadata: <T as Pointee>::Metadata,
254260
) -> *mut T {
255261
// SAFETY: Accessing the value from the `PtrRepr` union is safe since
@@ -258,7 +264,7 @@ pub const fn from_raw_parts_mut<T: Pointee + ?Sized>(
258264
unsafe {
259265
PtrRepr {
260266
components: PtrComponents {
261-
data_address,
267+
data_address: data_address.cast(),
262268
metadata,
263269
},
264270
}

0 commit comments

Comments
 (0)