Crates.io | rust_var_zeroed |
lib.rs | rust_var_zeroed |
version | 0.1.0 |
source | src |
created_at | 2021-06-27 19:01:49.976831 |
updated_at | 2021-06-27 19:01:49.976831 |
description | Create zeroed structs with rust without Default trait |
homepage | |
repository | https://github.com/Morglod/rust_var_zeroed |
max_upload_size | |
id | 415493 |
size | 4,011 |
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