romap

Crates.ioromap
lib.rsromap
version0.4.0
created_at2024-12-18 12:58:56.896237+00
updated_at2025-02-05 11:12:43.214427+00
descriptionA trait for read-only-maps
homepage
repositoryhttps://github.com/m-mueller678/romap
max_upload_size
id1487467
size23,208
(m-mueller678)

documentation

README

romap

The RoMap trait describes a read-only-map. It is intended to allow library authors more flexibility in the types they accept.

This crate includes combinators and implementations for common containers.

use romap::{deref_value, project_value, union, RoMap};

trait MyPetListTrait {
    fn cats(&self) -> impl RoMap<str, Cat>;
    fn dogs(&self) -> impl RoMap<str, Dog>;
    fn all_pets(&self) -> impl RoMap<str, dyn Pet> {
        union(
            project_value(self.cats(), |p| p as &dyn Pet),
            project_value(self.dogs(), |p| p as &dyn Pet),
        )
    }
}

struct MyPetListImpl {
    cats: HashMap<String, Cat>,
    dogs: BTreeMap<&'static str, Box<Dog>>,
}

impl MyPetListTrait for MyPetListImpl {
    fn cats(&self) -> impl RoMap<str, Cat> {
        &self.cats
    }

    fn dogs(&self) -> impl RoMap<str, Dog> {
        deref_value(&self.dogs)
    }
}

License: MIT OR Apache-2.0

Commit count: 26

cargo fmt