1use std::marker::PhantomData;
4
5use implicit_clone::unsync::IArray;
6use implicit_clone::ImplicitClone;
7use yew::html::ChildrenRenderer;
8
9pub fn into_node_iter<IT, T, R>(it: IT) -> impl Iterator<Item = R>
11where
12 IT: IntoIterator<Item = T>,
13 T: Into<R>,
14{
15 it.into_iter().map(|n| n.into())
16}
17
18#[derive(Debug)]
20pub struct NodeSeq<IN, OUT: ImplicitClone + 'static>(IArray<OUT>, PhantomData<IN>);
21
22impl<IN: Into<OUT>, OUT: ImplicitClone + 'static> From<IN> for NodeSeq<IN, OUT> {
23 fn from(val: IN) -> Self {
24 Self(IArray::Single([val.into()]), PhantomData)
25 }
26}
27
28impl<IN: Into<OUT>, OUT: ImplicitClone + 'static> From<Option<IN>> for NodeSeq<IN, OUT> {
29 fn from(val: Option<IN>) -> Self {
30 Self(
31 val.map(|s| IArray::Single([s.into()])).unwrap_or_default(),
32 PhantomData,
33 )
34 }
35}
36
37impl<IN: Into<OUT>, OUT: ImplicitClone + 'static> From<Vec<IN>> for NodeSeq<IN, OUT> {
38 fn from(mut val: Vec<IN>) -> Self {
39 if val.len() == 1 {
40 let item = val.pop().unwrap();
41 Self(IArray::Single([item.into()]), PhantomData)
42 } else {
43 Self(val.into_iter().map(|x| x.into()).collect(), PhantomData)
44 }
45 }
46}
47
48impl<IN: Into<OUT> + ImplicitClone, OUT: ImplicitClone + 'static> From<IArray<IN>>
49 for NodeSeq<IN, OUT>
50{
51 fn from(val: IArray<IN>) -> Self {
52 Self(val.iter().map(|x| x.into()).collect(), PhantomData)
53 }
54}
55
56impl<IN: Into<OUT> + ImplicitClone, OUT: ImplicitClone + 'static> From<&IArray<IN>>
57 for NodeSeq<IN, OUT>
58{
59 fn from(val: &IArray<IN>) -> Self {
60 Self(val.iter().map(|x| x.into()).collect(), PhantomData)
61 }
62}
63
64impl<IN: Into<OUT> + Clone, OUT: ImplicitClone + 'static> From<&ChildrenRenderer<IN>>
65 for NodeSeq<IN, OUT>
66{
67 fn from(val: &ChildrenRenderer<IN>) -> Self {
68 Self(val.iter().map(|x| x.into()).collect(), PhantomData)
69 }
70}
71
72impl<IN, OUT: ImplicitClone + 'static> IntoIterator for NodeSeq<IN, OUT> {
73 type IntoIter = implicit_clone::unsync::Iter<Self::Item>;
74 type Item = OUT;
75
76 fn into_iter(self) -> Self::IntoIter {
77 self.0.iter()
78 }
79}
80
81#[doc(hidden)]
84pub fn __ensure_type<T>(_: T) {}
85
86pub fn print_node(n: &web_sys::Node) -> String {
88 use wasm_bindgen::JsCast;
89
90 match n.dyn_ref::<web_sys::Element>() {
91 Some(el) => el.outer_html(),
92 None => n.text_content().unwrap_or_default(),
93 }
94}
95
96pub(crate) trait RcExt<T: Clone> {
98 fn unwrap_or_clone(this: Self) -> T;
99}
100
101impl<T: Clone> RcExt<T> for std::rc::Rc<T> {
102 fn unwrap_or_clone(this: Self) -> T {
103 std::rc::Rc::try_unwrap(this).unwrap_or_else(|rc| (*rc).clone())
104 }
105}