pub trait SpawnHandle<Out: 'static + Send> {
    // Required method
    fn spawn_handle_obj(
        &self,
        future: FutureObj<'static, Out>
    ) -> Result<JoinHandle<Out>, SpawnError>;
}
Expand description

Lets you spawn and get a JoinHandle to await the output of a future.

This trait works much like the Spawn trait from the futures library. It takes a FutureObj so we can hopefully make it no_std compatible when needed. This also allows it to be object safe. For convenience, there is SpawnHandleExt which allows you to spawn a generic future directly without having to manually make the FutureObj.

SpawnHandleExt is automatically implemented but must be in scope, so this works:

use async_executors::{ SpawnHandle, SpawnHandleExt };

async fn need_exec( exec: impl SpawnHandle<()> )
{
   let join_handle = exec.spawn_handle( async {} ).expect( "spawn" );

   join_handle.await;
}

and so does this:

use async_executors::{ SpawnHandle, SpawnHandleExt };

async fn need_exec( exec: Box< dyn SpawnHandle<()> > )
{
   let join_handle = exec.spawn_handle( async {} ).expect( "spawn" );

   join_handle.await;
}

One inconvenience of it having to be object safe is that the trait needs to be generic over the output parameter. This can be annoying if you need an executor that can spawn futures with different output parameters. Normally you should always be able to know which ones you need. If not you will have to make the type that stores the executor generic over the output type as well.

So to enable several output types you can use the following workaround. You can also use the trait-set crate to make that more streamlined.

Required Methods§

source

fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>

Spawn a future and return a JoinHandle that can be awaited for the output of the future.

Implementations on Foreign Types§

source§

impl<Out: 'static + Send, SH: SpawnHandle<Out> + ?Sized> SpawnHandle<Out> for &mut SH

source§

fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>

source§

impl<T, Out> SpawnHandle<Out> for WithDispatch<T>where T: SpawnHandle<Out>, Out: 'static + Send,

Available on crate feature tracing only.
source§

fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>

source§

impl<Out: 'static + Send, SH: SpawnHandle<Out> + ?Sized> SpawnHandle<Out> for &SH

source§

fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>

source§

impl<T, Out> SpawnHandle<Out> for Instrumented<T>where T: SpawnHandle<Out>, Out: 'static + Send,

Available on crate feature tracing only.
source§

fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>

source§

impl<Out: 'static + Send, SH: SpawnHandle<Out>> SpawnHandle<Out> for Box<SH>

source§

fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>

source§

impl<Out: 'static + Send, SH: SpawnHandle<Out> + ?Sized> SpawnHandle<Out> for Rc<SH>

source§

fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>

source§

impl<Out: 'static + Send, SH: SpawnHandle<Out> + ?Sized> SpawnHandle<Out> for Arc<SH>

source§

fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>

Implementors§

source§

impl<Out: 'static + Send> SpawnHandle<Out> for AsyncGlobal

source§

impl<Out: 'static + Send> SpawnHandle<Out> for AsyncStd

source§

impl<Out: 'static + Send> SpawnHandle<Out> for Bindgen

source§

impl<Out: 'static + Send> SpawnHandle<Out> for TokioCt

source§

impl<Out: 'static + Send> SpawnHandle<Out> for TokioTp

source§

impl<Out: 'static + Send> SpawnHandle<Out> for LocalSpawner

source§

impl<Out: 'static + Send> SpawnHandle<Out> for ThreadPool

source§

impl<Out: Send + 'static> SpawnHandle<Out> for GlommioCt