use
{
crate :: { LocalSpawnHandle, SpawnHandle, JoinHandle, YieldNow } ,
std :: { future::Future, rc::Rc } ,
futures_task :: { FutureObj, LocalSpawn, Spawn, SpawnError } ,
futures_util :: { FutureExt, task::LocalSpawnExt, future::LocalFutureObj } ,
glommio_crate :: { LocalExecutor, LocalExecutorBuilder, GlommioError } ,
};
#[ derive(Debug, Clone) ]
#[ cfg_attr( nightly, doc(cfg( feature = "glommio" )) ) ]
pub struct GlommioCt
{
exec: Rc<LocalExecutor>,
}
impl GlommioCt
{
pub fn new( builder: LocalExecutorBuilder ) -> Result< Self, GlommioError<()> >
{
let exec = Rc::new( builder.make()? );
Ok( Self{ exec } )
}
pub fn block_on<F: Future>( &self, future: F ) -> F::Output
{
self.exec.run( future )
}
}
impl LocalSpawn for GlommioCt
{
fn spawn_local_obj( &self, future: LocalFutureObj<'static, ()> ) -> Result<(), SpawnError>
{
glommio_crate::spawn_local( future ).detach();
Ok(())
}
}
impl<Out: 'static> LocalSpawnHandle<Out> for GlommioCt
{
fn spawn_handle_local_obj( &self, future: LocalFutureObj<'static, Out> )
-> Result<JoinHandle<Out>, SpawnError>
{
let (remote, handle) = future.remote_handle();
glommio_crate::spawn_local( remote ).detach();
Ok( JoinHandle::remote_handle(handle) )
}
}
impl Spawn for GlommioCt
{
fn spawn_obj( &self, future: FutureObj<'static, ()> ) -> Result<(), SpawnError>
{
self.spawn_local( future )
}
}
impl<Out: Send + 'static> SpawnHandle<Out> for GlommioCt
{
fn spawn_handle_obj( &self, future: FutureObj<'static, Out> ) -> Result<JoinHandle<Out>, SpawnError>
{
let (remote, handle) = future.remote_handle();
glommio_crate::spawn_local( remote ).detach();
Ok( JoinHandle::remote_handle(handle) )
}
}
#[ cfg( feature = "timer" ) ]
impl crate::Timer for GlommioCt
{
fn sleep( &self, dur: std::time::Duration ) -> futures_core::future::BoxFuture<'static, ()>
{
futures_timer::Delay::new( dur ).boxed()
}
}
impl YieldNow for GlommioCt
{
fn yield_now( &self ) -> crate::YieldNowFut
{
if glommio_crate::executor().need_preempt()
{
crate::YieldNowFut{ done: false }
}
else
{
crate::YieldNowFut{ done: true }
}
}
}
#[ cfg(test) ]
mod tests
{
use super::*;
static_assertions::assert_not_impl_any!( GlommioCt: Send, Sync );
}