-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
```rust | ||
// 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" | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters