dominator2 0.5.26

Zero-cost ultra-high-performance declarative DOM library using FRP signals
Documentation
use crate::dom::RefFn;

pub use crate::animation::AnimatedSignalVec;

pub trait StaticEvent {
    const EVENT_TYPE: &'static str;

    fn unchecked_from_event(event: web_sys::Event) -> Self;
}

#[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 {
    #[deprecated(since = "0.5.18", note = "Use with_str instead")]
    fn as_str(&self) -> &str;

    fn with_str<A, F>(&self, f: F) -> A
    where
        F: FnOnce(&str) -> A,
    {
        #[allow(deprecated)]
        f(self.as_str())
    }
}

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

    #[inline]
    fn with_str<B, F>(&self, f: F) -> B
    where
        F: FnOnce(&str) -> B,
    {
        AsStr::with_str(*self, f)
    }
}

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

    #[inline]
    fn with_str<A, F>(&self, f: F) -> A
    where
        F: FnOnce(&str) -> A,
    {
        f(&self)
    }
}

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

    #[inline]
    fn with_str<A, F>(&self, f: F) -> A
    where
        F: FnOnce(&str) -> A,
    {
        f(self)
    }
}

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

    #[inline]
    fn with_str<A, F>(&self, f: F) -> A
    where
        F: FnOnce(&str) -> A,
    {
        f(self)
    }
}

#[cfg(feature = "smartstring")]
impl AsStr for smartstring::alias::String {
    fn as_str(&self) -> &str {
        &*self
    }

    fn with_str<A, F>(&self, f: F) -> A
    where
        F: FnOnce(&str) -> A,
    {
        f(&*self)
    }
}

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

    #[inline]
    fn with_str<B, F>(&self, f: F) -> B
    where
        F: FnOnce(&str) -> B,
    {
        f(self.call_ref())
    }
}

impl<S: AsStr + ?Sized> AsStr for std::sync::Arc<S> {
    fn as_str(&self) -> &str {
        #[allow(deprecated)]
        (&**self).as_str()
    }

    fn with_str<A, F>(&self, f: F) -> A
    where
        F: FnOnce(&str) -> A,
    {
        (&**self).with_str(f)
    }
}

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

    #[inline]
    fn each<F>(&self, mut f: F)
    where
        F: FnMut(&str),
    {
        let _: Option<()> = self.find_map(|x| {
            f(x);
            None
        });
    }
}

impl<A> MultiStr for A
where
    A: AsStr,
{
    #[inline]
    fn find_map<B, F>(&self, f: F) -> Option<B>
    where
        F: FnMut(&str) -> Option<B>,
    {
        self.with_str(f)
    }
}

// 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 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))
    }
}

impl<A, const N: usize> MultiStr for [A; N]
where
    A: AsStr,
{
    #[inline]
    fn find_map<B, F>(&self, mut f: F) -> Option<B>
    where
        F: FnMut(&str) -> Option<B>,
    {
        self.iter().find_map(|x| x.with_str(|x| f(x)))
    }
}

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
    }
}