Trait palette::cast::UintCast

source ·
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 and T::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 from Self::Uint.
  • The type must not contain any internal padding.
  • The type must be repr(C) or repr(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.

Required Associated Types§

source

type Uint

An unsigned integer with the same size as Self.

Implementors§

source§

impl<O> UintCast for Packed<O, u8>

§

type Uint = u8

source§

impl<O> UintCast for Packed<O, u16>

§

type Uint = u16

source§

impl<O> UintCast for Packed<O, u32>

§

type Uint = u32

source§

impl<O> UintCast for Packed<O, u64>

§

type Uint = u64

source§

impl<O> UintCast for Packed<O, u128>

§

type Uint = u128

source§

impl<S> UintCast for Luma<S, u8>

§

type Uint = u8

source§

impl<S> UintCast for Luma<S, u16>

§

type Uint = u16

source§

impl<S> UintCast for Luma<S, u32>

§

type Uint = u32

source§

impl<S> UintCast for Luma<S, u64>

§

type Uint = u64

source§

impl<S> UintCast for Luma<S, u128>

§

type Uint = u128