3 ms·
A much better way to do partial initialization is by splitting up the struct into multiple parts. This can be easily done in safe rust with Option, or MaybeUnin
by ATsch 5y ago
A much better way to do partial initialization is by splitting up the struct into multiple parts. This can be easily done in safe rust with Option, or MaybeUninit if you're really desperate for performance.
- deleted 5y ago[deleted]
- bodhiandpysics1 5y agoWhen dealing with unix syscalls, you actually sometimes need to pass structs that aren't fully initialized, or are initialized to zero with the exception of some fields. The quintessential example is the sigaction struct.
- pjmlp 5y agoAnother good example is Win32, in many cases only the length is initialized and the API does the rest, this allows them to change the ABI across versions without impacting the caller.
- ATsch 5y agoThat should work perfectly with multiple structs. Define #[repr("C")] struct MyThing { length: usize, data: MaybeUninit<MyInner> } Then initialize with sizeof(data) and MaybeUninit::uninitialized(). When the call is complete, assume_init() and access the fields of the result struct as normal.
- pjmlp 5y agoI see, thanks.