Skip to main content

zr/
static_assert.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/// Compile-time assertion.
6/// Fails to compile if the condition is false.
7#[macro_export]
8macro_rules! static_assert {
9    ($x:expr $(,)?) => {
10        const _: [(); 0 - !{
11            const ASSERT: bool = $x;
12            ASSERT
13        } as usize] = [];
14    };
15    ($x:expr, $($arg:tt)+) => {
16        const _: () = assert!($x, $($arg)+);
17    };
18}
19
20/// Compile-time assertion that a type's size is <= `max_size` and alignment == `expected_align`.
21#[macro_export]
22macro_rules! static_assert_size_and_align {
23    ($ty:ty, $max_size:expr, $expected_align:expr $(,)?) => {
24        $crate::static_assert!(core::mem::size_of::<$ty>() <= $max_size as usize);
25        $crate::static_assert!(core::mem::align_of::<$ty>() == $expected_align as usize);
26    };
27}