this post was submitted on 29 Jul 2023
12 points (92.9% liked)
Rust
6340 readers
7 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
This is not possible, because Rust still stores a discriminant even when the enum values don't overlap.
As far as I can tell, the only situation where Rust doesn't store a discriminant is when either the
Ok
orErr
variant is zero-sized, and the other variant has a niche. So,Result<(), ErrorEnum>
can be represented as an integer, butResult
can not.You can still use enums, and implement simple conversions like this:
P.S. Sorry that the generics aren't displayed due to Lemmy's bad santiziation.
Discriminant is irrelevant and you're not supposed to fuck with it.
And there is zero reason to use unsafe/transmute for this.
And just to explicitly point out, your code's also better because of the use of the standard traits. It took me a while to get into the habit, but using what's already there is always a good idea.