Expand description
Compile-time assertions about constants, types, and more.
static_assertions
provides macros for compile-time assertions
that verify assumptions about constants, types, and traits.
All assertions execute at compile time with zero runtime cost. When an assertion fails, compilation stops with a clear error message, catching bugs early in the development process.
The crate is particularly valuable for library authors who need to enforce API contracts at compile time, such as ensuring types implement required traits, verifying type sizes match platform expectations, or validating constant expressions.
Key macros include const_assert!
for constant boolean expressions,
assert_impl_all!
to verify trait implementations,
assert_eq_size!
to check type sizes,
and assert_fields!
to confirm struct field presence.
§Examples
Assert constant boolean expressions:
use static_assertions::const_assert;
const BUFFER_SIZE: usize = 1024;
const MAX_SIZE: usize = 4096;
// Ensure buffer size is reasonable.
const_assert!(BUFFER_SIZE > 0);
const_assert!(BUFFER_SIZE <= MAX_SIZE);
const_assert!(BUFFER_SIZE.is_power_of_two());
Verify types implement required traits:
use static_assertions::assert_impl_all;
struct Config {
name: String,
enabled: bool,
}
// Ensure Config can be sent between threads and cloned.
assert_impl_all!(Config: Send, Sync, Clone);
impl Clone for Config {
fn clone(&self) -> Self {
Config {
name: self.name.clone(),
enabled: self.enabled,
}
}
}
Check type sizes match expectations:
use static_assertions::assert_eq_size;
#[repr(C)]
struct Header {
magic: u32,
version: u32,
}
// Ensure header is exactly 8 bytes for binary format compatibility.
assert_eq_size!(Header, [u8; 8]);
Modules§
- _core
- The Rust Core Library
Macros§
- assert_
cfg - Asserts that a given configuration is set.
- assert_
eq_ align - Asserts that types are equal in alignment.
- assert_
eq_ size - Asserts that types are equal in size.
- assert_
eq_ size_ ptr - Asserts that values pointed to are equal in size.
- assert_
eq_ size_ val - Asserts that values are equal in size.
- assert_
fields - Asserts that the type has the given fields.
- assert_
impl_ all - Asserts that the type implements all of the given traits.
- assert_
impl_ any - Asserts that the type implements any of the given traits.
- assert_
impl_ one - Asserts that the type implements exactly one in a set of traits.
- assert_
not_ impl_ all - Asserts that the type does not implement all of the given traits.
- assert_
not_ impl_ any - Asserts that the type does not implement any of the given traits.
- assert_
obj_ safe - Asserts that the traits support dynamic dispatch (object-safety).
- assert_
trait_ sub_ all - Asserts that the trait is a child of all of the other traits.
- assert_
trait_ super_ all - Asserts that the trait is a parent of all of the other traits.
- assert_
type_ eq_ all - Asserts that all types in a list are equal to each other.
- assert_
type_ ne_ all - Asserts that all types are not equal to each other.
- const_
assert - Asserts that constant expressions evaluate to
true
. - const_
assert_ eq - Asserts that constants are equal in value.
- const_
assert_ ne - Asserts that constants are not equal in value.