enum-ptr-derive 0.1.3

Ergonomic tagged pointer
Documentation

Enum Ptr

This crate provides a custom derive macro EnumPtr to automatically generate bridges between T and Compact<T> with the minimum cost. Compact<T> is the compact representation of T, and it is only one pointer wide.

For example, the following code

use enum_ptr::EnumPtr;

#[derive(EnumPtr)]
#[repr(C, usize)]
enum Foo<'a> {
    A(&'a i32),
    B(Option<Box<i32>>),
}

will generate

impl<'a> From<Foo<'a>> for enum_ptr::Compact<Foo<'a>> {
    // ...
}

impl<'a> From<enum_ptr::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 value).

Features

  • No need to write unsafe pointer operations
  • Supports various pointer types and can be extended
  • Minimum type conversion cost
  • Passes cargo +nightly miri test

Usage

Dependencies

[dependencies]
enum-ptr = "*"

This crate also supports no_std.

[dependencies]
enum-ptr = { version = "*", default-features = false }

Code

See docs.rs

Credits

  • Thanks to @oxalica for reviewing this crate and providing a lot of helpful suggestions.

License

This project is licensed under either of

at your option.