#specs #events

shrev

Event channel, meant to be used with specs

17 releases (2 stable)

1.0.1 May 13, 2018
0.8.2 Apr 24, 2018
0.8.1 Dec 17, 2017
0.6.0 Oct 6, 2017

#1 in Game engines

Download history 991/week @ 2018-11-14 848/week @ 2018-11-21 998/week @ 2018-11-28 859/week @ 2018-12-05 882/week @ 2018-12-12 965/week @ 2018-12-19 778/week @ 2018-12-26 825/week @ 2019-01-02 589/week @ 2019-01-09 731/week @ 2019-01-16 758/week @ 2019-01-23 652/week @ 2019-01-30 613/week @ 2019-02-06 846/week @ 2019-02-13 468/week @ 2019-02-20

3,843 downloads per month
Used in 44 crates (10 directly)

MIT/Apache

28KB
701 lines

crates.io badge docs badge

A pull based event channel, with events stored in a ring buffer, meant to be used as a resource in specs.

Example usage

extern crate shrev;

use shrev::EventChannel;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TestEvent {
    data: u32,
}

fn main() {
    let mut channel = EventChannel::new();

    channel.drain_vec_write(&mut vec![TestEvent { data: 1 }, TestEvent { data: 2 }]);

    let mut reader_id = channel.register_reader();

    // Should be empty, because reader was created after the write
    assert_eq!(
        Vec::<TestEvent>::default(),
        channel.read(&mut reader_id).cloned().collect::<Vec<_>>()
    );

    // Should have data, as a second write was done
    channel.single_write(TestEvent { data: 5 });

    assert_eq!(
        vec![TestEvent { data: 5 }],
        channel.read(&mut reader_id).cloned().collect::<Vec<_>>()
    );

    // We can also just send in an iterator.
    channel.iter_write(
        [TestEvent { data: 8 }, TestEvent { data: 9 }]
            .iter()
            .cloned(),
    );

    assert_eq!(
        vec![TestEvent { data: 8 }, TestEvent { data: 9 }],
        channel.read(&mut reader_id).cloned().collect::<Vec<_>>()
    );
}

Dependencies

~2MB
~33K SLoC