Macro dungeon_cell::layout_for
source · macro_rules! layout_for { ($($type:ty),* $(,)?) => { ... }; ($($t:tt)*) => { ... }; }
Expand description
Calculate the Layout
needed to store a set of types.
This macro expects input of the form layout_for!(Type1, Type2, Type3)
.
The created Layout
type will have a size equal to the maximum size
of the given types, and will have an alignment equal to the maximum alignment
of the given types. If you want the layout for the combination of types Type1
,
Type2
, Type3
as one value then use layout_for!((Type1, Type2, Type3))
.
Examples
use dungeon_cell::layout_for;
// sizes
assert_eq!(<layout_for!()>::size(), 0);
assert_eq!(<layout_for!(())>::size(), 0);
assert_eq!(<layout_for!(u8)>::size(), 1);
assert_eq!(<layout_for!(u8, i8)>::size(), 1);
assert_eq!(<layout_for!(u32)>::size(), 4);
assert_eq!(<layout_for!(u32, i32, u8, i16, ())>::size(), 4);
assert_eq!(<layout_for!(u32, u8, String, &'static str, ())>::size(), 24);
assert_eq!(<layout_for!([u8; 100], [i32; 5], String)>::size(), 100);
// alignments
assert_eq!(<layout_for!()>::alignment(), 1);
assert_eq!(<layout_for!(u8)>::alignment(), 1);
assert_eq!(<layout_for!(i32)>::alignment(), 4);
assert_eq!(<layout_for!([u8; 100], [i32; 5], String)>::alignment(), 8);
Example Expansion
layout_for!(i32, u8)
expands to
::dungeon_cell::Layout<
::dungeon_cell::Size<
{
let mut max = 0;
let size = ::core::mem::size_of::<i32>();
if size > max {
max = size;
}
let size = ::core::mem::size_of::<u8>();
if size > max {
max = size;
}
max
},
>,
::dungeon_cell::Alignment<
{
let mut max = 1;
let align = ::core::mem::align_of::<i32>();
if align > max {
max = align;
}
let align = ::core::mem::align_of::<u8>();
if align > max {
max = align;
}
max
},
>,
>