2019-06-11 09:29:46 -04:00
|
|
|
use crate::dom::RefFn;
|
2018-06-13 19:48:41 -04:00
|
|
|
|
2019-06-11 09:29:46 -04:00
|
|
|
pub use crate::animation::AnimatedSignalVec;
|
|
|
|
|
|
|
|
|
|
|
|
pub trait StaticEvent {
|
|
|
|
const EVENT_TYPE: &'static str;
|
|
|
|
|
|
|
|
fn unchecked_from_event(event: web_sys::Event) -> Self;
|
|
|
|
}
|
2018-03-15 06:54:18 -04:00
|
|
|
|
|
|
|
|
2018-10-30 05:06:22 -04:00
|
|
|
#[deprecated(since = "0.3.2", note = "Use the apply or apply_if methods instead")]
|
2018-04-25 19:01:52 -04:00
|
|
|
pub trait Mixin<A> {
|
2018-07-16 23:26:48 -04:00
|
|
|
fn apply(self, builder: A) -> A;
|
2018-02-25 06:58:20 -05:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:06:22 -04:00
|
|
|
#[allow(deprecated)]
|
2018-07-16 23:26:48 -04:00
|
|
|
impl<A, F> Mixin<A> for F where F: FnOnce(A) -> A {
|
2018-02-25 06:58:20 -05:00
|
|
|
#[inline]
|
2018-07-16 23:26:48 -04:00
|
|
|
fn apply(self, builder: A) -> A {
|
2018-04-25 19:01:52 -04:00
|
|
|
self(builder)
|
2018-02-25 06:58:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
// 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;
|
2018-06-28 13:02:35 -04:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:58:05 -04:00
|
|
|
impl<'a, A> AsStr for &'a A where A: AsStr {
|
|
|
|
#[inline]
|
|
|
|
fn as_str(&self) -> &str {
|
|
|
|
AsStr::as_str(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
impl AsStr for String {
|
2018-06-28 13:02:35 -04:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
fn as_str(&self) -> &str {
|
|
|
|
self
|
2018-06-28 13:02:35 -04:00
|
|
|
}
|
2018-09-17 17:17:55 -04:00
|
|
|
}
|
2018-06-28 13:02:35 -04:00
|
|
|
|
2018-09-17 17:58:05 -04:00
|
|
|
impl AsStr for str {
|
|
|
|
#[inline]
|
|
|
|
fn as_str(&self) -> &str {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
impl<'a> AsStr for &'a str {
|
2018-06-28 13:02:35 -04:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
fn as_str(&self) -> &str {
|
|
|
|
self
|
2018-06-28 13:02:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
impl<A, C> AsStr for RefFn<A, str, C> where C: Fn(&A) -> &str {
|
|
|
|
#[inline]
|
|
|
|
fn as_str(&self) -> &str {
|
|
|
|
self.call_ref()
|
|
|
|
}
|
2018-06-28 13:02:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
pub trait MultiStr {
|
2019-08-04 21:39:40 -04:00
|
|
|
fn find_map<A, F>(&self, f: F) -> Option<A> where F: FnMut(&str) -> Option<A>;
|
2018-06-22 13:04:08 -04:00
|
|
|
|
2018-02-25 06:58:20 -05:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
fn each<F>(&self, mut f: F) where F: FnMut(&str) {
|
2019-08-04 21:39:40 -04:00
|
|
|
let _: Option<()> = self.find_map(|x| {
|
2018-09-17 17:17:55 -04:00
|
|
|
f(x);
|
2019-08-04 21:39:40 -04:00
|
|
|
None
|
2018-09-17 17:17:55 -04:00
|
|
|
});
|
2018-02-25 06:58:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
impl<A> MultiStr for A where A: AsStr {
|
2018-06-13 19:48:41 -04:00
|
|
|
#[inline]
|
2019-08-04 21:39:40 -04:00
|
|
|
fn find_map<B, F>(&self, mut f: F) -> Option<B> where F: FnMut(&str) -> Option<B> {
|
2018-09-17 17:17:55 -04:00
|
|
|
f(self.as_str())
|
2018-06-13 19:48:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
// TODO it would be great to use IntoIterator instead, and then we can replace the array implementations with it
|
2018-09-18 17:04:03 -04:00
|
|
|
/*impl<'a, A> MultiStr for &'a [A] where A: AsStr {
|
2018-06-13 19:48:41 -04:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
fn any<F>(&self, mut f: F) -> bool where F: FnMut(&str) -> bool {
|
|
|
|
self.iter().any(|x| f(x.as_str()))
|
2018-06-13 19:48:41 -04:00
|
|
|
}
|
2018-09-18 17:04:03 -04:00
|
|
|
}*/
|
2018-06-22 13:04:08 -04:00
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
// TODO it would be great to use IntoIterator or Iterator instead
|
2018-09-18 06:50:07 -04:00
|
|
|
impl<'a, A, C> MultiStr for RefFn<A, [&'a str], C> where C: Fn(&A) -> &[&'a str] {
|
2018-06-13 19:48:41 -04:00
|
|
|
#[inline]
|
2019-08-04 21:39:40 -04:00
|
|
|
fn find_map<B, F>(&self, mut f: F) -> Option<B> where F: FnMut(&str) -> Option<B> {
|
|
|
|
self.call_ref().iter().find_map(|x| f(x))
|
2018-06-13 19:48:41 -04:00
|
|
|
}
|
2018-09-18 06:50:07 -04:00
|
|
|
}
|
2018-09-17 17:17:55 -04:00
|
|
|
|
|
|
|
macro_rules! array_multi_str {
|
|
|
|
($size:expr) => {
|
|
|
|
impl<A> MultiStr for [A; $size] where A: AsStr {
|
|
|
|
#[inline]
|
2019-08-04 21:39:40 -04:00
|
|
|
fn find_map<B, F>(&self, mut f: F) -> Option<B> where F: FnMut(&str) -> Option<B> {
|
|
|
|
self.iter().find_map(|x| f(x.as_str()))
|
2018-09-17 17:17:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! array_multi_strs {
|
|
|
|
($($size:expr),*) => {
|
|
|
|
$(array_multi_str!($size);)*
|
|
|
|
};
|
2018-06-13 19:48:41 -04:00
|
|
|
}
|
|
|
|
|
2019-10-27 23:40:20 -04:00
|
|
|
array_multi_strs!(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);
|
2018-06-22 13:04:08 -04:00
|
|
|
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
pub trait OptionStr {
|
|
|
|
type Output;
|
|
|
|
|
|
|
|
fn into_option(self) -> Option<Self::Output>;
|
2018-02-25 06:58:20 -05:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
impl<A> OptionStr for A where A: MultiStr {
|
|
|
|
type Output = A;
|
2018-06-22 13:04:08 -04:00
|
|
|
|
2018-06-05 11:17:49 -04:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
fn into_option(self) -> Option<A> {
|
|
|
|
Some(self)
|
2018-06-05 11:17:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:17:55 -04:00
|
|
|
impl<A> OptionStr for Option<A> where A: MultiStr {
|
|
|
|
type Output = A;
|
2018-06-22 13:04:08 -04:00
|
|
|
|
2018-06-05 11:17:49 -04:00
|
|
|
#[inline]
|
2018-09-17 17:17:55 -04:00
|
|
|
fn into_option(self) -> Option<A> {
|
|
|
|
self
|
2018-06-05 11:17:49 -04:00
|
|
|
}
|
|
|
|
}
|