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
//! Tools for working with Javascript properties.

/// A type that can be used as a property.
///
/// This allows a type to be used as a property in [`custom_html_element`]. It
/// exists so [`String`]s can be passed as `&str` and copyable values, like
/// [`bool`] can be passed by value.
///
/// [`custom_html_element`]: crate::custom_html_element
pub trait AsProperty {
    type Type<'a>
    where
        Self: 'a;

    fn as_property(&self) -> Self::Type<'_>;
}

impl<'a> AsProperty for &'a str {
    type Type<'b> = &'b str where 'a: 'b;

    fn as_property(&self) -> Self::Type<'_> {
        self
    }
}

impl AsProperty for String {
    type Type<'a> = &'a str;

    fn as_property(&self) -> Self::Type<'_> {
        self.as_ref()
    }
}

macro_rules! as_property{
    ($($typ:ty),*) => {
        $(
            impl AsProperty for $typ {
                type Type<'a> = Self;

                fn as_property(&self) -> Self::Type<'_> {
                    *self
                }
            }
        )*
    }
}

as_property!(bool);
as_property!(i8, i16, i32, i64);
as_property!(u8, u16, u32, u64);
as_property!(f32, f64);
as_property!(usize);