You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When working with FFI it's common to have large structs full of integers. In C/C++ these are idiomatically initialized to all-zero with memset. In Rust, the equivalent requires unsafe with unsafe { core::mem::zeroed() }. The Rust version also requires the programmer to verify that all fields are safe to zero-initialize, which is not true of some patterns such as "struct full of function pointers as pseudo-vtable" .
Motivation, use-cases
Given a struct like the following (used for calling Linux's statx syscall):
Without using unsafe initialization is less performant, and also an absolute giant pain because of all the fields. There's also increased maintenance overhead as the unused padding fields are filled in by upstream projects.
However, this is pretty much the perfect case for a struct that is safe to initialize with an all-zero byte pattern via core::mem::zeroed. It's also possible for the compiler to recognize this is safe, since it knows that (1) plain integers are safe to zero-initialize and (2) the struct (recursively) contains only plain integers.
Solution sketches
If there was a trait that could be derived to indicate "this struct is safe to initialize with zeros", then the compiler could verify the safety, and the programmer could use safe code to initialize it.
Compilation would fail if the safety can't be proven:
// all of these cases fail to compile#[derive(ZeroInit)]structnot_zero_init_safe_1{a:Option<u32>,}#[derive(ZeroInit)]structnot_zero_init_safe_2{a:&'staticu32,}#[derive(ZeroInit)]structnot_zero_init_safe_3{a:*constu32,}#[derive(ZeroInit)]structnot_zero_init_safe_4{a:fn() -> u32,}#[derive(ZeroInit)]structnot_zero_init_safe_5{a:(u32,u32),}
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered:
Proposal
Problem statement
When working with FFI it's common to have large structs full of integers. In C/C++ these are idiomatically initialized to all-zero with
memset
. In Rust, the equivalent requires unsafe withunsafe { core::mem::zeroed() }
. The Rust version also requires the programmer to verify that all fields are safe to zero-initialize, which is not true of some patterns such as "struct full of function pointers as pseudo-vtable" .Motivation, use-cases
Given a struct like the following (used for calling Linux's
statx
syscall):Without using
unsafe
initialization is less performant, and also an absolute giant pain because of all the fields. There's also increased maintenance overhead as the unused padding fields are filled in by upstream projects.However, this is pretty much the perfect case for a struct that is safe to initialize with an all-zero byte pattern via
core::mem::zeroed
. It's also possible for the compiler to recognize this is safe, since it knows that (1) plain integers are safe to zero-initialize and (2) the struct (recursively) contains only plain integers.Solution sketches
If there was a trait that could be derived to indicate "this struct is safe to initialize with zeros", then the compiler could verify the safety, and the programmer could use safe code to initialize it.
Rough example:
Compilation would fail if the safety can't be proven:
Links and related work
https://internals.rust-lang.org/t/request-iszero-trait-and-derive-iszero/12636/14 (similar name, different semantics)
rust-lang/rust#60978
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: