pub struct Sender<T> { /* private fields */ }
Expand description
Sending-half of the broadcast
channel.
May be used from many threads. Messages can be sent with
send
.
§Examples
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let (tx, mut rx1) = broadcast::channel(16);
let mut rx2 = tx.subscribe();
tokio::spawn(async move {
assert_eq!(rx1.recv().await.unwrap(), 10);
assert_eq!(rx1.recv().await.unwrap(), 20);
});
tokio::spawn(async move {
assert_eq!(rx2.recv().await.unwrap(), 10);
assert_eq!(rx2.recv().await.unwrap(), 20);
});
tx.send(10).unwrap();
tx.send(20).unwrap();
}
Implementations§
Source§impl<T> Sender<T>
impl<T> Sender<T>
Sourcepub fn new(capacity: usize) -> Sender<T>
pub fn new(capacity: usize) -> Sender<T>
Creates the sending-half of the broadcast
channel.
See the documentation of broadcast::channel
for more information on this method.
Sourcepub fn send(&self, value: T) -> Result<usize, SendError<T>>
pub fn send(&self, value: T) -> Result<usize, SendError<T>>
Attempts to send a value to all active Receiver
handles, returning
it back if it could not be sent.
A successful send occurs when there is at least one active Receiver
handle. An unsuccessful send would be one where all associated
Receiver
handles have already been dropped.
§Return
On success, the number of subscribed Receiver
handles is returned.
This does not mean that this number of receivers will see the message as
a receiver may drop or lag (see lagging) before receiving
the message.
§Note
A return value of Ok
does not mean that the sent value will be
observed by all or any of the active Receiver
handles. Receiver
handles may be dropped before receiving the sent message.
A return value of Err
does not mean that future calls to send
will fail. New Receiver
handles may be created by calling
subscribe
.
§Examples
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let (tx, mut rx1) = broadcast::channel(16);
let mut rx2 = tx.subscribe();
tokio::spawn(async move {
assert_eq!(rx1.recv().await.unwrap(), 10);
assert_eq!(rx1.recv().await.unwrap(), 20);
});
tokio::spawn(async move {
assert_eq!(rx2.recv().await.unwrap(), 10);
assert_eq!(rx2.recv().await.unwrap(), 20);
});
tx.send(10).unwrap();
tx.send(20).unwrap();
}
Sourcepub fn subscribe(&self) -> Receiver<T>
pub fn subscribe(&self) -> Receiver<T>
Creates a new Receiver
handle that will receive values sent after
this call to subscribe
.
§Examples
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let (tx, _rx) = broadcast::channel(16);
// Will not be seen
tx.send(10).unwrap();
let mut rx = tx.subscribe();
tx.send(20).unwrap();
let value = rx.recv().await.unwrap();
assert_eq!(20, value);
}
Sourcepub fn downgrade(&self) -> WeakSender<T>
pub fn downgrade(&self) -> WeakSender<T>
Converts the Sender
to a WeakSender
that does not count
towards RAII semantics, i.e. if all Sender
instances of the
channel were dropped and only WeakSender
instances remain,
the channel is closed.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of queued values.
A value is queued until it has either been seen by all receivers that were alive at the time it was sent, or has been evicted from the queue by subsequent sends that exceeded the queue’s capacity.
§Note
In contrast to Receiver::len
, this method only reports queued values and not values that
have been evicted from the queue before being seen by all receivers.
§Examples
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let (tx, mut rx1) = broadcast::channel(16);
let mut rx2 = tx.subscribe();
tx.send(10).unwrap();
tx.send(20).unwrap();
tx.send(30).unwrap();
assert_eq!(tx.len(), 3);
rx1.recv().await.unwrap();
// The len is still 3 since rx2 hasn't seen the first value yet.
assert_eq!(tx.len(), 3);
rx2.recv().await.unwrap();
assert_eq!(tx.len(), 2);
}
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if there are no queued values.
§Examples
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let (tx, mut rx1) = broadcast::channel(16);
let mut rx2 = tx.subscribe();
assert!(tx.is_empty());
tx.send(10).unwrap();
assert!(!tx.is_empty());
rx1.recv().await.unwrap();
// The queue is still not empty since rx2 hasn't seen the value.
assert!(!tx.is_empty());
rx2.recv().await.unwrap();
assert!(tx.is_empty());
}
Sourcepub fn receiver_count(&self) -> usize
pub fn receiver_count(&self) -> usize
Returns the number of active receivers.
An active receiver is a Receiver
handle returned from channel
or
subscribe
. These are the handles that will receive values sent on
this Sender
.
§Note
It is not guaranteed that a sent message will reach this number of
receivers. Active receivers may never call recv
again before
dropping.
§Examples
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let (tx, _rx1) = broadcast::channel(16);
assert_eq!(1, tx.receiver_count());
let mut _rx2 = tx.subscribe();
assert_eq!(2, tx.receiver_count());
tx.send(10).unwrap();
}
Sourcepub fn same_channel(&self, other: &Sender<T>) -> bool
pub fn same_channel(&self, other: &Sender<T>) -> bool
Returns true
if senders belong to the same channel.
§Examples
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let (tx, _rx) = broadcast::channel::<()>(16);
let tx2 = tx.clone();
assert!(tx.same_channel(&tx2));
let (tx3, _rx3) = broadcast::channel::<()>(16);
assert!(!tx3.same_channel(&tx2));
}
Sourcepub async fn closed(&self)
pub async fn closed(&self)
A future which completes when the number of Receivers subscribed to this Sender
reaches
zero.
§Examples
use futures::FutureExt;
use tokio::sync::broadcast;
#[tokio::main]
async fn main() {
let (tx, mut rx1) = broadcast::channel::<u32>(16);
let mut rx2 = tx.subscribe();
let _ = tx.send(10);
assert_eq!(rx1.recv().await.unwrap(), 10);
drop(rx1);
assert!(tx.closed().now_or_never().is_none());
assert_eq!(rx2.recv().await.unwrap(), 10);
drop(rx2);
assert!(tx.closed().now_or_never().is_some());
}
Sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
Returns the number of Sender
handles.
Sourcepub fn weak_count(&self) -> usize
pub fn weak_count(&self) -> usize
Returns the number of WeakSender
handles.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Sender<T>
impl<T> !RefUnwindSafe for Sender<T>
impl<T> Send for Sender<T>where
T: Send,
impl<T> Sync for Sender<T>where
T: Send,
impl<T> Unpin for Sender<T>
impl<T> !UnwindSafe for Sender<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);