Skip to content

Commit 7ed0abf

Browse files
committed
Copy optimization made for String to AttrValue
1 parent e3e09f3 commit 7ed0abf

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/yew/src/html/classes.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ impl From<&String> for Classes {
212212
}
213213
}
214214

215-
impl From<AttrValue> for Classes {
216-
fn from(t: AttrValue) -> Self {
215+
impl From<&AttrValue> for Classes {
216+
fn from(t: &AttrValue) -> Self {
217217
let set = t
218218
.split_whitespace()
219219
.map(ToOwned::to_owned)
@@ -223,6 +223,23 @@ impl From<AttrValue> for Classes {
223223
}
224224
}
225225

226+
impl From<AttrValue> for Classes {
227+
fn from(t: AttrValue) -> Self {
228+
match t.contains(|c: char| c.is_whitespace()) {
229+
// If the string only contains a single class, we can just use it
230+
// directly (rather than cloning it into a new string). Need to make
231+
// sure it's not empty, though.
232+
false => match t.is_empty() {
233+
true => Self::new(),
234+
false => Self {
235+
set: Rc::new(IndexSet::from_iter([t])),
236+
},
237+
},
238+
true => Self::from(&t),
239+
}
240+
}
241+
}
242+
226243
impl<T: Into<Classes>> From<Option<T>> for Classes {
227244
fn from(t: Option<T>) -> Self {
228245
t.map(|x| x.into()).unwrap_or_default()

0 commit comments

Comments
 (0)