-
Notifications
You must be signed in to change notification settings - Fork 58
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
defining zero-sized structs #37
Comments
I'd rather spell out exactly the structural recursion that is going rather than hand wave it away with "transitively", but yes I think we should guarantee:
Enums are interesting because we could try to be smart around enums with a single variant but that will likely depend on the |
What about a one-case enum whose body is zero-sized, e.g.:
or a union all of whose cases are zero-sized? |
The only-variant-containing-ZST case could also be defined as ZST depending on repr attributes, but doesn't seem very useful. Unions, good point, could probably be treated the same as structs, i.e., could be ZSTs even with repr(C). |
Generic enums with 2 or more variants are zero-sized if every variant is Void except for a zero-sized variant. |
Aren't empty enums considered uninhabited? I'm not sure if those types are technically ZSTs since there's no actual valid representation. |
@rodrimati1992 True, but I'd rather leave that sort of reasoning out of the picture for the initial step because:
@Mark-Simulacrum Empty enums are indeed uninhabited, but uninhabited types still have a size -- which is not necessarily zero, but definitely should be zero for empty enums. |
They are ZSTs in the sense that their size is 0 ( EDIT: Or maybe not, since we probably do consider |
This seems reasonable, except that it seems .. fine to say something stronger about enums (e.g., that an enum is zero-sized if it has only one "inhabited" variant and that variant is zero-sized). However, you are correct that we can "start small" (no pun intended) and grow, so perhaps we should just leave a "footnote" of some sort about that (in particular since I don't care to define inhabited just now). |
It seems to me that the full text of this section ought to include notes about uninhabitedness and its connection to size as well. |
I am wondering if we ought to pull "zero-sized types" into a section all its own, since it seems a bit .. cross-cutting? |
I think the guarantee we really want (for it to be helpful with e.g. #164) is more like "A struct consisting only of 1-ZST fields is itself a 1-ZST." |
From #31:
"If you have a struct which -- transitively -- contains no data of non-zero size, then the size of that struct will be zero as well. These zero-sized structs appear frequently as exceptions in other layout considerations (e.g., single-field structs). An example of such a struct is
std::marker::PhantomData
."Is that a sufficient definition for zero-sized structs? This seems like an important guarantee that we frequently rely upon for performance and other purposes, so it is worth specifying.
The text was updated successfully, but these errors were encountered: