pub trait Spawn<E: ?Sized> {
type Error;
// Required method
fn spawn<'life0, 'life1, 'async_trait>(
&'life0 self,
env: &'life1 mut E,
) -> Pin<Box<dyn Future<Output = Result<BoxFuture<'static, ExitStatus>, Self::Error>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
}Expand description
A trait for spawning commands.
Spawning a command is separated into two distinct parts: a future that requires a mutable environment to make progress, and a future which no longer needs any context and can make progress on its own.
This distinction allows a caller to drop an environment as soon as it is no longer needed, which will free up resources, and especially important in preventing deadlocks between pipelines (since the parent process will contain extra reader/writer ends of a pipe and may prevent processes from exiting).
Required Associated Types§
Required Methods§
Sourcefn spawn<'life0, 'life1, 'async_trait>(
&'life0 self,
env: &'life1 mut E,
) -> Pin<Box<dyn Future<Output = Result<BoxFuture<'static, ExitStatus>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn spawn<'life0, 'life1, 'async_trait>(
&'life0 self,
env: &'life1 mut E,
) -> Pin<Box<dyn Future<Output = Result<BoxFuture<'static, ExitStatus>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Spawn the command as a future which returns another future.
The first, or “outer” future returned is allowed to maintain references to both the type being spawned, and the environment itself. Once the command no longer needs a reference to the environment or any other data, it should return a second future which represents the final result of the command.
This separation allows the caller (or poller) to drop the environment as son as it is no longer needed, which will free up resources, and especially important in preventing deadlocks between pipelines (since the parent process will contain extra reader/writer ends of a pipe and may prevent processes from exiting).
Although the implementation is free to make any optimizations or
pre-computations, there should be no observable side-effects until the
very first call to poll or .await on the future. That way a constructed
future that was never polled could be dropped without the risk of unintended
side effects.