#[repr(C)]
pub struct TaskStorage<F: Future + 'static> { /* private fields */ }
Expand description

可以生成任务的原始存储器 Raw storage in which a task can be spawned.

这个结构体拥有生成一个 future 为 F 的任务所需的内存。 在给定的时间,TaskStorage 可能处于已生成或未生成状态。 你///可以使用 TaskStorage::spawn() 来生成它,如果它已经被生成,那么这个方法将会失败。

TaskStorage 必须一直存活,即使任务已经结束运行,它仍然不会被释放。因此,相关的方法需要 &'static self。 但是,它可以被重用。

embassy_executor::task 宏内部会分配一个 TaskStorage 类型的静态数组。 使用原始 Task 的最常见原因是控制在哪里给任务非配内存:在栈上,或者在堆上,例如使用 Box::leak 等。

需要使用 repr(C) 来保证任务在结构体的内存布局中,偏移量为 0 这使得 TaskHeader 和 TaskStorage 指针互相转换是安全的。


Raw storage in which a task can be spawned.

This struct holds the necessary memory to spawn one task whose future is F. At a given time, the TaskStorage may be in spawned or not-spawned state. You may spawn it with TaskStorage::spawn(), which will fail if it is already spawned.

A TaskStorage must live forever, it may not be deallocated even after the task has finished running. Hence the relevant methods require &'static self. It may be reused, however.

Internally, the embassy_executor::task macro allocates an array of TaskStorages in a static. The most common reason to use the raw Task is to have control of where the memory for the task is allocated: on the stack, or on the heap with e.g. Box::leak, etc.

Implementations§

source§

impl<F: Future + 'static> TaskStorage<F>

source

pub const fn new() -> Self

创建一个新的任务存储器,处于未创建的状态


Create a new TaskStorage, in not-spawned state.

source

pub fn spawn( &'static self, future: impl FnOnce() -> F ) -> SpawnToken<impl Sized>

尝试创建任务

future 闭包用来构造 future。 可以生成任务的情况下,它才被调用。它是一个闭包, 而不是简单的 future: F 参数,用来确保在适当的位置构造 future,多亏 NRVO 优化, 避免了在栈上的临时复制。

如果人物已经被创建,并且还没有运行结束,这个方法会失败。在这种情况下,这个错误会被推迟: 返回一个空的 SpawnToken,从而导致调用 Spawner::spawn() 返回错误。

一旦任务已经运行结束,你可以再一次创建它。允许在另一个不同的执行器中创建它。

NRVO优化,即命名返回值优化(Named Return Value Optimization),是Visual C++2005及之后版本支持的一种优化技术。 当一个函数的返回值是一个对象时,正常的返回语句的执行过程是将这个对象从当前函数的局部作用域拷贝到返回区,以便调用者可以访问。 然而,如果所有的返回语句都返回同一个对象,NRVO优化的作用是在这个对象建立的时候直接在返回区建立, 从而避免了函数返回时调用拷贝构造函数的需要,减少了对象的创建与销毁过程。


Try to spawn the task.

The future closure constructs the future. It’s only called if spawning is actually possible. It is a closure instead of a simple future: F param to ensure the future is constructed in-place, avoiding a temporary copy in the stack thanks to NRVO optimizations.

This function will fail if the task is already spawned and has not finished running. In this case, the error is delayed: a “poisoned” SpawnToken is returned, which will cause Spawner::spawn() to return the error.

Once the task has finished running, you may spawn it again. It is allowed to spawn it on a different executor.

Auto Trait Implementations§

§

impl<F> !RefUnwindSafe for TaskStorage<F>

§

impl<F> Send for TaskStorage<F>where F: Send,

§

impl<F> Sync for TaskStorage<F>

§

impl<F> Unpin for TaskStorage<F>where F: Unpin,

§

impl<F> !UnwindSafe for TaskStorage<F>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.