Skip to content
/ maparr Public

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

License

Notifications You must be signed in to change notification settings

zhiburt/maparr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

maparr (help with array based maps) 📙

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

Get started

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

use maparr::maparr;

maparr!(
    Continents;
    ASIA,
    AFRICA,
    AMERICA_NORTH,
    AMERICA_SOUTH,
    ANTARCTICA,
    EUROPE,
    AUSTRALIA,
);

const CONTINENT_SQUARE_MILES: Continents<usize> = maparr!(
    Continents;
    ASIA                = 17_212_000,
    AFRICA              = 11_608_000,
    AMERICA_NORTH       = 9_365_000,
    AMERICA_SOUTH       = 6_880_000,
    ANTARCTICA          = 5_100_000,
    EUROPE              = 3_837_000,
    AUSTRALIA           = 2_968_000,
);

fn main() {
    for (sq_miles, continent) in CONTINENT_SQUARE_MILES.into_iter().zip(Continents::names()) {
        println!("{continent:15} = {sq_miles:10} (sq mi)");
    }
}

You shall expect to get the following output in stdout.

ASIA            =   17212000 (sq mi)
AFRICA          =   11608000 (sq mi)
AMERICA_NORTH   =    9365000 (sq mi)
AMERICA_SOUTH   =    6880000 (sq mi)
ANTARCTICA      =    5100000 (sq mi)
EUROPE          =    3837000 (sq mi)
AUSTRALIA       =    2968000 (sq mi)

You can modify the built map (even in const context if allowed).

use maparr::maparr;

maparr!(
    Continents<usize>;
    ASIA,
    AFRICA,
    AMERICA_NORTH,
    AMERICA_SOUTH,
    ANTARCTICA,
    EUROPE,
    AUSTRALIA,
);

fn main() {
    let mut continents = maparr!(
        Continents;
        ASIA                = 17_212_000,
        AFRICA              = 11_608_000,
        AMERICA_NORTH       = 9_365_000,
        AMERICA_SOUTH       = 6_880_000,
        ANTARCTICA          = 5_100_000,
        EUROPE              = 3_837_000,
        AUSTRALIA           = 2_968_000,
    );

    continents.set(Continents::ASIA, 17_212_001);
    assert_eq!(continents[Continents::ASIA], 17_212_001);

    continents = continents.map(|value| value * 2);
    assert_eq!(continents[Continents::ASIA], 17_212_001 * 2);
}

Analogues

After being done I've got to know that there's already some takes on the same problem. The list just have the ones I've found, there might be other/better ones.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages