Expand description
Assertions to ensure correct assumptions about constants, types, and more.
All checks provided by this crate are performed at compile-time. This allows for finding errors quickly and early when it comes to ensuring certain features or aspects of a codebase. These macros are especially important when exposing a public API that requires types to be the same size or implement certain traits.
§Usage
This crate is available on crates.io and can be used by adding the
following to your project’s Cargo.toml
:
[dependencies]
static_assertions = "1.1.0"
and this to your crate root (main.rs
or lib.rs
):
#[macro_use]
extern crate static_assertions;
When using Rust 2018 edition, the following shorthand can help if
having #[macro_use]
is undesirable.
extern crate static_assertions as sa;
sa::const_assert!(true);
§Procedural Extensions
As an extension crate proc_static_assertions
adds a number of new
assertions to this. These are implemented as procedural macros, hence the
“proc” prefix. As a result, they have a bit more visibility over what’s
being asserted over than normal macros would.
It can be enabled via the proc
feature flag in your Cargo.toml
:
[dependencies]
static_assertions = { version = "1.1.0", features = ["proc"] }
§Examples
Very thorough examples are provided in the docs for each individual macro. Failure case examples are also documented.
§Changes
See CHANGELOG.md
for an exhaustive list of what has changed from one version to another.
§Donate
This project is made freely available (as in free beer), but unfortunately not all beer is free! So, if you would like to buy me a beer (or coffee or more), then consider supporting my work that’s benefited your project and thousands of others.


Macros§
- assert_
align_ eq - Asserts that the types’ alignments are equal.
- assert_
align_ ge - Asserts that the types’ alignments are greater than or equal to each other.
- assert_
align_ gt - Asserts that the types’ alignments are greater than each other.
- assert_
align_ le - Asserts that the types’ alignments are less than or equal to each other.
- assert_
align_ lt - Asserts that the types’ alignments are less than each other.
- assert_
align_ ne - Asserts that the types’ alignments are not equal.
- assert_
cfg - Asserts that a given configuration is set.
- assert_
eq_ align Deprecated - Asserts that the types’ alignments are equal.
- assert_
eq_ size Deprecated - Asserts that types are equal in alignment.
- assert_
eq_ size_ ptr Deprecated - Asserts that values pointed to are equal in size.
- assert_
eq_ size_ val Deprecated - Asserts that values pointed to are equal in size.
- assert_
fields - Asserts that the type has the given fields.
- assert_
impl - Asserts that the type implements a logical trait expression.
- 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_ not_ all - Asserts that the type does not implement all of the given traits.
- assert_
impl_ not_ any - Asserts that the type does not implement any of the given traits.
- assert_
impl_ one - Asserts that the type implements exactly one in a set of traits.
- assert_
not_ impl_ all Deprecated - Asserts that the type does not implement all of the given traits.
- assert_
not_ impl_ any Deprecated - 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_
size_ eq - Asserts that types are equal in size.
- assert_
size_ eq_ ptr - Asserts that values pointed to are equal in size.
- assert_
size_ eq_ val - Asserts that values are equal in size.
- assert_
trait_ sub_ all - Asserts that the trait is a child of all of the other traits.
- assert_
trait_ sub_ any - Asserts that the trait is a child of one or more 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_ eq_ usize - Asserts that constants of type
usize
are equal in value. - const_
assert_ ge - Asserts that constants are less than or equal to each other.
- const_
assert_ gt - Asserts that constants are greater than each other.
- const_
assert_ le - Asserts that constants are less than or equal to each other.
- const_
assert_ lt - Asserts that constants are less than each other.
- const_
assert_ ne - Asserts that constants are not equal in value.
- does_
impl - Returns
true
if the type does implement a logical trait expression.