This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.

yew/utils/
mod.rs

1//! This module contains useful utilities to get information about the current document.
2
3use std::marker::PhantomData;
4
5use implicit_clone::unsync::IArray;
6use implicit_clone::ImplicitClone;
7use yew::html::ChildrenRenderer;
8
9/// Map `IntoIterator<Item = Into<T>>` to `Iterator<Item = T>`
10pub 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/// A special type necessary for flattening components returned from nested html macros.
19#[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/// Hack to force type mismatch compile errors in yew-macro.
82// TODO: replace with `compile_error!`, when `type_name_of_val` is stabilised (https://github.com/rust-lang/rust/issues/66359).
83#[doc(hidden)]
84pub fn __ensure_type<T>(_: T) {}
85
86/// Print the [web_sys::Node]'s contents as a string for debugging purposes
87pub 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
96// NOTE: replace this by Rc::unwrap_or_clone() when it becomes stable
97pub(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}