-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Improve empty_enum lint #4905
Comments
This help message should be improved along with the documentation. In the mean time you can read up on empty enums here: https://doc.rust-lang.org/nomicon/exotic-sizes.html?highlight=enum#empty-types |
TL;DR: You should use If you just want to mark something, you should use ZSTs instead of empty types. ( |
@flip1995 Thanks for the link, I read through it and your I have a file parser for a format with several versions, and a newer version has a field, that doesn't exist on older ones, so I wrote my API like this use core::marker::PhantomData;
enum V2 {}
struct FileData<T> {
data: Option<String>,
_p: PhantomData<T>,
}
impl FileData<V2> {
pub fn data(&self) -> &String {
&self.data.as_ref().unwrap()
}
} This makes it possible for the developer to specify at compile time, which format they use and therefore removes the need for pointless With the above in mind, I ask myself why I should use struct V2; over enum V2 {} The benefit of an empty Another thing, that confuses me is that there are multiple ways to achieve the same thing: struct Marked(!); // can this be constructed?
struct Marked;
enum Marked {};
type Marked = !; // is there still a difference between this and `type NotMarked = !;`? like there would be with `enum Marked {}` and `enum NotMarked {}`
// what about
type Marked = (); |
I understood your first example like you wanted to just mark something and didn't care if it could be constructed. If you really need something that should not be constructed, you have no other way than using an empty type/enum on stable, since struct Marked(!); // can never be contructed, since ! is uninhabited (nightly only)
enum Marked {}; // can never be constructed, since it is an empty type
type Marked = !; // That is the same as using `!`, but with a name (only available on nightly). I'd suggest to use this method if you are on nightly and don't want something to be constructed
type Marked = (); // This is just a ZST, not what you want |
This code
causes the following error:
So it's telling me to replace my empty enums with
!
(which doesn't make sense in this case) or make a wrapper around!
.The problem is, that there is nowhere mentioned how this wrapper type should look like and what the benefits are over an empty enum?!
Playground
The text was updated successfully, but these errors were encountered: