From 40c6f9cb9cb38bf9b849c375a22c1003b9e82b16 Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 25 Nov 2021 21:24:58 -0700 Subject: [PATCH 1/3] implement From> for AttrValue --- packages/yew/src/virtual_dom/mod.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/yew/src/virtual_dom/mod.rs b/packages/yew/src/virtual_dom/mod.rs index e9b0e1b5493..f262f0df45c 100644 --- a/packages/yew/src/virtual_dom/mod.rs +++ b/packages/yew/src/virtual_dom/mod.rs @@ -20,6 +20,7 @@ pub mod vtext; use crate::html::{AnyScope, NodeRef}; use indexmap::IndexMap; use std::{collections::HashMap, fmt, hint::unreachable_unchecked, iter}; +use std::borrow::Cow; use web_sys::{Element, Node}; #[doc(inline)] @@ -83,6 +84,19 @@ 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 +166,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("Rc")); + assert_eq!(av.into_string(), "Rc"); + + let av = AttrValue::from(Cow::from("Rc".to_string())); + assert_eq!(av.into_string(), "Rc"); + } + #[test] fn test_equality() { // construct 3 AttrValue with same embedded value; expectation is that all are equal From b96ac85123f1957e7cae4e4e157bca7fe4b544c6 Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 25 Nov 2021 21:30:21 -0700 Subject: [PATCH 2/3] actually fmt code --- packages/yew/src/virtual_dom/mod.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/yew/src/virtual_dom/mod.rs b/packages/yew/src/virtual_dom/mod.rs index f262f0df45c..21ca0745bc3 100644 --- a/packages/yew/src/virtual_dom/mod.rs +++ b/packages/yew/src/virtual_dom/mod.rs @@ -19,8 +19,8 @@ pub mod vtext; use crate::html::{AnyScope, NodeRef}; use indexmap::IndexMap; -use std::{collections::HashMap, fmt, hint::unreachable_unchecked, iter}; use std::borrow::Cow; +use std::{collections::HashMap, fmt, hint::unreachable_unchecked, iter}; use web_sys::{Element, Node}; #[doc(inline)] @@ -87,12 +87,8 @@ 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() - } + Cow::Borrowed(s) => s.into(), + Cow::Owned(s) => s.into(), } } } From f02f1d79077ea3dce5daaf0ae2f3a9a6915a0cee Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 25 Nov 2021 22:00:16 -0700 Subject: [PATCH 3/3] oopsies --- packages/yew/src/virtual_dom/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/yew/src/virtual_dom/mod.rs b/packages/yew/src/virtual_dom/mod.rs index 21ca0745bc3..e4ab84f044d 100644 --- a/packages/yew/src/virtual_dom/mod.rs +++ b/packages/yew/src/virtual_dom/mod.rs @@ -170,11 +170,11 @@ mod tests_attr_value { let av = AttrValue::from("String".to_string()); assert_eq!(av.into_string(), "String"); - let av = AttrValue::from(Cow::from("Rc")); - assert_eq!(av.into_string(), "Rc"); + let av = AttrValue::from(Cow::from("BorrowedCow")); + assert_eq!(av.into_string(), "BorrowedCow"); - let av = AttrValue::from(Cow::from("Rc".to_string())); - assert_eq!(av.into_string(), "Rc"); + let av = AttrValue::from(Cow::from("OwnedCow".to_string())); + assert_eq!(av.into_string(), "OwnedCow"); } #[test]