derive_dst
Provides #[derive(DST)] for #[repr(C)] structs whose last field is an unsized slice
([T]) — the usual way to mirror a C/C++ "flexible array member" struct such as
SP_DEVICE_INTERFACE_DETAIL_DATA_W.
tl;dr
Requirements
The following will be validated by the macro and produce compilation errors with helpful hints if not upheld.
- pure
#[repr(C)](notpacked, not customalign-ed, not inside acfg_attrgate) - all head fields (all except last field) are
Sized - the dynamically-sized tail field (last field) is a slice
[T]whereT: Sized + Pointee<Metadata = usize> - your crate allows
#![feature(ptr_metadata)]
SAFETY
- The constructors
new_uninit_for_sizeandnew_uninit_for_countreturn a mirror type with all fields as zero-initialisedMaybeUninit<_> - You should create your own constructor which
- calls a suitable uninit constructor
- ensures all fields are correctly initialised (either where zero is not valid for the stored type or where your use case has additional specific requirements)
- then calls
assume_initon the uninitialised mirror The macro ensures that: - the types are well laid out, aligned & padded
- the mirror type is zero initialised
- the conversion from mirror to original is sound if all fields are correctly initialised You must ensure that:
- all fields are correctly initialised, either by using
MaybeUninit::writeor by accepting zero initialisation as valid, before callingassume_init
Why
The nomicon finishes the section on DSTs with the honest statement
(Yes, custom DSTs are a largely half-baked feature for now.)
and yet, various ffi functions make use of them.
Often this is to provide a filepath or other
string as the final field (e.g. SP_DEVICE_INTERFACE_DETAIL_DATA_W on Windows,
inotify_event on linux). Even with null-terminated strings, you still need to
allocate a buffer in advance. Sometimes it's a dynamically-sized tail of structured data
(e.g. fiemap on linux).
So do you:
- make a developer-time guess and hope your buffer is always big enough, wasting any oversizing on every call, or ...
- find a way to allocate a DST at runtime, when you know the exact size needed?
What you get
The case which gave me the pattern for this macro:
use io;
use DST;
/// A dynamically sized ffi-compatible version of `SP_DEVICE_INTERFACE_DETAIL_DATA_W`
Behind the scenes this generates (an import-collision-safe version of).
The doc-comments are included in the generated code, so you can read them locally and in IDE
/// `MaybeUninit` mirror of [`DeviceDetails`]: identical `repr(C)` layout, but every field is
/// wrapped in `MaybeUninit`. Constructed by `new_uninit_for_...` methods
/// converted back to the initialised type with [`Self::assume_init`].
Requirements
- Nightly toolchain (and
feature(ptr_metadata)not forbidden in config.toml) #![feature(ptr_metadata)]- DST must:
- be
#[repr(C)], no additional alignment or packing specification - have a single` unsized field as the tail (last field) as per the nomicon
- have a slice
[T]as the unsized tail
- be
SAFETY
We take extreme care to catch as many incorrect usages as possible at compile-time. Deliberately or accidentally circumventing the above requirements will most likely lead to UB.
We recommend you build the docs locally via cargo doc --document-private-items to review the
requirements for assume_init
The UninitMirror type is zero-initialised and not guaranteed to be valid. All fields must
be suitably initialised, including all elements of the tail slice, before calling
assume_init.
Alternatives
- Hand-roll your own DSTs using the same pattern (if you do, please star the repo on github so I know it's useful)
- If you don't have an API that can report the required size at runtime and need to trial-and-error
grobhandles growable buffers - Create a fixed, hopefully oversized, buffer type
- If you are staying in rust just
Boxthe unsized type within the struct and move on with all your hair intact!