1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use dom::RefFn;

pub use animation::AnimatedSignalVec;


#[deprecated(since = "0.3.2", note = "Use the apply or apply_if methods instead")]
pub trait Mixin<A> {
    fn apply(self, builder: A) -> A;
}

#[allow(deprecated)]
impl<A, F> Mixin<A> for F where F: FnOnce(A) -> A {
    #[inline]
    fn apply(self, builder: A) -> A {
        self(builder)
    }
}


// TODO figure out a way to implement this for all of AsRef / Borrow / etc.
// TODO implementations for &String and &mut String
pub trait AsStr {
    fn as_str(&self) -> &str;
}

impl<'a, A> AsStr for &'a A where A: AsStr {
    #[inline]
    fn as_str(&self) -> &str {
        AsStr::as_str(*self)
    }
}

impl AsStr for String {
    #[inline]
    fn as_str(&self) -> &str {
        self
    }
}

impl AsStr for str {
    #[inline]
    fn as_str(&self) -> &str {
        self
    }
}

impl<'a> AsStr for &'a str {
    #[inline]
    fn as_str(&self) -> &str {
        self
    }
}

impl<A, C> AsStr for RefFn<A, str, C> where C: Fn(&A) -> &str {
    #[inline]
    fn as_str(&self) -> &str {
        self.call_ref()
    }
}


pub trait MultiStr {
    fn any<F>(&self, f: F) -> bool where F: FnMut(&str) -> bool;

    #[inline]
    fn each<F>(&self, mut f: F) where F: FnMut(&str) {
        self.any(|x| {
            f(x);
            false
        });
    }
}

impl<A> MultiStr for A where A: AsStr {
    #[inline]
    fn any<F>(&self, mut f: F) -> bool where F: FnMut(&str) -> bool {
        f(self.as_str())
    }
}

// TODO it would be great to use IntoIterator instead, and then we can replace the array implementations with it
/*impl<'a, A> MultiStr for &'a [A] where A: AsStr {
    #[inline]
    fn any<F>(&self, mut f: F) -> bool where F: FnMut(&str) -> bool {
        self.iter().any(|x| f(x.as_str()))
    }
}*/

// TODO it would be great to use IntoIterator or Iterator instead
impl<'a, A, C> MultiStr for RefFn<A, [&'a str], C> where C: Fn(&A) -> &[&'a str] {
    #[inline]
    fn any<F>(&self, mut f: F) -> bool where F: FnMut(&str) -> bool {
        self.call_ref().iter().any(|x| f(x))
    }
}

macro_rules! array_multi_str {
    ($size:expr) => {
        impl<A> MultiStr for [A; $size] where A: AsStr {
            #[inline]
            fn any<F>(&self, mut f: F) -> bool where F: FnMut(&str) -> bool {
                self.iter().any(|x| f(x.as_str()))
            }
        }
    };
}

macro_rules! array_multi_strs {
    ($($size:expr),*) => {
        $(array_multi_str!($size);)*
    };
}

array_multi_strs!(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32);


pub trait OptionStr {
    type Output;

    fn into_option(self) -> Option<Self::Output>;
}

impl<A> OptionStr for A where A: MultiStr {
    type Output = A;

    #[inline]
    fn into_option(self) -> Option<A> {
        Some(self)
    }
}

impl<A> OptionStr for Option<A> where A: MultiStr {
    type Output = A;

    #[inline]
    fn into_option(self) -> Option<A> {
        self
    }
}