19 releases (5 stable)

1.1.3 Jun 4, 2022
1.1.1 May 4, 2019
1.0.1 May 13, 2018
0.8.2 Apr 24, 2018
0.6.0 Oct 6, 2017

#225 in Game dev

Download history 1934/week @ 2024-06-15 1958/week @ 2024-06-22 1105/week @ 2024-06-29 1047/week @ 2024-07-06 2340/week @ 2024-07-13 1783/week @ 2024-07-20 1861/week @ 2024-07-27 1510/week @ 2024-08-03 3342/week @ 2024-08-10 1921/week @ 2024-08-17 2042/week @ 2024-08-24 2207/week @ 2024-08-31 1699/week @ 2024-09-07 1695/week @ 2024-09-14 2166/week @ 2024-09-21 1907/week @ 2024-09-28

7,737 downloads per month
Used in 118 crates (11 directly)

MIT/Apache

43KB
954 lines

shrev

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<_>>()
    );
}

License

shrev-rs is free and open source software distributed under the terms of both the MIT License and the Apache License 2.0.

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.

No runtime deps