serde_hkx/bytes/align.rs
1//! Alignment functions
2
3/// Get the aligned value.
4/// - [Original code (Rust lang)](https://github.com/rust-lang/rust/blob/1.30.0/src/libcore/alloc.rs#L199-L219)
5///
6/// # Examples
7///
8/// ```rust
9/// use serde_hkx::align;
10///
11/// assert_eq!(align!(10_u32, 4_u32), 12);
12/// assert_eq!(align!(10_usize, 8_usize), 16);
13/// ```
14#[macro_export]
15macro_rules! align {
16 ($offset:expr, $align_num:expr) => {
17 $offset.wrapping_add($align_num).wrapping_sub(1) & !$align_num.wrapping_sub(1)
18 };
19}
20
21#[cfg(test)]
22mod tests {
23 #[test]
24 fn test_align() {
25 assert_eq!(align!(88_u64, 16_u64), 96);
26 }
27}