Flagged Pointer
A safe Rust abstraction for creating tagged pointers that store additional flag information within the unused bits of aligned pointers.
Features
- Type-safe: Uses Rust's type system && compile time check to prevent misuse
- Zero-cost: No runtime overhead compared to raw tagged pointers
- Flexible: Works with various pointer types (Box,Rc,Arc,NonNull)
- Arbitrary Flags: Supports flags defined by enumflags2, also allow user define their own flag type
Usage
Basic Example
use FlaggedPtr;
use ;
let boxed = Boxnew;
let mut flagged = new;
assert_eq!;
assert_eq!;
// Update flags
flagged.set_flag;
assert_eq!;
// Extract original pointer and flags
let  = flagged.dissolve;
assert_eq!;
assert_eq!;
With Different Pointer Types
use *;
use Arc;
use ;
// With Box
let boxed:  = new;
// With Arc
let shared:  = new;
With Trait Objects
use *;
use ;
use pointee;
// With trait objects
let trait_obj:  = 
    new;
println!;
How It Works
The library exploits the fact that aligned pointers have unused low-order bits (e.g., 4-byte aligned pointers have 2 unused bits), stored pointer and flag bits are separated using bitwise operations, and the flag bits are stored in the unused bits of the pointer. Due to the platform reason, high bits of the pointer are not used but I am working in progress.
Limitations
For trait objects (e.g., 'dyn MyTrait'), their alignment cannot be determined in compile time, so the assertion can only be done in runtime.
Also, ptr_meta is not stable yet, so you have to use ptr_meta
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
License
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)
Changelog
0.1.0
- Initial release
- Basic tagged pointer implementation
- Support for common pointer types
- Comprehensive documentation