rust_var_zeroed

Crates.iorust_var_zeroed
lib.rsrust_var_zeroed
version0.1.0
sourcesrc
created_at2021-06-27 19:01:49.976831
updated_at2021-06-27 19:01:49.976831
descriptionCreate zeroed structs with rust without Default trait
homepage
repositoryhttps://github.com/Morglod/rust_var_zeroed
max_upload_size
id415493
size4,011
Vladimir Kruzhkov (Morglod)

documentation

https://github.com/Morglod/rust_var_zeroed

README

rust_var_zeroed

Create structs with rust without Default trait

Similar to C/C++ (except var_stack_zeroed is zeroed):

struct MyStruct {
    int a: i32;
    MyStruct* ptr;
};

int main() {
    MyStruct my_var;
    memset(&my_var, 0, sizeof MyStruct);

    MyStruct* my_var2 = (MyStruct*)malloc(sizeof MyStruct);
    memset(&my_var2, 0, sizeof MyStruct);

    return 0;
}
struct MyStruct {
    a: i32,
    ptr: *mut MyStruct,
}

fn main() {
    // let mut my_var: &mut MyStruct
    var_stack_zeroed!(my_var, MyStruct);
    println!("{:?}", my_var.ptr); // -> 0x0

    // let mut my_var2: *mut MyStruct
    var_heap_zeroed!(my_var2, MyStruct);
    println!("{}", unsafe { (*my_var2).a }); // -> 0
}

Waiting for unstable Box features:
https://doc.rust-lang.org/beta/std/boxed/struct.Box.html#method.new_uninit

Commit count: 2

cargo fmt