async_slot/lib.rs
1//! The crate provides an unbounded channel that only stores last value sent.
2//!
3//! There are two implementations a one is traditionally called `sync` in the
4//! futures world. It works across threads. And the `unsync` one, which only
5//! works in single thread, but is potentially more performant.
6//!
7//! The `sync` one which should be used by default is also reexported at
8//! the root of this crate.
9//!
10//! # Features
11//!
12//! 1. Compacts memory (only last value is kept)
13//! 2. Has `poll_cancel` and `is_canceled`
14//! 3. Single-producer/single-consumer
15//! 4. Never has backpressure (because value is just replaced)
16//! 5. Replaced value can be recovered if using `swap` method.
17//! 6. `Sync`, so if multi-producer is desired, `Sender` can be wrapped
18//! into an `Arc`/`Rc` and `swap` method is used to update value.
19//!
20//! # Example
21//!
22//! ```
23//! # extern crate futures;
24//! # extern crate async_slot;
25//! #
26//! # use futures::prelude::*;
27//! # use futures::stream::iter_ok;
28//! #
29//! # fn main() {
30//! let (tx, rx) = async_slot::channel::<i32>();
31//!
32//! tx.send_all(iter_ok(vec![1, 2, 3])).wait();
33//!
34//! let received = rx.collect().wait().unwrap();
35//! assert_eq!(received, vec![3]);
36//! # }
37//! ```
38//!
39
40#![warn(missing_docs)]
41#![warn(missing_debug_implementations)]
42
43extern crate futures;
44
45pub mod sync;
46pub mod unsync;
47
48pub use sync::{channel, Receiver, Sender};
49
50use std::fmt;
51use std::error::Error;
52
53/// Error type for sending, used when the receiving end of a channel is
54/// dropped
55#[derive(Clone, PartialEq, Eq, Debug)]
56pub struct SendError<T>(T);
57
58impl<T: fmt::Debug> fmt::Display for SendError<T> {
59 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60 write!(f, "error sending value {:?} through a slot channel, \
61 receiver has gone", self.0)
62 }
63}
64
65impl<T: fmt::Debug> Error for SendError<T> {
66 fn description(&self) -> &str {
67 "error sending value through a slot channel, receiver has gone"
68 }
69}