2 releases

0.1.1 Aug 10, 2023
0.1.0 Feb 26, 2021

#584 in Development tools

Download history 888/week @ 2024-12-16 181/week @ 2024-12-23 318/week @ 2024-12-30 1002/week @ 2025-01-06 980/week @ 2025-01-13 1064/week @ 2025-01-20 947/week @ 2025-01-27 2185/week @ 2025-02-03 2561/week @ 2025-02-10 2886/week @ 2025-02-17 3273/week @ 2025-02-24 3289/week @ 2025-03-03 4398/week @ 2025-03-10 4015/week @ 2025-03-17 3608/week @ 2025-03-24 2884/week @ 2025-03-31

15,009 downloads per month
Used in 2 crates (via ledger_device_sdk)

MIT/Apache

8KB
103 lines

Const-zero

Provides a const version of core::mem::zeroed().

Example Usage

Example usage:

use const_zero::const_zero;
struct OpaqueStruct {
    nothing: core::ffi::c_void,
};
static mut zeroed_opaque: OpaqueStruct = unsafe {const_zero!(OpaqueStruct)};

Ideally const_zero would be a generic function, but const generics need more development first (const_fn_transmute, const_generics, const_evaluatable_checked)

Differences with std::mem::zeroed

const_zero zeroes padding bits, while std::mem::zeroed doesn't

How does it work?

The simplified version is

union TypeAsBytes<T> {
    bytes: [u8; core::mem::size_of::<T>()],
    inner: T,
};

which can be initalized with

TypeAsBytes {bytes: [0; core::mem::size_of::<T>()]};

Feel free to use this trick in your code if you want to skip out on a dependency

No runtime deps