use crate :: { import::*, RtConfig, RtErr, RtErrKind };
#[ derive( Debug ) ]
pub struct Exec03
{
config : RtConfig ,
local : Option<RefCell< LocalPool03 >> ,
spawner: Option<RefCell< LocalSpawner03 >> ,
}
impl Default for Exec03
{
fn default() -> Self
{
Exec03::new( RtConfig::default() )
}
}
impl Exec03
{
pub fn new( config: RtConfig ) -> Self
{
match config
{
RtConfig::Local =>
{
let local = LocalPool03 ::new();
let spawner = local.spawner();
Exec03
{
config ,
local : Some( RefCell::new( local ) ) ,
spawner: Some( RefCell::new( spawner ) ) ,
}
}
RtConfig::Pool{..} => Exec03{ config, local: None, spawner: None },
}
}
pub fn config( &self ) -> &RtConfig
{
&self.config
}
pub fn run( &self )
{
match self.config
{
RtConfig::Local => self.local.as_ref().unwrap().borrow_mut().run(),
RtConfig::Pool{..} => {}, }
}
pub fn spawn( &self, fut: impl Future< Output = () > + 'static + Send ) -> Result< (), RtErr >
{
match self.config
{
RtConfig::Local =>
self.spawner.as_ref().unwrap().borrow_mut().spawn_local( fut )
.map_err( |_| RtErrKind::Spawn{ context: "Futures 0.3 LocalPool spawn".into() }.into() ),
RtConfig::Pool =>
{
#[ cfg( feature = "juliex" ) ]
{
juliex::spawn( fut );
Ok(())
}
#[ cfg( not( feature = "juliex" ) ) ]
Err( RtErrKind::Spawn{ context: "async_runtime was compiled without the juliex feature".into() }.into() )
}
}
}
pub fn spawn_local( &self, fut: impl Future< Output = () > + 'static ) -> Result< (), RtErr >
{
match self.config
{
RtConfig::Local =>
self.spawner.as_ref().unwrap().borrow_mut().spawn_local( fut )
.map_err( |_| RtErrKind::Spawn{ context: "Futures 0.3 LocalPool spawn".into() }.into() ),
RtConfig::Pool{..} => Err( RtErrKind::Spawn{ context: "Exec03 spawn_local when initialized executor is the threadpool. Use `spawn` to spawn on the threadpool or initialize the default executor for the thread to be the thread local executor".into() }.into() ),
}
}
}