跳到主要内容

WeakSender

搜索

结构体 WeakSender 

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

一个 sender,不会阻止 channel 被关闭。

如果通道的所有 Sender 实例 都已被丢弃, 且仅剩 WeakSender 实例, 则该通道 被关闭。

为了发送消息, WeakSender 需要使用 WeakSender::upgrade 进行升级, 它返回 Option<Sender>。 如果所有 Sender 都已被丢弃, 则返回 None, 否则返回一个 Sender

§示例

use tokio::sync::mpsc::channel;

let (tx, _rx) = channel::<i32>(15);
let tx_weak = tx.downgrade();

// Upgrading will succeed because `tx` still exists.
assert!(tx_weak.upgrade().is_some());

// If we drop `tx`, then it will fail.
drop(tx);
assert!(tx_weak.clone().upgrade().is_none());

实现§

Source§

impl<T> WeakSender<T>

Source

pub fn upgrade(&self) -> Option<Sender<T>>

尝试将 WeakSender 转换为 Sender。如果还有其他 Sender 实例处于活动状态且通道先前未被丢弃,则返回 Some,否则返回 None。

Source

pub fn strong_count(&self) -> usize

返回 Sender 句柄的数量。

Source

pub fn weak_count(&self) -> usize

返回 WeakSender 句柄的数量。

Trait 实现§

Source§

impl<T> Clone for WeakSender<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 WeakSender<T>

Source§

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

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

impl<T> Drop for WeakSender<T>

Source§

fn drop(&mut self)

执行此类型的析构函数。 更多信息

自动 Trait 实现§

§

impl<T> Freeze for WeakSender<T>

§

impl<T> RefUnwindSafe for WeakSender<T>

§

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

§

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

§

impl<T> Unpin for WeakSender<T>

§

impl<T> UnsafeUnpin for WeakSender<T>

§

impl<T> UnwindSafe for WeakSender<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>

执行转换。