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
use std::{ops::Deref, sync::Arc};

use super::{Component, Empty};

/// An opaque type holding a struct that implements [`Component`].
///
/// `Any` is rarely used directly, even when implementing [`Component`].
/// It is typically indirectly used when a component receives children,
/// through [`Children<N>`].
///
/// An example of a component that requires `Any` is [`Modal`]. This is
/// because it provides a function, [`Funcs::show`], that receives a component
/// and presents it.
///
/// [`Children<N>`]: children/struct.Children.html
/// [`Component`]: trait.Component.html
/// [`Funcs::show`]: experimental/modal/struct.Funcs.html#method.show
/// [`Modal`]: experimental/modal/struct.Modal.html
#[derive(Clone)]
pub struct Any(Arc<dyn Component + 'static + Send + Sync>);

impl Any {
  fn new<C: Component + 'static + Send + Sync>(component: C) -> Self {
    Any(Arc::new(component))
  }
}

impl Default for Any {
  fn default() -> Self {
    Empty::new()
  }
}

impl Deref for Any {
  type Target = Arc<dyn Component + Send + Sync>;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<C: Component + 'static + Send + Sync> From<C> for Any {
  fn from(component: C) -> Self {
    Self::new(component)
  }
}