Enums with "all" value


#1

In HTTP headers, in Language Tags and in many other places you give a value or allow all values with a *.

Like the MIME-Type application/* will match both application/json and application/xml, because of the wildcard star used.

So what is the preferred way to express such values?

The two common ways I have seen are the following:

Either add a wildcard variant to the enum

enum Mimetype {
    Star,
    ApplicationJson,
    ApplicationXml,
    ...
}

Or use an Option

enum Mimetype {
    ApplicationJson,
    ApplicationXml,
    ...
}

and than use it as Option<Mimetype> in other structs. This consumes one extra byte for the option, but this could probably be optimised by compiler magic. :smiley:

Which option fits better into Rust, and is therefore preferred, or can these types get expressed in even more ways?


#2

Using Option here is bad because you would use None for expressing everything.

In this simple scenario I prefer the first version but with the name Any or Wildcard instead of Star (describe the meaning not the symbol). For more complex structures like with additional single character wildcard (often ?), I prefer something like

enum Mimetype {
    Literal(LiteralType),
    AnyChar,
    AnySequence,
    RegEx(String)
}

#3

For me the “*” means “lack of a value” , but probably I am wrong, so None looked like a good choice.


#4

I would say it’s rather “Any” value rather than None


#5

Structure layout is not optimized by the compiler unfortunately.