From 746d5308e6277d738ffb52de9a9b9248f5ac7ef5 Mon Sep 17 00:00:00 2001 From: Pauan Date: Wed, 13 Jun 2018 13:48:41 -1000 Subject: [PATCH] Adding in more implementations for AsStr and AsOptionStr --- src/dom.rs | 92 +++++++++++++++++++++++++++++++++++++++++---------- src/traits.rs | 86 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 148 insertions(+), 30 deletions(-) diff --git a/src/dom.rs b/src/dom.rs index e9fb72a..d3eda22 100644 --- a/src/dom.rs +++ b/src/dom.rs @@ -563,24 +563,6 @@ impl DomBuilder { } -#[cfg(test)] -mod tests { - use super::{create_element_ns, DomBuilder, HTML_NAMESPACE}; - use stdweb::web::{HtmlElement, IHtmlElement}; - - #[test] - fn mixin() { - let a: DomBuilder = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE)); - - fn my_mixin(builder: DomBuilder) -> DomBuilder { - builder.style("foo", "bar") - } - - a.mixin(my_mixin); - } -} - - // TODO better warning message for must_use #[must_use] pub struct StylesheetBuilder { @@ -743,3 +725,77 @@ impl ClassBuilder { self.class_name } } + + + +#[cfg(test)] +mod tests { + use super::{create_element_ns, DomBuilder, HTML_NAMESPACE, text_signal}; + use std::rc::Rc; + use std::borrow::Cow; + use std::sync::Arc; + use futures_signals::signal::always; + use stdweb::web::{HtmlElement, IHtmlElement}; + + #[test] + fn mixin() { + let a: DomBuilder = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE)); + + fn my_mixin(builder: DomBuilder) -> DomBuilder { + builder.style("foo", "bar") + } + + a.mixin(my_mixin); + } + + #[test] + fn text_signal_types() { + text_signal(always("foo")); + text_signal(always("foo".to_owned())); + text_signal(always(Arc::new("foo"))); + text_signal(always(Arc::new("foo".to_owned()))); + text_signal(always(Rc::new("foo"))); + text_signal(always(Rc::new("foo".to_owned()))); + text_signal(always(Box::new("foo"))); + text_signal(always(Box::new("foo".to_owned()))); + text_signal(always(Cow::Borrowed(&"foo"))); + text_signal(always(Cow::Owned::("foo".to_owned()))); + } + + #[test] + fn style_signal_types() { + let _a: DomBuilder = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE)) + .style_signal("foo", always("bar")) + .style_signal("foo", always("bar".to_owned())) + .style_signal("foo", always(Arc::new("bar"))) + .style_signal("foo", always(Arc::new("bar".to_owned()))) + .style_signal("foo", always(Rc::new("bar"))) + .style_signal("foo", always(Rc::new("bar".to_owned()))) + .style_signal("foo", always(Box::new("bar"))) + .style_signal("foo", always(Box::new("bar".to_owned()))) + .style_signal("foo", always(Cow::Borrowed(&"bar"))) + .style_signal("foo", always(Cow::Owned::("bar".to_owned()))) + + .style_signal("foo", always(Some("bar"))) + .style_signal("foo", always(Some("bar".to_owned()))) + .style_signal("foo", always(Some(Arc::new("bar")))) + .style_signal("foo", always(Some(Arc::new("bar".to_owned())))) + .style_signal("foo", always(Some(Rc::new("bar")))) + .style_signal("foo", always(Some(Rc::new("bar".to_owned())))) + .style_signal("foo", always(Some(Box::new("bar")))) + .style_signal("foo", always(Some(Box::new("bar".to_owned())))) + .style_signal("foo", always(Some(Cow::Borrowed(&"bar")))) + .style_signal("foo", always(Some(Cow::Owned::("bar".to_owned())))) + + /*.style_signal("foo", always(Arc::new(Some("bar")))) + .style_signal("foo", always(Arc::new(Some("bar".to_owned())))) + .style_signal("foo", always(Rc::new(Some("bar")))) + .style_signal("foo", always(Rc::new(Some("bar".to_owned())))) + .style_signal("foo", always(Box::new(Some("bar")))) + .style_signal("foo", always(Box::new(Some("bar".to_owned())))) + .style_signal("foo", always(Cow::Borrowed(&Some("bar")))) + .style_signal("foo", always(Cow::Owned::>(Some("bar".to_owned()))))*/ + + ; + } +} diff --git a/src/traits.rs b/src/traits.rs index 2bce169..619d02c 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1,3 +1,7 @@ +use std::sync::Arc; +use std::rc::Rc; +use std::borrow::Cow; + pub use animation::AnimatedSignalVec; @@ -13,6 +17,7 @@ impl Mixin for F where F: Fn(A) -> A { } +// TODO figure out a way to implement this for all of AsRef / Borrow / etc. pub trait AsStr { fn as_str(&self) -> &str; } @@ -24,6 +29,41 @@ impl AsStr for String { } } +impl AsStr for Box { + #[inline] + fn as_str(&self) -> &str { + (**self).as_str() + } +} + +impl AsStr for Arc { + #[inline] + fn as_str(&self) -> &str { + (**self).as_str() + } +} + +impl AsStr for Rc { + #[inline] + fn as_str(&self) -> &str { + (**self).as_str() + } +} + +impl<'a, A: AsStr + Clone> AsStr for Cow<'a, A> { + #[inline] + fn as_str(&self) -> &str { + (**self).as_str() + } +} + +impl AsStr for str { + #[inline] + fn as_str(&self) -> &str { + self + } +} + impl<'a> AsStr for &'a str { #[inline] fn as_str(&self) -> &str { @@ -31,35 +71,57 @@ impl<'a> AsStr for &'a str { } } +impl<'a> AsStr for &'a mut str { + #[inline] + fn as_str(&self) -> &str { + self + } +} + +// TODO figure out a way to implement this for all of AsRef / Borrow / etc. pub trait AsOptionStr { fn as_option_str(&self) -> Option<&str>; } -impl AsOptionStr for String { +impl AsOptionStr for A { #[inline] fn as_option_str(&self) -> Option<&str> { - Some(self) + Some(self.as_str()) } } -impl<'a> AsOptionStr for &'a str { - #[inline] - fn as_option_str(&self) -> Option<&str> { - Some(self) - } -} - -impl AsOptionStr for Option { +impl AsOptionStr for Option { #[inline] fn as_option_str(&self) -> Option<&str> { self.as_ref().map(|x| x.as_str()) } } -impl<'a> AsOptionStr for Option<&'a str> { +/*impl AsOptionStr for Box> { #[inline] fn as_option_str(&self) -> Option<&str> { - *self + (**self).as_option_str() } } + +impl AsOptionStr for Arc> { + #[inline] + fn as_option_str(&self) -> Option<&str> { + (**self).as_option_str() + } +} + +impl AsOptionStr for Rc> { + #[inline] + fn as_option_str(&self) -> Option<&str> { + (**self).as_option_str() + } +} + +impl<'a, A: AsStr + Clone> AsOptionStr for Cow<'a, Option> { + #[inline] + fn as_option_str(&self) -> Option<&str> { + (**self).as_option_str() + } +}*/