Crates.io | const-zero |
lib.rs | const-zero |
version | 0.1.1 |
source | src |
created_at | 2021-02-26 14:06:38.142199 |
updated_at | 2023-08-10 12:52:21.332979 |
description | a const mem::zeroed |
homepage | |
repository | https://github.com/maxbla/const-zero |
max_upload_size | |
id | 360933 |
size | 17,801 |
Provides a const version of core::mem::zeroed()
.
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
)
std::mem::zeroed
const_zero
zeroes padding bits, while std::mem::zeroed
doesn't
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