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}
16
17/// Compile-time assertion that a type's size is <= `max_size` and alignment == `expected_align`.
18#[macro_export]
19macro_rules! static_assert_size_and_align {
20    ($ty:ty, $max_size:expr, $expected_align:expr $(,)?) => {
21        $crate::static_assert!(core::mem::size_of::<$ty>() <= $max_size as usize);
22        $crate::static_assert!(core::mem::align_of::<$ty>() == $expected_align as usize);
23    };
24}