1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Aligned newtypes
//!
//! ```
//! use aligned_utils::stack::Align8;
//! let mut arr = Align8([1, 2, 3]);
//! let bytes: &[u8] = &*arr;
//! ```
//!

use core::ops::{Deref, DerefMut};

macro_rules! define_align_newtype {
    ($($(#[$id_attr:meta])* $id:ident: $align:tt,)+) => {
        $(
            $(#[$id_attr])*
            #[repr(align($align))]
            #[derive(Debug, Clone, Copy)]
            pub struct $id<T: ?Sized>(pub T);

            impl<T: ?Sized> Deref for $id<T> {
                type Target = T;
                fn deref(&self) -> &T {
                    &self.0
                }
            }

            impl<T: ?Sized> DerefMut for $id<T> {
                fn deref_mut(&mut self) -> &mut T {
                    &mut self.0
                }
            }
        )+

        #[cfg(test)]
        mod tests{
            use super::*;

            #[test]
            fn check_aligned_wrappers(){
                $(
                    {
                        let a = $id([0_u8;1]);
                        assert_eq!(core::mem::align_of_val(&a), $align);
                        assert_eq!(a.as_ptr() as usize % $align, 0);
                        assert_eq!(a.as_ref(), &[0_u8]);

                        #[cfg(feature="alloc")]
                        {
                            let b = alloc::boxed::Box::new(a);
                            let p: *const $id<[u8;1]> = &*b;
                            assert_eq!(p as usize % $align, 0);
                        }

                        let c: &$id<[u8]> = &a;
                        assert_eq!(c.as_ref(), &[0_u8]);
                    }
                )+
            }
        }
    };
}

define_align_newtype! (
    /// A newtype with alignment of at least 2 bytes
    Align2: 2,
    /// A newtype with alignment of at least 4 bytes
    Align4: 4,
    /// A newtype with alignment of at least 8 bytes
    Align8: 8,
    /// A newtype with alignment of at least 16 bytes
    Align16: 16,
    /// A newtype with alignment of at least 32 bytes
    Align32: 32,
    /// A newtype with alignment of at least 64 bytes
    Align64: 64,
    /// A newtype with alignment of at least 128 bytes
    Align128: 128,
    /// A newtype with alignment of at least 256 bytes
    Align256: 256,
    /// A newtype with alignment of at least 512 bytes
    Align512: 512,
    /// A newtype with alignment of at least 1024 bytes
    Align1024: 1024,
    /// A newtype with alignment of at least 2048 bytes
    Align2048: 2048,
    /// A newtype with alignment of at least 4096 bytes
    Align4096: 4096,
);