Changed from several open-ended enum
s to an opaque Mime
struct.
-
It should be fast. The improvement here is that the full text is stored in a single buffer. In the previous versions, being a bunch of internal enums, it was quite slow to use
fmt::Display
. Writing a single buffer reduces the overhead of thestd::fmt
machinery.Comparisons with constants is near instant. Comparing with parsed
Mime
s is slightly slower, but there is also room to add optimizations which I've skipped in order to get this our the door. -
It should be easy to create a
Mime
. In this design, you can use one of the common constants, likemime::TEXT_PLAIN
, or parse a custom string into aMime
, like"application/vnd.github.api.v3+json".parse()
. No more importing 5 different types or passing emptyVec
s.With macros 2.0, it will be possible to make a macro that can parse the text at compile time, allowing custom constant
Mime
s. -
It should be extensible. As a bunch of enums, adding new variants was technically a breaking change (though who would match on every
SubLevel
variant?). By using constants, new ones can be added without breaking anyone. -
Knowledge of suffixes. You can check the suffix, such as in
image/svg+xml
. -
Comparing is still easy. You can still compare the various components of a
Mime
in a match. This works perfectly fine:match (mime.type_(), mime.subtype()) { (mime::TEXT, mime::PLAIN) => println!("plain text"), (mime::TEXT, _) => println!("structured text"), _ => println!("not text"), }
Additionally,
Mime
and its components can be easily compared against&str
.assert_eq!(mime::TEXT_PLAIN, "text/plain");