| ▲ | xeubie a day ago | |
No, you can't, because you're misunderstanding my point. You can make an enum in rust that doesn't contain data, but you can't make an enum that is explicitly backed by a tag type. `#[repr(u8)]` lets the tag be a u8 with values like Human = 1, Cat = 4, but you can’t say "Person's tag is my PersonKind enum." Zig lets you do exactly that with `union(PersonKind)`. In Rust, PersonKind would always be a separate type you keep in sync by hand. | ||
| ▲ | clownstrikelol a day ago | parent | next [-] | |
Ok from reading through your other comments on this thread and the links out to Ada and Zig docs, I think what is roughly equivalent would be aliases of unit. i.e. type Whatever = () That gets you a “tag” that is compile time enforced. You can explicitly match against these aliases and one alias will never == another alias (unless you explicitly write out a from / into impl, and a deref impl to make it happen implicitly). What you seem to be ultimately asking for is being able to use a tag that is freely floating around to determine the variant of a union. This would need to be explicitly handled inside an unsafe block though. The “happy path” here explicitly ties them together. Letting them not be handcuffed together is not memory safe, and the compiler has no way to guarantee you’re reading a value correctly. You can of course bypass all of that inside an unsafe block and pinky promise to the compiler you made no mistakes. | ||
| ▲ | dralley a day ago | parent | prev [-] | |
I doubt that would be too difficult to write a macro for. | ||