Struct async_executors::exec::TokioCt
source · [−]pub struct TokioCt { /* private fields */ }
tokio_ct
only.Expand description
An executor that uses a tokio::runtime::Runtime
with the current thread
and a tokio::task::LocalSet
. Can spawn !Send
futures.
Creation of the runtime
You must use TokioCtBuilder
to create the executor.
// Make sure to set the `tokio_ct` feature on async_executors.
//
use
{
async_executors :: { TokioCt, TokioCtBuilder, LocalSpawnHandleExt } ,
tokio :: { runtime::Builder } ,
std :: { rc::Rc } ,
};
// You must use the builder. This guarantees that TokioCt is always backed by a single threaded runtime.
// You can set other configurations by calling `tokio_builder()` on TokioCtBuilder, so you get
// access to the `tokio::runtime::Builder`.
//
let exec = TokioCtBuilder::new().build().expect( "create tokio runtime" );
// block_on takes a &self, so if you need to `async move`,
// just clone it for use inside the async block.
//
exec.block_on( async
{
let not_send = async { let rc = Rc::new(()); };
// We can spawn !Send futures here.
//
let join_handle = exec.spawn_handle_local( not_send ).expect( "spawn" );
join_handle.await;
});
Unwind Safety.
When a future spawned on this wrapper panics, the panic will be caught by tokio in the poll function.
You must only spawn futures to this API that are unwind safe. Tokio will wrap spawned tasks in
std::panic::AssertUnwindSafe
and wrap the poll invocation with std::panic::catch_unwind
.
They reason that this is fine because they require Send + 'static
on the task. As far
as I can tell this is wrong. Unwind safety can be circumvented in several ways even with
Send + 'static
(eg. parking_lot::Mutex
is Send + 'static
but !UnwindSafe
).
You should make sure that if your future panics, no code that lives on after the panic, nor any destructors called during the unwind can observe data in an inconsistent state.
Note: the future running from within block_on
as opposed to spawn
does not exhibit this behavior and will panic
the current thread.
Note that these are logic errors, not related to the class of problems that cannot happen in safe rust (memory safety, undefined behavior, unsoundness, data races, …). See the relevant catch_unwind RFC and it’s discussion threads for more info as well as the documentation of std::panic::UnwindSafe for more information.
Implementations
sourceimpl TokioCt
impl TokioCt
sourcepub fn block_on<F: Future>(&self, f: F) -> F::Output
pub fn block_on<F: Future>(&self, f: F) -> F::Output
This is the entry point for this executor. Once this call returns, no remaining tasks shall be polled anymore.
However the tasks stay in the executor, so if you make a second call to block_on
with a new task, the older
tasks will start making progress again.
For simplicity, it’s advised to just create top level task that you run through block_on
and make sure your
program is done when it returns.
See: tokio::runtime::Runtime::block_on
Panics
This function will panic if it is called from an async context, including but not limited to making a nested call. It will also panic if the provided future panics.
Trait Implementations
sourceimpl LocalSpawn for TokioCt
impl LocalSpawn for TokioCt
sourcefn spawn_local_obj(
&self,
future: LocalFutureObj<'static, ()>
) -> Result<(), SpawnError>
fn spawn_local_obj(
&self,
future: LocalFutureObj<'static, ()>
) -> Result<(), SpawnError>
Spawns a future that will be run to completion. Read more
sourcefn status_local(&self) -> Result<(), SpawnError>
fn status_local(&self) -> Result<(), SpawnError>
Determines whether the executor is able to spawn new tasks. Read more
sourceimpl<Out: 'static> LocalSpawnHandle<Out> for TokioCt
impl<Out: 'static> LocalSpawnHandle<Out> for TokioCt
sourcefn spawn_handle_local_obj(
&self,
future: LocalFutureObj<'static, Out>
) -> Result<JoinHandle<Out>, SpawnError>
fn spawn_handle_local_obj(
&self,
future: LocalFutureObj<'static, Out>
) -> Result<JoinHandle<Out>, SpawnError>
Spawn a future and return a JoinHandle
that can be awaited for the output of the future.
sourceimpl<R: Send + 'static> SpawnBlocking<R> for TokioCt
impl<R: Send + 'static> SpawnBlocking<R> for TokioCt
sourcefn spawn_blocking<F>(&self, f: F) -> BlockingHandle<R>ⓘNotable traits for BlockingHandle<T>impl<T: 'static> Future for BlockingHandle<T> type Output = T;
where
F: FnOnce() -> R + Send + 'static,
fn spawn_blocking<F>(&self, f: F) -> BlockingHandle<R>ⓘNotable traits for BlockingHandle<T>impl<T: 'static> Future for BlockingHandle<T> type Output = T;
where
F: FnOnce() -> R + Send + 'static,
Runs the provided closure on a thread where blocking is acceptable.
sourcefn spawn_blocking_dyn(
&self,
f: Box<dyn FnOnce() -> R + Send>
) -> BlockingHandle<R>ⓘNotable traits for BlockingHandle<T>impl<T: 'static> Future for BlockingHandle<T> type Output = T;
fn spawn_blocking_dyn(
&self,
f: Box<dyn FnOnce() -> R + Send>
) -> BlockingHandle<R>ⓘNotable traits for BlockingHandle<T>impl<T: 'static> Future for BlockingHandle<T> type Output = T;
Runs the provided closure on a thread where blocking is acceptable. This part of the trait is object safe but your closure must be boxed and you cannot have a return value. Read more
sourceimpl<Out: 'static + Send> SpawnHandle<Out> for TokioCt
impl<Out: 'static + Send> SpawnHandle<Out> for TokioCt
sourcefn spawn_handle_obj(
&self,
future: FutureObj<'static, Out>
) -> Result<JoinHandle<Out>, SpawnError>
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.
sourceimpl YieldNow for TokioCt
impl YieldNow for TokioCt
sourcefn yield_now(&self) -> YieldNowFutⓘNotable traits for YieldNowFutimpl Future for YieldNowFut type Output = ();
fn yield_now(&self) -> YieldNowFutⓘNotable traits for YieldNowFutimpl Future for YieldNowFut type Output = ();
Await this future in order to yield to the executor.
impl TokioIo for TokioCt
tokio_io
only.Auto Trait Implementations
impl !RefUnwindSafe for TokioCt
impl !Send for TokioCt
impl !Sync for TokioCt
impl Unpin for TokioCt
impl !UnwindSafe for TokioCt
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<Sp> LocalSpawnExt for Sp where
Sp: LocalSpawn + ?Sized,
impl<Sp> LocalSpawnExt for Sp where
Sp: LocalSpawn + ?Sized,
sourcefn spawn_local<Fut>(&self, future: Fut) -> Result<(), SpawnError> where
Fut: 'static + Future<Output = ()>,
fn spawn_local<Fut>(&self, future: Fut) -> Result<(), SpawnError> where
Fut: 'static + Future<Output = ()>,
Spawns a task that polls the given future with output ()
to
completion. Read more
sourcefn spawn_local_with_handle<Fut>(
&self,
future: Fut
) -> Result<RemoteHandle<<Fut as Future>::Output>, SpawnError> where
Fut: 'static + Future,
fn spawn_local_with_handle<Fut>(
&self,
future: Fut
) -> Result<RemoteHandle<<Fut as Future>::Output>, SpawnError> where
Fut: 'static + Future,
Spawns a task that polls the given future to completion and returns a future that resolves to the spawned future’s output. Read more
impl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<Sp> SpawnExt for Sp where
Sp: Spawn + ?Sized,
impl<Sp> SpawnExt for Sp where
Sp: Spawn + ?Sized,
sourcefn spawn<Fut>(&self, future: Fut) -> Result<(), SpawnError> where
Fut: 'static + Future<Output = ()> + Send,
fn spawn<Fut>(&self, future: Fut) -> Result<(), SpawnError> where
Fut: 'static + Future<Output = ()> + Send,
Spawns a task that polls the given future with output ()
to
completion. Read more
sourcefn spawn_with_handle<Fut>(
&self,
future: Fut
) -> Result<RemoteHandle<<Fut as Future>::Output>, SpawnError> where
Fut: 'static + Future + Send,
<Fut as Future>::Output: Send,
fn spawn_with_handle<Fut>(
&self,
future: Fut
) -> Result<RemoteHandle<<Fut as Future>::Output>, SpawnError> where
Fut: 'static + Future + Send,
<Fut as Future>::Output: Send,
Spawns a task that polls the given future to completion and returns a future that resolves to the spawned future’s output. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more