From d2aa7ca9fe6aa7a94f92e600bab37cbe0aaa04f5 Mon Sep 17 00:00:00 2001 From: Intege-rs Date: Sun, 29 Dec 2024 14:01:25 -0500 Subject: [PATCH] alignment macro --- sub/core/src/align.rs | 26 ++++++++++++++++++++++++++ sub/core/src/lib.rs | 2 ++ 2 files changed, 28 insertions(+) create mode 100644 sub/core/src/align.rs diff --git a/sub/core/src/align.rs b/sub/core/src/align.rs new file mode 100644 index 0000000..db1d9db --- /dev/null +++ b/sub/core/src/align.rs @@ -0,0 +1,26 @@ + +pub struct Align( ::Alignment ) where Self: Aligned; +pub trait Aligned { type Alignment; } + +macro_rules! gen_alignment { + ($($alignment:literal),+) => { + $( + const _: () = { + #[repr(align($alignment))] pub struct Anon; + impl Aligned for Align<$alignment> { + type Alignment = Anon; + } + assert!(align_of::>() == $alignment) + }; + )+ + }; +} + +gen_alignment!( + 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, + 1024, 2048, 4096, 8192, 16384, 32768, + 65536, 131072, 262144, 524288, 1048576, + 2097152, 4194304, 8388608, 16777216, + 33554432, 67108864, 134217728, 268435456, + 536870912); + diff --git a/sub/core/src/lib.rs b/sub/core/src/lib.rs index bf11b8d..8e7e311 100644 --- a/sub/core/src/lib.rs +++ b/sub/core/src/lib.rs @@ -20,6 +20,7 @@ pub mod time; pub mod fnv1; pub mod pstruct; +pub mod align; // // Export Preludes: @@ -51,6 +52,7 @@ pub mod prelude { pub use crate::fnv1::*; pub use crate::pstruct::struct_offset; pub use crate::pod::{Pod,Mut}; + pub use crate::align::Align; } pub mod public {