intuitive/components/
children.rs

1//! Structures for dealing with child components.
2
3use std::ops::Deref;
4
5use super::Any as AnyComponent;
6use crate::element::Any as AnyElement;
7
8/// An array of child components.
9///
10/// The main purpose of `Children<N>` is to provide a [`render`] method,
11/// and to implement [`Default`].
12///
13/// [`render`]: #method.render
14/// [`Default`]: https://doc.rust-lang.org/std/default/trait.Default.html
15#[derive(Clone)]
16pub struct Children<const N: usize>([AnyComponent; N]);
17
18impl<const N: usize> Children<N> {
19  /// Render the components in the array.
20  pub fn render(&self) -> [AnyElement; N] {
21    let mut components = [(); N].map(|()| AnyElement::default());
22
23    for (i, component) in components.iter_mut().enumerate() {
24      *component = self.0[i].render();
25    }
26
27    components
28  }
29}
30
31impl<const N: usize> Default for Children<N> {
32  fn default() -> Self {
33    Self([(); N].map(|_| AnyComponent::default()))
34  }
35}
36
37impl<const N: usize> Deref for Children<N> {
38  type Target = [AnyComponent; N];
39
40  fn deref(&self) -> &Self::Target {
41    &self.0
42  }
43}
44
45impl<const N: usize> From<[AnyComponent; N]> for Children<N> {
46  fn from(children: [AnyComponent; N]) -> Self {
47    Self(children)
48  }
49}