tagged-pointer-as-enum
A set of structs, traits and macros to implement tagged pointers.
Basic usage
// import macro
use tagged_enum;
// declare it like a normal enum but inside tagged_enum! macro
tagged_enum!
// macro generates constructors:
let a = A;
let b = B;
let c = C;
// and a helper module with tags
assert_eq!;
assert_eq!;
assert_eq!;
// these tags can be used to check variant of enum
assert_eq!;
assert_eq!;
assert_eq!;
// only variants that behave like containers can be borrowed
assert_eq!;
// borrowing values variants is impossible
// because there's no memory location containing value WITHOUT tag
// of course, you can get values back
assert_eq!;
assert_eq!;
assert_eq!;
Custom variant types
By default the following types can be used as variants:
u8
u16
u32
i8
i16
i32
Box<T>
Option<Box<T>>
It is possible to use other types by implementing TaggedPointerValue
for them:
use ;
// even if it looks like a marker trait in fact it's not
tagged_enum!
let custom = Custom;
let unwrapped = custom.;
assert_eq!;
assert_eq!;
Implementing default traits
Out of the box tagged_enum!
macro generates a struct that doesn't implement any builtin traits.
It is possible to attach #[derive(...)]
metadata similar to how it's usually attached to native Rust enums, however all variant types must also implement these traits.
Only the following traits are supported
Debug
Clone
PartialEq
Eq
Also, Drop
is auto-implemented for all tagged enums.
use ;
// implement comparison by absolute value
tagged_enum!
// variants ARE equal if they have the same tag and value
assert_eq!;
// variants ARE NOT equal if tags are different
assert_ne!;
// variants ARE NOT equal if values are different
assert_ne!;