跳到主要内容

UnboundedSender

搜索

结构体 UnboundedSender 

Source
pub struct UnboundedSender<T> { /* private fields */ }
展开描述

向关联的 UnboundedReceiver 发送值。

实例 由 unbounded_channel 函数创建。

实现§

Source§

impl<T> UnboundedSender<T>

Source

pub fn send(&self, message: T) -> Result<(), SendError<T>>

尝试在不阻塞的情况下通过此 UnboundedSender 发送一条消息。

此方法未标记为 async,因为向无界通道发送消息永远不需要任何形式的等待。这是由于通道的无限容量,允许 send 操作立即完成。因此,send 方法可以在同步和异步代码中使用而不会出现问题。

如果通道的接收半部已关闭(无论是因为调用了 close 还是 UnboundedReceiver 已被丢弃),此函数返回错误。错误包含传递给 send 的值。

Source

pub async fn closed(&self)

当接收者已丢弃时完成。

这允许生产者在对所产生值的兴趣被取消时收到通知,并立即停止工作。

§Cancel safety

此方法是取消安全的。一旦通道关闭,它将永远保持关闭状态,所有后续对 closed 的调用都将立即返回。

§示例
use tokio::sync::mpsc;

let (tx1, rx) = mpsc::unbounded_channel::<()>();
let tx2 = tx1.clone();
let tx3 = tx1.clone();
let tx4 = tx1.clone();
let tx5 = tx1.clone();
tokio::spawn(async move {
    drop(rx);
});

futures::join!(
    tx1.closed(),
    tx2.closed(),
    tx3.closed(),
    tx4.closed(),
    tx5.closed()
);
println!("Receiver dropped");
Source

pub fn is_closed(&self) -> bool

检查通道是否已关闭。当 UnboundedReceiver 被丢弃时或调用 UnboundedReceiver::close 方法时会发生这种情况。

let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<()>();
assert!(!tx.is_closed());

let tx2 = tx.clone();
assert!(!tx2.is_closed());

drop(rx);
assert!(tx.is_closed());
assert!(tx2.is_closed());
Source

pub fn same_channel(&self, other: &Self) -> bool

如果发送者属于同一通道则返回 true。

§示例
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let  tx2 = tx.clone();
assert!(tx.same_channel(&tx2));

let (tx3, rx3) = tokio::sync::mpsc::unbounded_channel::<()>();
assert!(!tx3.same_channel(&tx2));
Source

pub fn downgrade(&self) -> WeakUnboundedSender<T>

将 UnboundedSender 转换为 WeakUnboundedSender,它不计入 RAII 语义——也就是说,如果通道的所有 UnboundedSender 实例都已被丢弃且仅剩 WeakUnboundedSender 实例,则通道被关闭。

Source

pub fn strong_count(&self) -> usize

返回 UnboundedSender 句柄的数量。

Source

pub fn weak_count(&self) -> usize

返回 WeakUnboundedSender 句柄的数量。

Trait 实现§

Source§

impl<T> Clone for UnboundedSender<T>

Source§

fn clone(&self) -> Self

返回值的副本。 更多信息
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 更多信息
Source§

impl<T> Debug for UnboundedSender<T>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息

自动 Trait 实现§

§

impl<T> Freeze for UnboundedSender<T>

§

impl<T> RefUnwindSafe for UnboundedSender<T>

§

impl<T> Send for UnboundedSender<T>
where T: Send,

§

impl<T> Sync for UnboundedSender<T>
where T: Send,

§

impl<T> Unpin for UnboundedSender<T>

§

impl<T> UnsafeUnpin for UnboundedSender<T>

§

impl<T> UnwindSafe for UnboundedSender<T>

Blanket 实现§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

获得所有权后的类型。
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. 更多信息
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 更多信息
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。