3 releases
Uses new Rust 2024
| 0.1.2 | Nov 12, 2025 |
|---|---|
| 0.1.1 | Nov 7, 2025 |
| 0.1.0 | Nov 6, 2025 |
#1 in #canary
27,856 downloads per month
Used in unsafe-tools
4KB
62 lines
canary
canary wraps your types and checks for uninitialized memory. There is no reason to use this type in safe Rust,
but unsafe Rust or Rust that interacts with other languages over FFI may find this useful to find problematic behaviour.
Just wrap the type in question in Canary and it will automatically check for correct initialization on each Deref or DerefMut (when debug assertions are enabled).
use unsafe_tools_canary::Canary;
const CANARY: u64 = u64::from_le_bytes(*b"testmagc");
let mut my_val = Canary::<_, CANARY>::new(String::new());
assert_eq!(*my_val, "");
my_val.push_str("foo");
assert_eq!(*my_val, "foo");
When you attempt to access an incorrectly uninitialized type, it will panic:
use unsafe_tools_canary::Canary;
use std::mem::MaybeUninit;
const CANARY: u64 = u64::from_le_bytes(*b"testmagc");
let b: Canary<u64, CANARY> = unsafe { MaybeUninit::uninit().assume_init() };
// THIS WILL PANIC!
assert_eq!(*b, 42);