Closed jsgf closed 3 months ago
Not sure why, but when I run
Oh, cargo fmt
locally it wants to change things all over the place.cargo +nightly fmt
This doesn't handle the case of a C struct pointing to a DST object (ie thin vs fat pointers).
I've updated the API to:
struct MyStruct<FAM: ?Sized = [u8; 0]>
- ie add a type parameter for the flexible array member, which can be dynamically sized but by default is a zero-sized array to make the structure sized. This means that all other references to this type are to the sized variant (without parameter qualification) which makes it equivalent to using __IncompleteArrayField
.flex_ref
, flex_ref_mut
, flex_ptr
, flex_ptr_mut
to convert a sized reference/pointer to an unsized one. These are unsafe, because you must provide the size of the flexible array correctly.struct MyStruct<[u8]>
has methods fixed
, fixed_mut
to return the sized structure along with the length. It also has layout
which returns the size & alignment needed for a memory allocation.I've been using this API for one of my own projects, and so far it feels pretty comfortable.
I also changed the generation of PhantomData fields for type parameters to put them at the start of the structure. This was necessary as putting them at the end interfered with the FAM field which must be last. The side-effect of this is a lot of test fixtures changed in a trivial way.
OK updated with comments (and still on top of #2783)
:umbrella: The latest upstream changes (presumably 3b5ce9c5861cd2e97e9789f5b686238656abd2d6) made this pull request unmergeable. Please resolve the merge conflicts.
yeah we literally have the experimental
flag on the CLI and a feature as well. I think other features are gated behind it already.
Does --experimental
do anything? I couldn't see anywhere that referenced it.
@emilio @pvdrz Ping? Are there any outstanding concerns about this?
Fixed:
flex_mut_ref
vs flex_ptr_mut
names (went with trailing _mut
) @emilio @pvdrz Ping?
:umbrella: The latest upstream changes (presumably fb39a30a052bbc3c4e7afe78c5c1655622094d9d) made this pull request unmergeable. Please resolve the merge conflicts.
Please rebase and ping, happy to merge. Hopefully it's just a matter of regenerating test expectations. Sorry, this didn't get into the merge queue because it got configured right after I pressed the "Merge when ready" button :/
Do you want me to squash the commits?
Do you want me to squash the commits?
Nah, all good, thanks!
Add
--flexarray-dst
option to allow use of a dynamically sized struct to model a C flexible array member. For example, given the C struct:generate the corresponding Rust type:
This single definition is used for both Sized (with a zero-length array as the FAM type) and DST (with a slice).
This also generates the helper methods to convert a raw pointer into a Rust view of the C type, with a given length.
Upside:
__IncompleteArrayField
typesize_of
will work, taking into account the dynamically sized extensionProblems/TODO:
ptr_metadata
andlayout_for_ptr
featuresfrom_ptr(_mut)
return references with unbounded lifetimes - it would be up to the caller to constrain them to the underlying storage's lifetime_lt: &'a LT
which can be used to bound the returned lifetime, but I'll need to try it in practice to see if it works.This is a prototype for #2771