baton/
lib.rs

1//! Baton is a simple channel that only keeps the latest value.
2//! If you try to [Send] too quickly, then oops the [Recv] side will drop the baton.
3//!
4//! Comes with a useful [Baton] macro to create a [Send] and [Recv] half for an entire struct.
5//! This allows you to send and receive updates independently for each field so it's clear what changed.
6//!
7//! The API is inspired by `tokio::sync::watch` but with a simpler design given the 1:1 nature.
8//! Notably, [Recv::recv] will return a reference to the next value so you don't have to fumble around with `changed()` state.
9//! Additionally, there's no way to accidentally deadlock like with `tokio::sync::watch::Ref`.
10
11// Required for derive to work.
12extern crate self as baton;
13
14mod recv;
15mod send;
16
17pub use recv::*;
18pub use send::*;
19
20#[cfg(feature = "derive")]
21mod derive;
22#[cfg(feature = "derive")]
23pub use derive::*;
24
25use tokio::sync::watch;
26
27/// Create a new channel with a [Send] and [Recv] half.
28/// This is a simple channel with no back-pressure and an initial value.
29pub fn channel<T: Clone>(value: T) -> (Send<T>, Recv<T>) {
30    let (send, recv) = watch::channel(value);
31    (Send::new(send), Recv::new(recv))
32}