maxbla/const-zero


a const macro to replace `std::mem::zero`

License: Apache-2.0

Language: Rust


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

Project Statistics

Sourcerank 4
Repository Size 10.7 KB
Stars 2
Forks 0
Watchers 1
Open issues 0
Dependencies 0
Contributors 2
Tags 0
Created
Last updated
Last pushed

Top Contributors See all

Max Blachman René Kijewski

Packages Referencing this Repo

const-zero
a const mem::zeroed
Latest release 0.1.1 - Updated - 2 stars

Something wrong with this page? Make a suggestion

Last synced: 2023-08-10 12:55:04 UTC

Login to resync this repository