Crate maparr

source ·
Expand description

maparr (help with array based maps)

A rust macro to build a static Map based on const array.

The idea is that you define your map first, and then you can use it wheather nessary.

The only macro which is exported is maparr. Open it’s documentation to see its syntax and what are the methods available to you after definition.

Example

Basic example.

use maparr::maparr;

maparr!(Planets; Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune);

let planets_weight_10x24kg = maparr!(
    Planets;
    Mercury = 0.33,
    Venus   = 4.87,
    Earth   = 5.97,
    Mars    = 0.642,
    Jupiter = 1898.0,
    Saturn  = 568.0,
    Uranus  = 86.8,
    Neptune = 102.0,
);

assert_eq!(planets_weight_10x24kg[Planets::Neptune], 102.0);

Use as a constant example.

use maparr::maparr;

maparr!(Planets; Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune);

const PLANETS_DISTANCE_AU: Planets<f32> = maparr!(
    Planets;
    Mercury = 0.39,
    Venus   = 0.72,
    Earth   = 1.00,
    Mars    = 1.52,
    Jupiter = 5.20,
    Saturn  = 9.54,
    Uranus  = 19.22,
    Neptune = 30.06,
);

for (distance, id) in PLANETS_DISTANCE_AU.iter().into_iter().cloned().zip(Planets::keys()) {
    assert_eq!(PLANETS_DISTANCE_AU[id], distance);
}

Macros

  • A macros for declaration and definition of a maparr type.