pub unsafe trait UintCast {
type Uint;
}
Expand description
Marker trait for types that can be represented as an unsigned integer.
A type that implements this trait is assumed to have the exact same memory layout and representation as an unsigned integer, with the current compile target’s endianness. This implies a couple of useful properties:
- Casting between
T
andT::Uint
is free and will (or should) be optimized away. [T]
can be cast to and from[T::Uint]
.
This allows a number of common and useful optimizations, including casting buffers and reusing memory. It does however come with some strict requirements.
§Safety
- The type must be inhabited (eg: no Infallible).
- The type must allow any bit pattern (eg: either no requirements or some ability to recover from invalid values).
- The type must be either a wrapper around
Self::Uint
or be safe to transmute to and fromSelf::Uint
. - The type must not contain any internal padding.
- The type must be
repr(C)
orrepr(transparent)
. - The type must have the same size and alignment as
Self::Uint
.
Note also that the type is assumed to not implement Drop
. This will
rarely, if ever, be an issue. The requirements above ensures that the
underlying field types stay the same and will be dropped.