2 releases

0.1.1 Aug 10, 2023
0.1.0 Feb 26, 2021

#1128 in Development tools

Download history 1076/week @ 2024-07-28 684/week @ 2024-08-04 307/week @ 2024-08-11 855/week @ 2024-08-18 803/week @ 2024-08-25 1066/week @ 2024-09-01 702/week @ 2024-09-08 800/week @ 2024-09-15 1433/week @ 2024-09-22 1380/week @ 2024-09-29 818/week @ 2024-10-06 736/week @ 2024-10-13 603/week @ 2024-10-20 1172/week @ 2024-10-27 1310/week @ 2024-11-03 905/week @ 2024-11-10

4,012 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