assert-size-derive
A Rust procedural macro for compile-time type size assertions.
Overview
assert-size-derive provides a simple attribute macro that verifies types have the expected size in bytes at compile time. If the actual size doesn't match the expected size, compilation fails with a clear error message.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Apply the #[assert_size(N)] attribute to any type definition, where N is the expected size in bytes:
use assert_size;
union MyUnion
If the size doesn't match, you'll get a compile-time error:
// This will fail to compile!
Use Cases
- Prevent regressions: Catch unintended size changes from refactoring
- FFI safety: Ensure types meet specific memory layout requirements for C interop
- Serialization: Verify types have expected sizes for binary protocols
- Performance: Document and enforce size constraints for cache-friendly data structures
- Cross-platform compatibility: Detect platform-specific size variations
Features
- ✅ Zero runtime overhead - all checks happen at compile time
- ✅ Works with structs, enums, and unions
- ✅ Supports all type attributes like
#[repr(C)],#[repr(packed)], etc. - ✅ Clear error messages on size mismatches
- ✅ Simple syntax - just one attribute with the expected size
- ✅
no_stdcompatible - works in embedded and bare-metal environments
Examples
With repr attributes
Zero-sized types
;
Tuple structs
;
How It Works
The macro generates a compile-time assertion using core::mem::size_of and const evaluation:
const _: = assert!;
The type definition itself is preserved unchanged, so there's no impact on the generated code. Using core ensures compatibility with both std and no_std environments.
License
Licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.