-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: partial structs #7969
Comments
This seems quite similar to #1214 which was rejected. |
Something similar can be achieved with #7311, but this proposal has the following features:
In the vtable example, it may be possible to change the vtable and a large part of the layout without having to reallocate memory and/or adjust pointers to our object. My usecase was to implement promises/futures, where we either store the intermediate state of the computation or its result. It will also be possible to switch between (chain/tail call) different computations at runtime, provided we have pre-reserved the maximum storage size and alignment. The base of the future stays at the same address and is never invalidated, so users can always manage callbacks/cancellation.
In this case, More interesting alignment optimizations are also possible for non-extern structs. Here is an example of a permitted layout:
This is also very difficult to achieve using fields.
|
I'd like to see a set of use cases we can use to compare the various proposals to solve this class of issues. Like the io_uring example for one. One question I have about this is why introduce a partial struct at all? Why shouldn't you be able to "extend" normal structs as well? |
Sadly with this proposal it seems like it's impossible to put
I think the difference between normal structs and ones that should be extended is significant enough that they should be separate at the language level. One advantage is that we make object slicing difficult. Extending normal structs will not allow the layouts shown in #7969 (comment). People assume that they can memcpy structs around, and if there are fields in the padding, things will go wrong. |
Oh geeze, yeah "Object Slicing" looks like a really bad footgun. With that I see why you wouldn't want to extend a normal struct without some sort of marker saying you should never copy a value of the struct you're extending. It looks like because this feature is limited to only appending fields, the io_uring example can't be solved with this feature. I wonder if this proposal could be modified to be able to solve it? Otherwise, I'd be much less inclined to choose this solution over one that can solve all the use cases. |
No. A partial struct is not a struct, it's just a set of fields. Consider this example:
It is equivalent to
except that we also require that |
The idea of this proposal is to provide a convenient way to represent structs with identical prefixes. For example,
struct sockaddr
, or mimicking C++-style inheritance.A partial struct is effectively a sequence of fields laid out in an unspecified manner. Any struct or partial struct
B
can extend a partial structA
. In that case all fields ofA
are visible as fields ofB
and have the same offsets.In terms of layout, the first new field of
B
may begin after the last field ofA
- paddingA
to its alignment is not needed. IfA
andB
are extern, the layout ofB
is the same as if all fields were in a single extern struct. An extern struct or partial struct can only extend a partial struct.A
*Type
pointer to a struct or partial struct can be coerced to a base type pointer. Partial structs have a@sizeOf
0 and cannot be used as types of expressions, so dereferencing a partial struct pointer usingptr.*
is also not allowed.[*]Type
pointers to partial structs are not allowed.We need to decide what to do with empty partial structs. I think for now they should not be allowed.
Example:
We can also use it with unions:
The text was updated successfully, but these errors were encountered: