romap 0.1.1

A trait for read-only-maps
Documentation
  • Coverage
  • 57.89%
    11 out of 19 items documented1 out of 11 items with examples
  • Size
  • Source code size: 16.17 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.13 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • m-mueller678/romap
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • m-mueller678

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