Trait Layer

Source
pub trait Layer: Clone {
    const NUM_LAYERS: usize;
    const FIRST: Self;

    // Required methods
    fn to_index(&self) -> usize;
    fn from_index(index: usize) -> Self;
    fn all() -> Vec<Self>;
}
Expand description

Trait which defines the type of the layer index.

Layer indices should be enums which implement the Layer trait. By limiting indices to enums we can guarentee that all possible values of layer exist, and therefore do not have to provide run time checking that the layer exists within the map.

This is enforced by the use of the #[derive(Layer)] macro, which can only be implemented on enums.

§Safety

Do not manually implement this trait for non-enum types, as CellMap will be unable to guarentee that the layer you’re attempting to access will be present in the map.

§Example

use cell_map::Layer;

#[derive(Layer, Clone)]
enum MyLayer {
    Height,
    Gradient
}

Required Associated Constants§

Source

const NUM_LAYERS: usize

Contains the total number of layers possible with this Layer

Source

const FIRST: Self

Contains the first layer variant

Required Methods§

Source

fn to_index(&self) -> usize

Maps each variant of the enum to a unique layer index, which can be used to get that layer from the map.

Source

fn from_index(index: usize) -> Self

Maps each layer index into a variant of the layer.

§Safety

If the provided index doesn’t match a layer this function will panic.

Source

fn all() -> Vec<Self>

Returns a vector of all layers in index order.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§