diff --git a/packages/yew/src/virtual_dom/mod.rs b/packages/yew/src/virtual_dom/mod.rs index e9b0e1b5493..e4ab84f044d 100644 --- a/packages/yew/src/virtual_dom/mod.rs +++ b/packages/yew/src/virtual_dom/mod.rs @@ -19,6 +19,7 @@ pub mod vtext; use crate::html::{AnyScope, NodeRef}; use indexmap::IndexMap; +use std::borrow::Cow; use std::{collections::HashMap, fmt, hint::unreachable_unchecked, iter}; use web_sys::{Element, Node}; @@ -83,6 +84,15 @@ impl From> for AttrValue { } } +impl From> for AttrValue { + fn from(s: Cow<'static, str>) -> Self { + match s { + Cow::Borrowed(s) => s.into(), + Cow::Owned(s) => s.into(), + } + } +} + impl Clone for AttrValue { fn clone(&self) -> Self { match self { @@ -152,6 +162,21 @@ mod tests_attr_value { assert_eq!(av.into_string(), "Rc"); } + #[test] + fn test_from_string() { + let av = AttrValue::from("str"); + assert_eq!(av.into_string(), "str"); + + let av = AttrValue::from("String".to_string()); + assert_eq!(av.into_string(), "String"); + + let av = AttrValue::from(Cow::from("BorrowedCow")); + assert_eq!(av.into_string(), "BorrowedCow"); + + let av = AttrValue::from(Cow::from("OwnedCow".to_string())); + assert_eq!(av.into_string(), "OwnedCow"); + } + #[test] fn test_equality() { // construct 3 AttrValue with same embedded value; expectation is that all are equal