6 releases

0.2.0 Mar 19, 2019
0.1.4 May 9, 2018
0.1.3 Apr 26, 2018

#286 in Data structures

Download history 1/week @ 2019-07-24 2/week @ 2019-07-31 10/week @ 2019-08-07 1/week @ 2019-08-14 69/week @ 2019-08-21 14/week @ 2019-08-28 19/week @ 2019-09-04 13/week @ 2019-09-11 22/week @ 2019-09-18 20/week @ 2019-09-25 9/week @ 2019-10-02 12/week @ 2019-10-16 7/week @ 2019-10-23 12/week @ 2019-10-30

56 downloads per month

Apache-2.0

28KB
547 lines

Lending Library

Travis Crates.io docs.rs

A data store that lends temporary ownership of stored values.

This allows for access and/or mutation of independent keys in the store simultaneously.

Example

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,
        o_id: i64,
        o_dat: &'static str,
    }
}

const EVTS: &[Event] = &[Event::Foo {id:1, dat:"a_val"},
                         Event::Foo {id:2, dat:"b_val"},
                         Event::Bar {id:1, o_id: 2, o_dat:"B_val"},
                         Event::Bar {id:1, o_id: 3, o_dat:"c_val"}];

struct Store {
    id_gen: Box<Iterator<Item = i64>>,
    id_to_dat: LendingLibrary<i64, Item>,
}

impl Store {
    fn new() -> Self {
        Store {
            id_gen: Box::new(0..),
            id_to_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, o_id, o_dat } => {
                let i = store.declare(id, "");
                let o = store.declare(o_id, o_dat);
                pro.link(&i, &o);
            }
        }
    }
}

No runtime deps