SpawnHandle

Trait SpawnHandle 

Source
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 &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 &mut 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>

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<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<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>

Implementors§

Source§

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

Available on crate feature async_global and non-WebAssembly only.
Source§

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

Available on crate feature async_std and non-WebAssembly only.
Source§

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

Available on crate feature bindgen only.
Source§

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

Available on crate feature tokio_ct only.
Source§

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

Available on crate feature tokio_tp only.
Source§

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

Available on crate feature localpool only.
Source§

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

Available on crate feature threadpool only.
Source§

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

Available on crate feature glommio only.