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, type_size};
use constmuck::{ImplsPod, ImplsZeroable, TypeSize, zeroed, zeroed_array};
use std::num::NonZeroU8;
pub const fn zeroed_pair<T, const SIZE: usize, const LEN: usize>(
bound: TypeSize<ImplsPod<T>, T, SIZE>,
) -> (T, [T; LEN]) {
// The type annotation is just for the reader
let bound: TypeSize<ImplsZeroable<T>, T, SIZE> =
map_bound!(bound, |x| x.impls_zeroable);
(zeroed(bound), zeroed_array(bound))
}
const PAIR_U8: (u8, [u8; 4]) = zeroed_pair(type_size!(u8));
const PAIR_NONE: (Option<NonZeroU8>, [Option<NonZeroU8>; 2]) =
zeroed_pair(type_size!(Option<NonZeroU8>));
assert_eq!(PAIR_U8, (0, [0, 0, 0, 0]));
assert_eq!(PAIR_NONE, (None, [None, None]));