cell_map/iterators/
layerers.rs

1//! Provides layerer types which are used in combination with [`Slicer`] types to determine the
2//! order and form in which data is produced from the layers within a [`CellMap`].
3//!
4//! [`CellMap`]: crate::CellMap
5//! [`Slicer`]: crate::iterators::slicers::Slicer
6
7// ------------------------------------------------------------------------------------------------
8// IMPORTS
9// ------------------------------------------------------------------------------------------------
10
11use std::collections::VecDeque;
12
13use crate::Layer;
14
15// ------------------------------------------------------------------------------------------------
16// TRAITS
17// ------------------------------------------------------------------------------------------------
18
19/// [`Layerer`] controls how items are iterated over a [`CellMap`]s layers.
20///
21/// [`CellMap`]: crate::CellMap
22pub trait Layerer<L>
23where
24    L: Layer,
25{
26    /// Returns the current layer.
27    fn current(&self) -> Option<L>;
28}
29
30// ------------------------------------------------------------------------------------------------
31// STRUCTS
32// ------------------------------------------------------------------------------------------------
33
34/// Produces data from a single layer in a [`CellMap`].
35///
36/// [`CellMap`]: crate::CellMap
37#[derive(Debug, Clone, Copy)]
38pub struct Single<L>
39where
40    L: Layer,
41{
42    pub(crate) layer: L,
43}
44
45/// Produces data from many layers in a [`CellMap`]
46///
47/// The data is produced in [`Layer::to_index()`] order.
48///
49/// [`CellMap`]: crate::CellMap
50#[derive(Debug, Clone)]
51pub struct Many<L>
52where
53    L: Layer,
54{
55    pub(crate) layers: VecDeque<L>,
56}
57
58/// Produces data from two layers in pairs of `(&from, &mut to)`, allowing you to map data from one
59/// layer into another.
60#[derive(Debug, Clone, Copy)]
61pub struct Map<L>
62where
63    L: Layer,
64{
65    pub(crate) from: L,
66    pub(crate) to: L,
67}
68
69// ------------------------------------------------------------------------------------------------
70// IMPLS
71// ------------------------------------------------------------------------------------------------
72
73impl<L> Layerer<L> for Single<L>
74where
75    L: Layer,
76{
77    fn current(&self) -> Option<L> {
78        Some(self.layer.clone())
79    }
80}
81
82impl<L> Layerer<L> for Many<L>
83where
84    L: Layer,
85{
86    fn current(&self) -> Option<L> {
87        self.layers.front().cloned()
88    }
89}
90
91impl<L> Layerer<L> for Map<L>
92where
93    L: Layer,
94{
95    fn current(&self) -> Option<L> {
96        Some(self.from.clone())
97    }
98}