react/
key.rs

1use wasm_bindgen::JsValue;
2
3#[derive(Debug, Clone)]
4pub struct Key {
5    inner: JsValue,
6}
7
8impl Into<JsValue> for Key {
9    #[inline]
10    fn into(self) -> JsValue {
11        self.inner
12    }
13}
14
15impl AsRef<JsValue> for Key {
16    #[inline]
17    fn as_ref(&self) -> &JsValue {
18        &self.inner
19    }
20}
21
22pub trait AsKey {
23    fn as_key(&self) -> Key;
24}
25
26impl AsKey for String {
27    #[inline]
28    fn as_key(&self) -> Key {
29        Key {
30            inner: JsValue::from_str(self),
31        }
32    }
33}
34
35impl AsKey for str {
36    #[inline]
37    fn as_key(&self) -> Key {
38        Key {
39            inner: JsValue::from_str(self),
40        }
41    }
42}
43
44impl<T: AsKey> AsKey for &T {
45    #[inline]
46    fn as_key(&self) -> Key {
47        (*self).as_key()
48    }
49}
50
51macro_rules! impl_as_key {
52    ($($t:ty)*) => {
53        $(
54            impl AsKey for $t {
55                #[inline]
56                fn as_key(&self) -> Key {
57                    Key { inner: JsValue::from(*self) }
58                }
59            }
60        )*
61    };
62}
63
64impl_as_key! {
65    // numbers https://docs.rs/wasm-bindgen/0.2.78/src/wasm_bindgen/lib.rs.html#849
66    i8 u8 i16 u16 i32 u32 f32 f64
67    // big_numbers https://docs.rs/wasm-bindgen/0.2.78/src/wasm_bindgen/lib.rs.html#869
68    i64 u64 i128 u128 isize usize
69}