Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/ptr_meta/example.md # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / ptr_meta / example.md` 52 lines ยท 1344 bytes Raw Blame History Rendered 1 ```rust 2 // Get the associated metadata for pointers 3 let str = "hello world"; 4 assert_eq!(ptr_meta::metadata(str), str.len()); 5 6 let slice = &[1, 2, 3, 4, 5] as &[i32]; 7 assert_eq!(ptr_meta::metadata(slice), slice.len()); 8 9 // Make your own wide pointers from data pointers and metadata 10 let bytes = [b'h', b'e', b'l', b'l', b'o']; 11 let ptr = ptr_meta::from_raw_parts::(bytes.as_ptr().cast(), 5); 12 println!("{} world!", unsafe { &*ptr }); // prints "hello world!" 13 14 // Derive Pointee on your own types 15 #[derive(ptr_meta::Pointee)] 16 #[repr(transparent)] 17 struct CoolStr { 18 inner: str, 19 } 20 21 impl CoolStr { 22 fn print_cool(&self) { 23 println!("๐Ÿ˜Ž {} ๐Ÿ˜Ž", &self.inner); 24 } 25 } 26 27 let ptr = ptr_meta::from_raw_parts::(bytes.as_ptr().cast(), 5); 28 let cool = unsafe { &*ptr }; 29 cool.print_cool(); // prints "๐Ÿ˜Ž hello ๐Ÿ˜Ž" 30 31 // Implement Pointee for trait objects 32 #[ptr_meta::pointee] 33 trait Printable { 34 fn print(&self); 35 } 36 37 impl Printable for i32 { 38 fn print(&self) { 39 println!("i32: {self}"); 40 } 41 } 42 43 let i32_vtable = ptr_meta::metadata(&0i32 as &dyn Printable); 44 let one_hundred = 100i32; 45 let printable = ptr_meta::from_raw_parts::( 46 (&one_hundred as *const i32).cast(), 47 i32_vtable, 48 ); 49 unsafe { 50 (*printable).print(); // prints "i32: 100" 51 } 52 ```