cell_map/
layer.rs

1//! Provies definition of the [`Layer`] trait.
2
3// ------------------------------------------------------------------------------------------------
4// TRAITS
5// ------------------------------------------------------------------------------------------------
6
7/// Trait which defines the type of the layer index.
8///
9/// Layer indices should be `enum`s which implement the [`Layer`] trait. By limiting indices to
10/// `enums` we can guarentee that all possible values of layer exist, and therefore do not have to
11/// provide run time checking that the layer exists within the map.
12///
13/// This is enforced by the use of the `#[derive(Layer)]` macro, which can only be implemented on
14/// `enums`.
15///
16/// # Safety
17///
18/// Do not manually implement this trait for non-enum types, as [`CellMap`] will be unable to
19/// guarentee that the layer you're attempting to access will be present in the map.
20///
21/// # Example
22/// ```
23/// use cell_map::Layer;
24///
25/// #[derive(Layer, Clone)]
26/// enum MyLayer {
27///     Height,
28///     Gradient
29/// }
30/// ```
31///
32/// [`CellMap`]: crate::CellMap
33pub trait Layer: Clone {
34    /// Contains the total number of layers possible with this [`Layer`]
35    const NUM_LAYERS: usize;
36
37    /// Contains the first layer variant
38    const FIRST: Self;
39
40    /// Maps each variant of the enum to a unique layer index, which can be used to get that layer
41    /// from the map.
42    fn to_index(&self) -> usize;
43
44    /// Maps each layer index into a variant of the layer.
45    ///
46    /// # Safety
47    ///
48    /// If the provided index doesn't match a layer this function will panic.
49    fn from_index(index: usize) -> Self;
50
51    /// Returns a vector of all layers in index order.
52    fn all() -> Vec<Self>;
53}