The only way to create a single tokio::sync::watch::Sender
without a Receiver
seems to be creating a channel
and dropping the Receiver
use tokio::sync::watch;
#[allow(unused_variables)]
fn main() {
let (sender, _) = watch::channel(());
// or:
let sender = watch::channel(()).0;
}
By having to use channel
instead of being able to create a Sender
through soemthing like Sender::new
, I needlessly create a Receiver
, even if I don't need one (yet). Note that it's always possible to create Receiver
s later:
use tokio::sync::watch;
fn main() {
let sender = watch::channel(()).0;
println!("{:?}", sender.send(()));
let _receiver = sender.subscribe();
println!("{:?}", sender.send(()));
}
(Also note Tokio issue #4957 which I filed in that context.)
So I wonder if there is a particular reason why there is no tokio::sync::watch::Sender::new
. The same applies to tokio::sync::broadcast::Sender
. Maybe it just has been forgotten?