Enum Ptr
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 EnumPtr;
will generate
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).
Features
- No need to write unsafe pointer operations
- Supports various types and can be extended
- Supports
no_std - Minimum type conversion cost
- Passes
cargo +nightly miri testwith strict provenance enabled.
Testing
$ cargo test
$ cargo +nightly miri test
Credits
- Thanks to @oxalica for reviewing this crate and providing a lot of helpful suggestions.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.