Skip to content
Eventually consistent lock-free (when reading) synchronization primitive
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Run rustfmt. Jun 12, 2019
tests Run rustfmt. Jun 12, 2019
.gitignore Initial Commit. Jun 11, 2019
.travis.yml Add Travis CI. Jun 11, 2019
Cargo.toml Version 0.1.2. Jun 12, 2019
LICENSE-APACHE Add the licenses. Jun 11, 2019
LICENSE-MIT Add the licenses. Jun 11, 2019
README.md Correct "lock-free" to "lock-free when reading". Jun 12, 2019

README.md

evc

Crates.io Docs.rs Build Status

A lock-free (when reading), eventually consistent synchronization primitive.

This primitive makes reading and writing possible at the same time, although refreshing is needed to make writes visible to the readers.

This crate is very similar to evmap, but generalized to any type. Unlike evmap, which wraps a HashMap, evc is lower level, meaning that you need to be able to cache all possible mutations on the inner type (OperationCache). Therefore making an extension trait and implementing it for WriteHandle<YourType> is encouraged, so that accessing the inner data can be done using regular methods (like evmap does internally).

Examples

VecWrapper

use evc::OperationCache;

#[derive(Clone, Debug, Default)]
struct VecWrapper(Vec<u16>);

#[derive(Clone, Copy, Debug)]
enum Operation {
    Push(u16),
    Remove(usize),
    Clear,
}

impl OperationCache for VecWrapper {
    type Operation = Operation;

    fn apply_operation(&mut self, operation: Self::Operation) {
        match operation {
            Operation::Push(value) => self.0.push(value),
            Operation::Remove(index) => { self.0.remove(index); },
            Operation::Clear => self.0.clear(),
        }
    }
}

let (mut w_handle, r_handle) = evc::new(VecWrapper::default());

w_handle.write(Operation::Push(42));
w_handle.write(Operation::Push(24));

assert_eq!(r_handle.read().0, &[]);

w_handle.refresh();

assert_eq!(r_handle.read().0, &[42, 24]);

w_handle.write(Operation::Push(55));
w_handle.write(Operation::Remove(0));
w_handle.refresh();

assert_eq!(r_handle.read().0, &[24, 55]);

w_handle.write(Operation::Clear);

assert_eq!(r_handle.read().0, &[24, 55]);

w_handle.refresh();

assert_eq!(r_handle.read().0, &[]);

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

You can’t perform that action at this time.