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
use crate::{IntoView, View};
use leptos_reactive::Scope;
use std::{any::Any, fmt, rc::Rc};

/// Wrapper for arbitrary data that can be passed through the view.
#[derive(Clone)]
pub struct Transparent(Rc<dyn Any>);

impl Transparent {
    /// Creates a new wrapper for this data.
    pub fn new<T>(value: T) -> Self
    where
        T: 'static,
    {
        Self(Rc::new(value))
    }

    /// Returns some reference to the inner value if it is of type `T`, or `None` if it isn't.
    pub fn downcast_ref<T>(&self) -> Option<&T>
    where
        T: 'static,
    {
        self.0.downcast_ref()
    }
}

impl fmt::Debug for Transparent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Transparent").finish()
    }
}

impl PartialEq for Transparent {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(&self.0, &other.0)
    }
}

impl Eq for Transparent {}

impl IntoView for Transparent {
    fn into_view(self, _: Scope) -> View {
        View::Transparent(self)
    }
}