A data store that lends temporary ownership of stored values.
This allows for access and/or mutation of independent keys in the store simultaneously.
```rust use lending_library::*;
struct Processor; struct Item(String);
impl Item { fn gen(dat: &str) -> Self { Item(dat.to_string()) } }
impl Processor { fn link(&self, _first: &Item, _second: &Item) {} }
enum Event { Foo { id: i64, dat: &'static str, }, Bar { id: i64, oid: i64, odat: &'static str, } }
const EVTS: &[Event] = &[Event::Foo {id:1, dat:"aval"}, Event::Foo {id:2, dat:"bval"}, Event::Bar {id:1, oid: 2, odat:"Bval"}, Event::Bar {id:1, oid: 3, odat:"cval"}];
struct Store {
idgen: Box
impl Store { fn new() -> Self { Store { idgen: Box::new(0..), idto_dat: LendingLibrary::new(), } }
pub fn declare(&mut self, uid: i64, dat: &str) -> Loan<i64, Item> {
if !self.id_to_dat.contains_key(&uid) {
self.id_to_dat.insert(uid, Item::gen(dat));
}
self.id_to_dat.lend(&uid).unwrap()
}
}
fn main() { let mut store = Store::new(); let pro = Processor; for evt in EVTS { match *evt { Event::Foo { id, dat } => { store.declare(id, dat); } Event::Bar { id, oid, odat } => { let i = store.declare(id, ""); let o = store.declare(oid, odat); pro.link(&i, &o); } } } } ```