rust-dominator/src/traits.rs

128 lines
2.3 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use std::rc::Rc;
use std::borrow::Cow;
2018-03-15 06:54:18 -04:00
pub use animation::AnimatedSignalVec;
pub trait Mixin<A> {
fn apply(&self, builder: A) -> A;
2018-02-25 06:58:20 -05:00
}
impl<A, F> Mixin<A> for F where F: Fn(A) -> A {
2018-02-25 06:58:20 -05:00
#[inline]
fn apply(&self, builder: A) -> A {
self(builder)
2018-02-25 06:58:20 -05:00
}
}
// TODO figure out a way to implement this for all of AsRef / Borrow / etc.
pub trait AsStr {
fn as_str(&self) -> &str;
2018-02-25 06:58:20 -05:00
}
impl AsStr for String {
2018-02-25 06:58:20 -05:00
#[inline]
fn as_str(&self) -> &str {
self.as_str()
2018-02-25 06:58:20 -05:00
}
}
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 {
2018-02-25 06:58:20 -05:00
#[inline]
fn as_str(&self) -> &str {
self
2018-02-25 06:58:20 -05:00
}
}
impl<'a> AsStr for &'a mut str {
#[inline]
fn as_str(&self) -> &str {
self
}
}
2018-02-25 06:58:20 -05:00
// TODO figure out a way to implement this for all of AsRef / Borrow / etc.
pub trait AsOptionStr {
fn as_option_str(&self) -> Option<&str>;
2018-02-25 06:58:20 -05:00
}
impl<A: AsStr> AsOptionStr for A {
#[inline]
fn as_option_str(&self) -> Option<&str> {
Some(self.as_str())
}
}
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: AsStr> AsOptionStr for Box<Option<A>> {
2018-02-25 06:58:20 -05:00
#[inline]
fn as_option_str(&self) -> Option<&str> {
(**self).as_option_str()
2018-02-25 06:58:20 -05:00
}
}
impl<A: AsStr> AsOptionStr for Arc<Option<A>> {
#[inline]
fn as_option_str(&self) -> Option<&str> {
(**self).as_option_str()
}
2018-02-25 06:58:20 -05:00
}
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()
}
}*/