Adding in more implementations for AsStr and AsOptionStr

This commit is contained in:
Pauan 2018-06-13 13:48:41 -10:00
parent 4d3f936824
commit 746d5308e6
2 changed files with 148 additions and 30 deletions

View File

@ -563,24 +563,6 @@ impl<A: IHtmlElement + Clone + 'static> DomBuilder<A> {
}
#[cfg(test)]
mod tests {
use super::{create_element_ns, DomBuilder, HTML_NAMESPACE};
use stdweb::web::{HtmlElement, IHtmlElement};
#[test]
fn mixin() {
let a: DomBuilder<HtmlElement> = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE));
fn my_mixin<A: IHtmlElement>(builder: DomBuilder<A>) -> DomBuilder<A> {
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<HtmlElement> = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE));
fn my_mixin<A: IHtmlElement>(builder: DomBuilder<A>) -> DomBuilder<A> {
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::<String>("foo".to_owned())));
}
#[test]
fn style_signal_types() {
let _a: DomBuilder<HtmlElement> = 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::<String>("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::<String>("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::<Option<String>>(Some("bar".to_owned()))))*/
;
}
}

View File

@ -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<A, F> Mixin<A> 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<A: AsStr> AsStr for Box<A> {
#[inline]
fn as_str(&self) -> &str {
(**self).as_str()
}
}
impl<A: AsStr> AsStr for Arc<A> {
#[inline]
fn as_str(&self) -> &str {
(**self).as_str()
}
}
impl<A: AsStr> AsStr for Rc<A> {
#[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<A: AsStr> 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<String> {
impl<A: AsStr> AsOptionStr for Option<A> {
#[inline]
fn as_option_str(&self) -> Option<&str> {
self.as_ref().map(|x| x.as_str())
}
}
impl<'a> AsOptionStr for Option<&'a str> {
/*impl<A: AsStr> AsOptionStr for Box<Option<A>> {
#[inline]
fn as_option_str(&self) -> Option<&str> {
*self
(**self).as_option_str()
}
}
impl<A: AsStr> AsOptionStr for Arc<Option<A>> {
#[inline]
fn as_option_str(&self) -> Option<&str> {
(**self).as_option_str()
}
}
impl<A: AsStr> AsOptionStr for Rc<Option<A>> {
#[inline]
fn as_option_str(&self) -> Option<&str> {
(**self).as_option_str()
}
}
impl<'a, A: AsStr + Clone> AsOptionStr for Cow<'a, Option<A>> {
#[inline]
fn as_option_str(&self) -> Option<&str> {
(**self).as_option_str()
}
}*/