跳到主要内容

AbortHandle

搜索

结构体 AbortHandle 

源代码
pub struct AbortHandle { /* 私有字段 */ }
展开描述

中止已生成任务的所拥有权限,无需等待其完成。

JoinHandle 不同,AbortHandle 表示等待任务完成的权限,只表示终止它的权限。

可以通过调用 AbortHandle::abort 方法中止任务。Drop AbortHandle 会释放终止任务的权限——它不会中止任务。

请注意,使用 spawn_blocking 生成的任务无法中止,因为它们不是异步的。如果对 spawn_blocking 任务调用 abort,那么这将没有任何效果,任务将继续正常运行。例外情况是如果任务尚未开始运行;在这种情况下,调用 abort 可能会阻止任务启动。

实现§

源代码§

impl AbortHandle

源代码

pub fn abort(&self)

中止与该句柄关联的任务。

await 一个已取消的任务可能会像往常一样完成(如果任务在被取消时已经完成),但最有可能的是失败并返回已取消JoinError

如果任务已被取消(例如通过 JoinHandle::abort),则此方法将不执行任何操作。

请注意,使用 spawn_blocking 派生的任务无法被中止,因为它们不是异步的。如果对 spawn_blocking 任务调用 abort则不会产生任何效果,任务将继续正常运行。例外情况是任务尚未开始运行;此时调用 abort 可能会阻止任务启动。

有关取消的更多信息,另请参阅 模块级文档

源代码

pub fn is_finished(&self) -> bool

检查与此 AbortHandle 关联的任务是否已完成。

请注意,即使已对任务调用了 abort,此方法也可能返回 false。这是因为取消过程可能需要一些时间,并且此方法在取消完成之前不会返回 true

源代码

pub fn id(&self) -> Id

返回相对于其他当前已派生任务能唯一标识此任务的 任务 ID

trait 实现§

源代码§

impl Clone for AbortHandle

源代码§

fn clone(&self) -> Self

返回一个克隆的 AbortHandle,可用于远程中止此任务。

1.0.0 · 源代码§

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

Performs copy-assignment from source. 更多信息
源代码§

impl Debug for AbortHandle

源代码§

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

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

impl Drop for AbortHandle

源代码§

fn drop(&mut self)

执行此类型的析构函数。 更多信息
源代码§

impl RefUnwindSafe for AbortHandle

源代码§

impl Send for AbortHandle

源代码§

impl Sync for AbortHandle

源代码§

impl UnwindSafe for AbortHandle

自动 trait 实现§

blanket 实现§

源代码§

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

源代码§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
源代码§

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

源代码§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
源代码§

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

源代码§

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

Mutably borrows from an owned value. 更多信息
源代码§

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

源代码§

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. 更多信息
源代码§

impl<T> From<T> for T

源代码§

fn from(t: T) -> T

原样返回参数。

源代码§

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

源代码§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换是 From<T> for U 实现选择执行的操作。

源代码§

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

源代码§

type Owned = T

获得所有权后的类型。
源代码§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. 更多信息
源代码§

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

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

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

源代码§

type Error = Infallible

转换出错时返回的类型。
源代码§

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

执行转换。
源代码§

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

源代码§

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

转换出错时返回的类型。
源代码§

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

执行转换。