pub unsafe trait CompactRepr<R>: Copy + Sized {
const UNUSED_SENTINEL: R;
}Expand description
§Safety
Implementors must guarantee:
- For every
Tvalue stored viaCompactOption::some, the transmutedRbit pattern must not equalCompactRepr::UNUSED_SENTINEL. - Non-sentinel
Rvalues used asSomepayloads must be sound to transmute back toTunder the sameAssumebundle used byCompactOptionforTransmuteFrombetweenRandT. - 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§
Sourceconst UNUSED_SENTINEL: R
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.