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
//! Structures for dealing with child components.

use std::ops::Deref;

use super::Any as AnyComponent;
use crate::element::Any as AnyElement;

/// An array of child components.
///
/// The main purpose of `Children<N>` is to provide a [`render`] method,
/// and to implement [`Default`].
///
/// [`render`]: #method.render
/// [`Default`]: https://doc.rust-lang.org/std/default/trait.Default.html
#[derive(Clone)]
pub struct Children<const N: usize>([AnyComponent; N]);

impl<const N: usize> Children<N> {
  /// Render the components in the array.
  pub fn render(&self) -> [AnyElement; N] {
    let mut components = [(); N].map(|()| AnyElement::default());

    for (i, component) in components.iter_mut().enumerate() {
      *component = self.0[i].render();
    }

    components
  }
}

impl<const N: usize> Default for Children<N> {
  fn default() -> Self {
    Self([(); N].map(|_| AnyComponent::default()))
  }
}

impl<const N: usize> Deref for Children<N> {
  type Target = [AnyComponent; N];

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

impl<const N: usize> From<[AnyComponent; N]> for Children<N> {
  fn from(children: [AnyComponent; N]) -> Self {
    Self(children)
  }
}