Skip to main content

CompactRepr

Trait CompactRepr 

Source
pub unsafe trait CompactRepr<R>: Copy + Sized {
    const UNUSED_SENTINEL: R;
}
Expand description

§Safety

Implementors must guarantee:

  1. For every T value stored via CompactOption::some, the transmuted R bit pattern must not equal CompactRepr::UNUSED_SENTINEL.
  2. Non-sentinel R values used as Some payloads must be sound to transmute back to T under the same Assume bundle used by CompactOption for TransmuteFrom between R and T.
  3. If you care about logical round-tripping, transmuting that raw value back yields an equivalent T.

§Choosing UNUSED_SENTINEL

Pick an R value that is not the transmuted bit pattern of any T you will ever store as Some. If the sentinel aliases a valid Some encoding, NONE and Some collide and the type becomes logically unusable.

§Validation

After changing an unsafe impl CompactRepr, run cargo miri test (or your project’s Miri CI) to exercise transmute-based paths under the stacked borrows / provenance model.

§Procedural macro

Enable the macros crate feature for a re-exported #[compact_option(...)] attribute, or depend on the compact-option-proc-macro crate directly.

The #[compact_option(repr(R = …, sentinel = …))] macro only emits unsafe impl CompactRepr; it does not validate #[repr], discriminants, or sentinel collisions. Structs additionally get size_of / align_of checks against R. See the proc-macro crate’s rustdoc and Miri for safety review.

Required Associated Constants§

Source

const UNUSED_SENTINEL: R

Raw value reserved for CompactOption::NONE.

§Safety (encoding)

This bit pattern must never equal the transmuted R encoding of any T you store via CompactOption::some. If it does, NONE and Some collide: CompactOption::is_none may return true for a value you constructed with some, and CompactOption::try_unwrap returns None.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§