Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/QuarticCat/enum-ptr
Ergonomic tagged pointer
https://github.com/QuarticCat/enum-ptr
rust
Last synced: 3 months ago
JSON representation
Ergonomic tagged pointer
- Host: GitHub
- URL: https://github.com/QuarticCat/enum-ptr
- Owner: QuarticCat
- License: apache-2.0
- Created: 2022-10-29T09:57:26.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-25T02:37:21.000Z (over 1 year ago)
- Last Synced: 2024-05-29T10:12:51.043Z (8 months ago)
- Topics: rust
- Language: Rust
- Homepage: https://crates.io/crates/enum-ptr
- Size: 155 KB
- Stars: 28
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Enum Ptr
[![crates.io](https://img.shields.io/crates/v/enum-ptr)](https://crates.io/crates/enum-ptr)
[![docs.rs](https://img.shields.io/badge/docs.rs-enum--ptr-latest)](https://docs.rs/enum-ptr)This crate provides a custom derive macro `EnumPtr` to generate bridges between an enum `T` and `Compact` with minimum cost. `Compact` 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
```rust
use enum_ptr::EnumPtr;#[derive(EnumPtr)]
#[repr(C, usize)]
enum Foo<'a> {
A(&'a i32),
B(Box),
}
```will generate
```rust
impl<'a> From> for Compact> {
// ...
}impl<'a> From>> for Foo<'a> {
// ...
}
```Since `&i32` and `Box` are aligned by 4 bytes, the lowest 2 bits of them are always zeros. `Compact>` 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 test` with strict provenance enabled.## Testing
```console
$ cargo test
$ cargo +nightly miri test
```## Credits
- Thanks to [@oxalica](https://github.com/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](/LICENSE-APACHE) or [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0))
- MIT license ([LICENSE-MIT](/LICENSE-MIT) or [http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT))at your option.