Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ptr_meta/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ assert_eq!(ptr_meta::metadata(slice), slice.len());

// Make your own wide pointers from data pointers and metadata
let bytes = [b'h', b'e', b'l', b'l', b'o'];
let ptr = ptr_meta::from_raw_parts::<str>(bytes.as_ptr().cast(), 5);
let ptr = ptr_meta::from_raw_parts::<str>(bytes.as_ptr(), 5);
println!("{} world!", unsafe { &*ptr }); // prints "hello world!"

// Derive Pointee on your own types
Expand All @@ -24,7 +24,7 @@ impl CoolStr {
}
}

let ptr = ptr_meta::from_raw_parts::<CoolStr>(bytes.as_ptr().cast(), 5);
let ptr = ptr_meta::from_raw_parts::<CoolStr>(bytes.as_ptr(), 5);
let cool = unsafe { &*ptr };
cool.print_cool(); // prints "😎 hello 😎"

Expand All @@ -43,7 +43,7 @@ impl Printable for i32 {
let i32_vtable = ptr_meta::metadata(&0i32 as &dyn Printable);
let one_hundred = 100i32;
let printable = ptr_meta::from_raw_parts::<dyn Printable>(
(&one_hundred as *const i32).cast(),
(&one_hundred as *const i32),
i32_vtable,
);
unsafe {
Expand Down
14 changes: 10 additions & 4 deletions ptr_meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ unsafe impl Pointee for std::ffi::OsStr {
type Metadata = usize;
}

/// Pointers to types implementing this trait alias are “thin”.
///
/// This includes statically-Sized types and extern types.
pub trait Thin: Pointee<Metadata = ()> {}
impl<T: Pointee<Metadata = ()>> Thin for T {}

/// Returns the metadata of the given pointer.
///
/// `*mut T`, `&T`, and `&mut T` can all be passed directly to this function as
Expand Down Expand Up @@ -227,7 +233,7 @@ pub const fn to_raw_parts_mut<T: Pointee + ?Sized>(
/// [`slice::from_raw_parts`]: core::slice::from_raw_parts
#[inline]
pub const fn from_raw_parts<T: Pointee + ?Sized>(
data_address: *const (),
data_address: *const impl Thin,
metadata: <T as Pointee>::Metadata,
) -> *const T {
// SAFETY: Accessing the value from the `PtrRepr` union is safe since
Expand All @@ -236,7 +242,7 @@ pub const fn from_raw_parts<T: Pointee + ?Sized>(
unsafe {
PtrRepr {
components: PtrComponents {
data_address,
data_address: data_address.cast(),
metadata,
},
}
Expand All @@ -249,7 +255,7 @@ pub const fn from_raw_parts<T: Pointee + ?Sized>(
/// See [`from_raw_parts`] for more details.
#[inline]
pub const fn from_raw_parts_mut<T: Pointee + ?Sized>(
data_address: *mut (),
data_address: *mut impl Thin,
metadata: <T as Pointee>::Metadata,
) -> *mut T {
// SAFETY: Accessing the value from the `PtrRepr` union is safe since
Expand All @@ -258,7 +264,7 @@ pub const fn from_raw_parts_mut<T: Pointee + ?Sized>(
unsafe {
PtrRepr {
components: PtrComponents {
data_address,
data_address: data_address.cast(),
metadata,
},
}
Expand Down