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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use wasm_bindgen::JsValue;
use crate::AnyNode;
pub trait Node {
fn as_react_node_js(&self) -> JsValue;
#[inline]
fn as_react_children_js(&self) -> Option<js_sys::Array> {
Some(js_sys::Array::of1(&self.as_react_node_js()))
}
#[inline]
fn into_any_node(self) -> AnyNode
where
Self: Sized + 'static,
{
AnyNode::wrap(self)
}
}
impl Node for () {
#[inline]
fn as_react_node_js(&self) -> JsValue {
JsValue::UNDEFINED
}
#[inline]
fn as_react_children_js(&self) -> Option<js_sys::Array> {
None
}
}
impl<T: Node> Node for Option<T> {
#[inline]
fn as_react_node_js(&self) -> JsValue {
if let Some(node) = self {
node.as_react_node_js()
} else {
JsValue::NULL
}
}
#[inline]
fn as_react_children_js(&self) -> Option<js_sys::Array> {
self.as_ref().and_then(Node::as_react_children_js)
}
}
macro_rules! into_js_node {
(deref: $($n:ty)*) => ($(
impl Node for $n {
#[inline]
fn as_react_node_js(&self) -> JsValue {
JsValue::from(*self)
}
}
)*);
($($n:ty)*) => ($(
impl Node for $n {
#[inline]
fn as_react_node_js(&self) -> JsValue {
JsValue::from(self)
}
}
)*);
}
into_js_node! {
react_sys::Element
String
js_sys::JsString
js_sys::Number
js_sys::BigInt
js_sys::Boolean
}
into_js_node! {
deref:
&str
i8 u8 i16 u16 i32 u32 f32 f64
i64 u64 i128 u128 isize usize
bool
}
macro_rules! impl_node_for_iter {
($($t:tt)+) => {
$($t)+ {
#[inline]
fn as_react_node_js(&self) -> JsValue {
js_sys::Array::from_iter(self.iter().map(|node| node.as_react_node_js())).into()
}
}
};
}
impl_node_for_iter! { impl<T: Node> Node for Vec<T> }
impl_node_for_iter! { impl<T: Node> Node for &[T] }
impl_node_for_iter! { impl<N: Node, const S: usize> Node for [N; S] }
macro_rules! impl_node_for_tuple {
(@impl ( $($t:ident),+ $(,)? )) => {
impl<$($t: Node),+> Node for ($($t),+ ,) {
#[inline]
fn as_react_node_js(&self) -> JsValue {
#![allow(non_snake_case)]
let ($($t),+ ,) = self;
js_sys::Array::from_iter([
$($t.as_react_node_js()),+
]).into()
}
}
};
( $(( $($t:ident),+ $(,)? ))* ) => {
$(
impl_node_for_tuple! { @impl ($($t),+ ,) }
)*
};
}
impl_node_for_tuple! {
(T0,)
(T0,T1)
(T0,T1,T2)
(T0,T1,T2,T3)
(T0,T1,T2,T3,T4)
(T0,T1,T2,T3,T4,T5)
(T0,T1,T2,T3,T4,T5,T6)
(T0,T1,T2,T3,T4,T5,T6,T7)
(T0,T1,T2,T3,T4,T5,T6,T7,T8)
(T0,T1,T2,T3,T4,T5,T6,T7,T8,T9)
}
impl<N: Node> Node for Box<N> {
#[inline]
fn as_react_node_js(&self) -> JsValue {
self.as_ref().as_react_node_js()
}
#[inline]
fn into_any_node(self) -> AnyNode
where
N: 'static,
{
AnyNode(self)
}
}