typelayout/
align.rs

1use typenum::*;
2use frunk::hlist::*;
3use crate::*;
4
5/// A computer for the alignment of a type.
6pub trait Align<R> {
7  /// The alignment of a type, in bytes.
8  type Value: Unsigned;
9}
10
11impl<R> Align<R> for ()   { type Value = U1;  }
12
13impl<R> Align<R> for f32  { type Value = U4;  }
14impl<R> Align<R> for f64  { type Value = U8;  }
15
16impl<R> Align<R> for i8   { type Value = U1;  }
17impl<R> Align<R> for i16  { type Value = U2;  }
18impl<R> Align<R> for i32  { type Value = U4;  }
19impl<R> Align<R> for i64  { type Value = U8;  }
20impl<R> Align<R> for i128 { type Value = U16; }
21
22impl<R> Align<R> for u8   { type Value = U1;  }
23impl<R> Align<R> for u16  { type Value = U2;  }
24impl<R> Align<R> for u32  { type Value = U4;  }
25impl<R> Align<R> for u64  { type Value = U8;  }
26impl<R> Align<R> for u128 { type Value = U16; }
27
28impl Align<C> for HNil {
29  type Value = U0;
30}
31
32impl<H, Tail> Align<C> for HCons<H, Tail>
33where
34  H: Align<C>,
35  Tail: Align<C>,
36
37  <H as Align<C>>::Value: Max<<Tail as Align<C>>::Value>,
38  Maximum<<H as Align<C>>::Value, <Tail as Align<C>>::Value>: Unsigned,
39{
40  /// The alignment of a struct is the maximum alignment of its fields.
41  type Value = Maximum<<H as Align<C>>::Value, <Tail as Align<C>>::Value>;
42}