Macro constmuck::map_bound[][src]

macro_rules! map_bound {
    ($this : expr, | $bound : ident | $returned : expr $(,) *) => { ... };
    ($this : expr, | $($anything : tt) *) => { ... };
    ($this : expr, $function : expr $(,) *) => { ... };
}
Expand description

Maps the bound field of a TypeSize.

Example

Making a function to repeat a zeroed value, with stronger requirements than it needs.

use constmuck::map_bound;
use constmuck::{IsPod, IsZeroable, TypeSize, zeroed, zeroed_array};

use std::num::NonZeroU8;

pub const fn zeroed_pair<T, const SIZE: usize, const LEN: usize>(
    bound: TypeSize<T, IsPod<T>, SIZE>,
) -> (T, [T; LEN]) {
    // The type annotation is just for the reader
    let bound: TypeSize<T, IsZeroable<T>, SIZE> =
        map_bound!(bound, |x| x.is_zeroable);
    (zeroed(bound), zeroed_array(bound))
}

const PAIR_U8: (u8, [u8; 4]) = zeroed_pair(TypeSize!(u8));

const PAIR_NONE: (Option<NonZeroU8>, [Option<NonZeroU8>; 2]) =
    zeroed_pair(TypeSize!(Option<NonZeroU8>));

assert_eq!(PAIR_U8, (0, [0, 0, 0, 0]));

assert_eq!(PAIR_NONE, (None, [None, None]));