#no-std #no-alloc

no-std ptr_meta

A radioactive stabilization of the ptr_meta rfc

13 releases

0.3.1 Sep 29, 2025
0.3.0 Sep 11, 2024
0.3.0-pre1 Nov 4, 2023
0.3.0-alpha.3 Aug 11, 2024
0.1.2 Mar 17, 2021

#5 in Rust patterns

Download history 391098/week @ 2026-03-03 2362627/week @ 2026-03-10 1909747/week @ 2026-03-17 1701549/week @ 2026-03-24 1692202/week @ 2026-03-31 2039236/week @ 2026-04-07 2040066/week @ 2026-04-14 2023419/week @ 2026-04-21 2079393/week @ 2026-04-28 2104438/week @ 2026-05-05 2441847/week @ 2026-05-12 2463698/week @ 2026-05-19 2519701/week @ 2026-05-26 2242887/week @ 2026-06-02

10,013,129 downloads per month
Used in 2,368 crates (19 directly)

MIT license

23KB
335 lines

ptr_meta

crates.io badge docs badge license badge

A radioactive stabilization of the ptr_meta RFC.

Documentation

Example

// Get the associated metadata for pointers
let str = "hello world";
assert_eq!(ptr_meta::metadata(str), str.len());

let slice = &[1, 2, 3, 4, 5] as &[i32];
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);
println!("{} world!", unsafe { &*ptr }); // prints "hello world!"

// Derive Pointee on your own types
#[derive(ptr_meta::Pointee)]
#[repr(transparent)]
struct CoolStr {
    inner: str,
}

impl CoolStr {
    fn print_cool(&self) {
        println!("😎 {} 😎", &self.inner);
    }
}

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

// Implement Pointee for trait objects
#[ptr_meta::pointee]
trait Printable {
    fn print(&self);
}

impl Printable for i32 {
    fn print(&self) {
        println!("i32: {self}");
    }
}

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(),
    i32_vtable,
);
unsafe {
    (*printable).print(); // prints "i32: 100"
}

Dependencies

~64KB