#channel #bounded-channel #actor-model #actor #inbox #async

heph-inbox

Bounded capacity channel designed to be used as inbox for actors. Also supports one shot channels.

8 releases

0.2.3 Aug 27, 2022
0.2.2 Aug 14, 2021
0.2.1 Jun 3, 2021
0.1.3 May 31, 2021
0.1.1 Apr 8, 2021

#920 in Asynchronous

Download history 3/week @ 2024-07-21 24/week @ 2024-07-28 20/week @ 2024-09-22 1/week @ 2024-09-29

101 downloads per month
Used in 2 crates

MIT license

78KB
1.5K SLoC

Inbox

License: MIT Crates.io Docs

Bounded capacity channel.

The channel is a multi-producer, single-consumer (MPSC) bounded queue. It is designed to be used as inbox for actors, following the actor model.

Examples

Simple creation of a channel and sending a message over it.

use std::thread;

use heph_inbox::RecvError;

// Create a new small channel.
let (mut sender, mut receiver) = heph_inbox::new_small();

let sender_handle = thread::spawn(move || {
    if let Err(err) = sender.try_send("Hello world!".to_owned()) {
        panic!("Failed to send value: {}", err);
    }
});

let receiver_handle = thread::spawn(move || {
    // NOTE: this is just an example don't actually use a loop like this, it
    // will waste CPU cycles when the channel is empty!
    loop {
        match receiver.try_recv() {
            Ok(value) => println!("Got a value: {}", value),
            Err(RecvError::Empty) => continue,
            Err(RecvError::Disconnected) => break,
        }
    }
});

sender_handle.join().unwrap();
receiver_handle.join().unwrap();

License

Licensed under the MIT license (LICENSE or https://opensource.org/licenses/MIT).

Contribution

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

No runtime deps

Features