73c4aa5d
—
vpzomtrrfrt 5 years ago
impl Render for Cow<'_, str> (#36)
- render/src/text_element.rs +6 -0
- render_tests/src/lib.rs +21 -0
render/src/text_element.rs
CHANGED
|
@@ -14,6 +14,12 @@ impl Render for &str {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
impl Render for std::borrow::Cow<'_, str> {
|
|
18
|
+
fn render_into<W: Write>(self, writer: &mut W) -> Result {
|
|
19
|
+
escape_html(&self, writer)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
17
23
|
/// A raw (unencoded) html string
|
|
18
24
|
#[derive(Debug)]
|
|
19
25
|
pub struct Raw<'s>(&'s str);
|
render_tests/src/lib.rs
CHANGED
|
@@ -118,6 +118,27 @@ fn owned_string() {
|
|
|
118
118
|
);
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
#[test]
|
|
122
|
+
fn cow_str() {
|
|
123
|
+
use pretty_assertions::assert_eq;
|
|
124
|
+
use render::html;
|
|
125
|
+
use std::borrow::Cow;
|
|
126
|
+
|
|
127
|
+
let owned1 = "Borrowed from owned".to_owned();
|
|
128
|
+
let owned2 = "Owned".to_owned();
|
|
129
|
+
|
|
130
|
+
assert_eq!(
|
|
131
|
+
html! {
|
|
132
|
+
<div>
|
|
133
|
+
<p>{Cow::Borrowed("Static")}</p>
|
|
134
|
+
<p>{Cow::<'_, str>::Borrowed(&owned1)}</p>
|
|
135
|
+
<p>{Cow::<'_, str>::Owned(owned2)}</p>
|
|
136
|
+
</div>
|
|
137
|
+
},
|
|
138
|
+
r#"<div><p>Static</p><p>Borrowed from owned</p><p>Owned</p></div>"#,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
121
142
|
#[test]
|
|
122
143
|
fn number() {
|
|
123
144
|
use pretty_assertions::assert_eq;
|