跳到主要内容

AbortHandle

搜索

结构体 AbortHandle 

Source
pub struct AbortHandle { /* private fields */ }
展开描述

用于终止已派生任务的 owned 权限,不会等待任务完成。

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

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

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

实现§

Source§

impl AbortHandle

Source

pub fn abort(&self)

终止与此 handle 关联的任务。

等待一个被取消的任务,如果该任务在被取消时已经完成,则可能会正常完成;但大多数情况下,它会因 取消 而失败并返回 JoinError

如果任务已被取消(例如通过 JoinHandle::abort),则此方法不会做任何事。

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

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

Source

pub fn is_finished(&self) -> bool

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

请注意,即使任务上已调用了 abort,此方法也可能返回 false。这是因为取消过程可能需要一些时间,只有在取消完成后此方法才会返回 true

Source

pub fn id(&self) -> Id

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

Trait 实现§

Source§

impl Clone for AbortHandle

Source§

fn clone(&self) -> Self

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

1.0.0 · Source§

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

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

impl Debug for AbortHandle

Source§

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

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

impl Drop for AbortHandle

Source§

fn drop(&mut self)

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

impl RefUnwindSafe for AbortHandle

Source§

impl Send for AbortHandle

Source§

impl Sync for AbortHandle

Source§

impl UnwindSafe for AbortHandle

自动 Trait 实现§

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>

执行转换。