rust-dominator/src/traits.rs

88 lines
1.6 KiB
Rust
Raw Normal View History

use dom::DerefFn;
use std::ops::Deref;
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.
2018-06-25 21:02:48 -04:00
// TODO implementations for &String and &mut String
pub trait IntoStr {
type Output: Deref<Target = str>;
fn into_str(self) -> Self::Output;
2018-02-25 06:58:20 -05:00
}
impl IntoStr for String {
type Output = Self;
2018-02-25 06:58:20 -05:00
#[inline]
fn into_str(self) -> Self::Output {
self
2018-02-25 06:58:20 -05:00
}
}
impl<'a> IntoStr for &'a str {
type Output = Self;
#[inline]
fn into_str(self) -> Self::Output {
self
}
}
impl<'a> IntoStr for &'a mut str {
type Output = Self;
#[inline]
fn into_str(self) -> Self::Output {
self
}
}
impl<A, B> IntoStr for DerefFn<A, B> where B: Fn(&A) -> &str {
type Output = Self;
#[inline]
fn into_str(self) -> Self::Output {
self
}
}
// TODO figure out a way to implement this for all of AsRef / Borrow / etc.
pub trait IntoOptionStr {
type Output: Deref<Target = str>;
fn into_option_str(self) -> Option<Self::Output>;
2018-02-25 06:58:20 -05:00
}
impl<A: IntoStr> IntoOptionStr for A {
type Output = A::Output;
#[inline]
fn into_option_str(self) -> Option<Self::Output> {
Some(self.into_str())
}
}
impl<A: IntoStr> IntoOptionStr for Option<A> {
type Output = A::Output;
#[inline]
fn into_option_str(self) -> Option<Self::Output> {
self.map(|x| x.into_str())
}
}