Macro const_zero::const_zero
source · macro_rules! const_zero { ($type_:ty) => { ... }; }
Expand description
A marco that acts similarly to core::mem::zeroed()
, only is const
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
)
Safety
Similar to core::mem::zeroed()
, except this zeroes padding bits. Zeroed
padding usually isn’t relevant to safety, but might be if a C union is used.
To repeat core::mem::zeroed()
’s safety, an all zero byte pattern might not
be a valid value for a type; for example, references &T, &mut T.
const_zero does not work on unsized types
ⓘ
use const_zero::const_zero;
// error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
const BYTES: [u8] = unsafe{const_zero!([u8])};
reference types trigger a (denied by default) lint and cause immediate undefined behavior if the lint is ignored
ⓘ
use const_zero::const_zero;
// error: any use of this value will cause an error
// note: `#[deny(const_err)]` on by default
const STR: &str = unsafe{const_zero!(&'static str)};
Differences with core::mem::zeroed
const_zero
zeroes padding bits, while core::mem::zeroed
doesn’t