This document contains implementation details and technical information for the diagnostics_tools crate.
The crate is organized into three main modules:
rta- Runtime assertions (Runtime-Time Assertions)cta- Compile-time assertions (Compile-Time Assertions)layout- Memory layout validation
All runtime assertion macros follow the pattern a_* (assertion):
a_true!(condition)- Assert condition is truea_false!(condition)- Assert condition is falsea_id!(left, right)- Assert values are identical (equal)a_not_id!(left, right)- Assert values are not identical
Debug variants (a_dbg_*) print values even when assertions pass:
a_dbg_true!(condition)a_dbg_false!(condition)a_dbg_id!(left, right)a_dbg_not_id!(left, right)
cta_true!(condition)- Compile-time boolean check usingcfgconditions
cta_type_same_size!(Type1, Type2)- Verify types have same sizecta_type_same_align!(Type1, Type2)- Verify types have same alignmentcta_ptr_same_size!(ptr1, ptr2)- Verify pointers have same sizecta_mem_same_size!(val1, val2)- Verify values have same memory size
The crate uses pretty_assertions internally to provide:
- Colored diff output
- Structured comparison formatting
- Better visual distinction between expected and actual values
Compile-time assertions use Rust's compile_error! macro combined with cfg attributes to validate conditions during compilation.
Memory layout assertions use:
core::mem::size_of::<T>()for size validationcore::mem::align_of::<T>()for alignment validation- Array length tricks to force compile-time evaluation
The crate supports several feature flags for conditional compilation:
enabled- Master switch for all functionality (default)diagnostics_runtime_assertions- Runtime assertion macros (default)diagnostics_compiletime_assertions- Compile-time assertion macros (default)diagnostics_memory_layout- Memory layout validation macros (default)no_std- Support for no_std environmentsfull- Enable all features
- Runtime assertions have the same overhead as standard
assert!macros - Debug variants have additional overhead for value formatting
- All assertions are removed in release builds unless explicitly enabled
- Compile-time assertions have zero runtime overhead
- They may slightly increase compilation time due to additional checking
- Memory layout assertions are resolved entirely at compile time
The crate uses a hierarchical namespace structure:
diagnostics_tools/
├── own/ - Direct exports
├── orphan/ - Re-exports from submodules
├── exposed/ - Extended API surface
└── prelude/ - Common imports
The runtime assertions integrate seamlessly with:
- Built-in Rust test framework (
#[test]) - Custom test harnesses
- Benchmark frameworks
The crate follows Rust's philosophy of "fail fast":
- Runtime assertions panic on failure (like standard assertions)
- Compile-time assertions prevent compilation on failure
- Clear, actionable error messages help identify root causes quickly
- Full support for all Rust-supported platforms
no_stdcompatibility for embedded systems- Consistent behavior across different architectures