| Crates.io | enum-ptr |
| lib.rs | enum-ptr |
| version | 0.2.0 |
| created_at | 2022-11-01 14:27:11.988054+00 |
| updated_at | 2024-10-11 00:52:10.171779+00 |
| description | Ergonomic tagged pointer |
| homepage | |
| repository | https://github.com/QuarticCat/enum-ptr |
| max_upload_size | |
| id | 702838 |
| size | 35,303 |
This crate provides a custom derive macro EnumPtr to generate bridges between an enum T and Compact<T> with minimum cost. Compact<T> is the compact representation of T, and it is only one pointer wide.
In other words, this crate is a library for defining tagged pointers in ergonomic way, even supporting different pointer types (&, Box, Arc, etc) as different enum variants.
For example, the following code
use enum_ptr::EnumPtr;
#[derive(EnumPtr)]
#[repr(C, usize)]
enum Foo<'a> {
A(&'a i32),
B(Box<i32>),
}
will generate
impl<'a> From<Foo<'a>> for Compact<Foo<'a>> {
// ...
}
impl<'a> From<Compact<Foo<'a>>> for Foo<'a> {
// ...
}
Since &i32 and Box<i32> are aligned by 4 bytes, the lowest 2 bits of them are always zeros. Compact<Foo<'a>> utilizes these bits to store the tag (discriminant).
no_stdcargo +nightly miri test with strict provenance enabled.$ cargo test
$ cargo +nightly miri test
This project is licensed under either of
at your option.