4 releases (2 breaking)
new 0.3.0 | Dec 20, 2024 |
---|---|
0.2.0 | Dec 18, 2024 |
0.1.1 | Dec 18, 2024 |
0.1.0 | Dec 18, 2024 |
#1885 in Rust patterns
157 downloads per month
22KB
563 lines
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
lib.rs
:
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)
}
}
Dependencies
~45KB