#pointers #union #enums #thin

no-std ptr-union

Pointer union types the size of a pointer by storing the tag in the alignment bits

6 releases (stable)

2.2.2 Mar 25, 2022
2.2.1 Mar 16, 2022
2.1.0 May 27, 2020
2.0.0 Apr 26, 2020
1.0.0 Jan 31, 2020

#1344 in Rust patterns

Download history 48/week @ 2024-01-08 13/week @ 2024-01-15 23/week @ 2024-01-22 16/week @ 2024-02-05 52/week @ 2024-02-12 6/week @ 2024-02-19 19/week @ 2024-02-26 13/week @ 2024-03-04

91 downloads per month

MIT/Apache

61KB
939 lines

Pointer union types the size of a pointer by storing the tag in the alignment bits.

Changelist

2.2.2

Fixed

  • Crate now works under a strict provenance model. (In short, it doesn't use inttoptr.)
  • Crate is now tested with many more strictness Miri flags. Notably,
    • -Zmiri-check-number-validity and
    • -Zmiri-tag-raw-pointers

2.2.0

Added

  • Union8 and Union16 types. You can now use up to the four low bits for a tagged pointer.
  • Union* now has safe new_$variant and try_deref methods, and Enum* has a try_pack method. These do a dynamic alignment check that the pointer is sufficiently aligned, and allow Union types to be used without unsafe.

2.1.0

Added

  • fn Union*::as_untagged_ptr(&self) -> ErasedPtr

2.0.0

Fixes

  • Union types now drop their contents properly. (Whoops!) This is a breaking change for two main reasons:

    • Trait bounds must be added to the union type to have them on Drop
    • Copy can no longer be provided for unions of Copy pointers, because Drop and Copy are mutually exclusive.

    I also took this opportunity to clean up the builder proof API a little, as the previous shape was more difficult to use than intended.

How did this happen

We do run the test suite for this crate under [miri]. In fact, miri is how the leak was diagnosed and ensured to be fixed. However, the test suite previously did not actually attempt to drop any pointer union, and the author thought that it did. This combination let the lack of a Drop impl be overlooked.

Using previous versions

In short, you're better off not. However, if you must for some reason, make sure that any time you drop a pointer union, you call unpack. This will ensure that the inner types are properly dropped instead of leaking.

  • erasable: Erase pointers of their concrete type.
  • rc-borrow: Borrowed forms of Rc and Arc.
  • rc-box: Known unique forms of Rc and Arc.
  • slice-dst: Support for custom slice-based DSTs.

Minimum Supported Rust Version

We require a minimum Rust version of 1.41.0. This is for an adjustment of local trait impl checking.

Minimum version support is only guaranteed with minimal version resolution (-Z minimal-versions/--minimal-versions) due to how dependencies are handled. The minimum version of Rust will only be incremented with minor version bumps, not patch version bumps, and will be deliberate and clearly noted in change notes.

License

Licensed under either of

at your option.

If you are a highly paid worker at any company that prioritises profit over people, you can still use this crate. I simply wish you will unionise and push back against the obsession for growth, control, and power that is rampant in your workplace. Please take a stand against the horrible working conditions they inflict on your lesser paid colleagues, and more generally their disrespect for the very human rights they claim to fight for.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~22KB