Skip to content

Commit 27950ea

Browse files
committed
Refactor media type internals to use Cow
1 parent f18821e commit 27950ea

File tree

3 files changed

+24
-58
lines changed

3 files changed

+24
-58
lines changed

src/mime/constants.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::ParamKind;
22
use crate::Mime;
3+
use std::borrow::Cow;
34

45
macro_rules! utf8_mime_const {
56
($name:ident, $desc:expr, $base:expr, $sub:expr) => {
@@ -32,13 +33,10 @@ macro_rules! mime_const {
3233
(doc_expanded, $name:ident, $desc:expr, $base:expr, $sub:expr, $params:expr, $doccomment:expr) => {
3334
#[doc = $doccomment]
3435
pub const $name: Mime = Mime {
35-
essence: String::new(),
36-
basetype: String::new(),
37-
subtype: String::new(),
36+
essence: Cow::Borrowed(concat!($base, "/", $sub)),
37+
basetype: Cow::Borrowed($base),
38+
subtype: Cow::Borrowed($sub),
3839
params: $params,
39-
static_essence: Some(concat!($base, "/", $sub)),
40-
static_basetype: Some($base),
41-
static_subtype: Some($sub),
4240
};
4341
};
4442
}

src/mime/mod.rs

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ use infer::Infer;
2828
/// ```
2929
// NOTE: we cannot statically initialize Strings with values yet, so we keep dedicated static
3030
// fields for the static strings.
31-
#[derive(Clone)]
31+
#[derive(Clone, Eq)]
3232
pub struct Mime {
33-
pub(crate) essence: String,
34-
pub(crate) basetype: String,
35-
pub(crate) subtype: String,
36-
pub(crate) static_essence: Option<&'static str>,
37-
pub(crate) static_basetype: Option<&'static str>,
38-
pub(crate) static_subtype: Option<&'static str>,
33+
pub(crate) essence: Cow<'static, str>,
34+
pub(crate) basetype: Cow<'static, str>,
35+
pub(crate) subtype: Cow<'static, str>,
3936
pub(crate) params: Option<ParamKind>,
4037
}
4138

@@ -68,29 +65,17 @@ impl Mime {
6865
/// According to the spec this method should be named `type`, but that's a reserved keyword in
6966
/// Rust so hence prefix with `base` instead.
7067
pub fn basetype(&self) -> &str {
71-
if let Some(basetype) = self.static_basetype {
72-
&basetype
73-
} else {
74-
&self.basetype
75-
}
68+
&self.basetype
7669
}
7770

7871
/// Access the Mime's `subtype` value.
7972
pub fn subtype(&self) -> &str {
80-
if let Some(subtype) = self.static_subtype {
81-
&subtype
82-
} else {
83-
&self.subtype
84-
}
73+
&self.subtype
8574
}
8675

8776
/// Access the Mime's `essence` value.
8877
pub fn essence(&self) -> &str {
89-
if let Some(essence) = self.static_essence {
90-
&essence
91-
} else {
92-
&self.essence
93-
}
78+
&self.essence
9479
}
9580

9681
/// Get a reference to a param.
@@ -138,20 +123,6 @@ impl Mime {
138123
}
139124
}
140125

141-
impl PartialEq<Mime> for Mime {
142-
fn eq(&self, other: &Mime) -> bool {
143-
let left = match self.static_essence {
144-
Some(essence) => essence,
145-
None => &self.essence,
146-
};
147-
let right = match other.static_essence {
148-
Some(essence) => essence,
149-
None => &other.essence,
150-
};
151-
left == right
152-
}
153-
}
154-
155126
impl Display for Mime {
156127
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157128
parse::format(self, f)
@@ -160,11 +131,7 @@ impl Display for Mime {
160131

161132
impl Debug for Mime {
162133
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163-
if let Some(essence) = self.static_essence {
164-
Debug::fmt(essence, f)
165-
} else {
166-
Debug::fmt(&self.essence, f)
167-
}
134+
Debug::fmt(&self.essence, f)
168135
}
169136
}
170137

@@ -196,6 +163,13 @@ impl ToHeaderValues for Mime {
196163
Ok(header.to_header_values().unwrap())
197164
}
198165
}
166+
167+
impl PartialEq<Mime> for Mime {
168+
fn eq(&self, other: &Mime) -> bool {
169+
self.essence == other.essence
170+
}
171+
}
172+
199173
/// A parameter name.
200174
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
201175
pub struct ParamName(Cow<'static, str>);

src/mime/parse.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::fmt;
23

34
use super::{Mime, ParamKind, ParamName, ParamValue};
@@ -107,13 +108,10 @@ pub(crate) fn parse(input: &str) -> crate::Result<Mime> {
107108
}
108109

109110
Ok(Mime {
110-
essence: format!("{}/{}", &basetype, &subtype),
111-
basetype,
112-
subtype,
111+
essence: Cow::Owned(format!("{}/{}", &basetype, &subtype)),
112+
basetype: basetype.into(),
113+
subtype: subtype.into(),
113114
params: params.map(ParamKind::Vec),
114-
static_essence: None,
115-
static_basetype: None,
116-
static_subtype: None,
117115
})
118116
}
119117

@@ -205,11 +203,7 @@ fn collect_http_quoted_string(mut input: &str) -> (String, &str) {
205203

206204
/// Implementation of [WHATWG MIME serialization algorithm](https://mimesniff.spec.whatwg.org/#serializing-a-mime-type)
207205
pub(crate) fn format(mime_type: &Mime, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208-
if let Some(essence) = mime_type.static_essence {
209-
write!(f, "{}", essence)?
210-
} else {
211-
write!(f, "{}", &mime_type.essence)?
212-
}
206+
write!(f, "{}", &mime_type.essence)?;
213207
if let Some(params) = &mime_type.params {
214208
match params {
215209
ParamKind::Utf8 => write!(f, ";charset=utf-8")?,

0 commit comments

Comments
 (0)