#initialization #canary #unsafe #checking #panic

unsafe-tools-canary

Runtime initialization checking for Rust types

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

Download history 19/week @ 2025-11-02 31/week @ 2025-11-09 4945/week @ 2025-11-16 7179/week @ 2025-11-23 5821/week @ 2025-11-30 3713/week @ 2025-12-07 4273/week @ 2025-12-14 3180/week @ 2025-12-21 2124/week @ 2025-12-28 3352/week @ 2026-01-04 5036/week @ 2026-01-11 6305/week @ 2026-01-18 7176/week @ 2026-01-25 6433/week @ 2026-02-01 7574/week @ 2026-02-08

27,856 downloads per month
Used in unsafe-tools

MIT license

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);

No runtime deps