Struct AsyncRuntime

Source
pub struct AsyncRuntime {
    pub name: Arc<String>,
    /* private fields */
}
Expand description

Offers a unified interface for awaiting both async tasks and thread-blocking tasks from any context.

Clone is intentionally not implemented so that an AsyncRuntime can only be shared explicitly, as the destructor needs to shut down the underlying owned Tokio runtime, so it needs to be called only once all references are dropped.

Dropping an [‘AsyncRuntime’] also extracts and shuts down the underlying owned Tokio runtime without waiting for tasks to finish. This allows dropping it also from async contexts, but users may want to ensure manually that all tasks are finished beforehand.

Fields§

§name: Arc<String>

Implementations§

Source§

impl AsyncRuntime

Source

pub fn new(name: impl Into<String>, tokio: Option<Runtime>) -> AsyncRuntime

Owns the passed runtime, using it only if no contextual handle is available; if None is passed, it creates a runtime with multi-threaded support, CPU-based thread pool size and all features enabled.

Source

pub fn block_on_async<R>(&self, f: impl Future<Output = R>) -> R

Blocks the current thread executing the passed future to completion. In an async context, this relinquishes an executor thread to then re-enter the async context, which is inefficient and should be done sparingly.

Source

pub fn spawn_async<R>( &self, f: impl Future<Output = R> + Send + 'static, ) -> JoinHandle<R>
where R: Send + 'static,

Source

pub fn thread_blocking<R>(&self, f: impl FnOnce() -> R) -> R

Source

pub fn spawn_thread_blocking_send<MsgT>( &self, f: impl FnOnce() -> MsgT + Send + 'static, actor_ref: impl ActorRef<MsgT> + 'static, delay: Option<Duration>, ) -> JoinHandle<()>
where MsgT: ActorMsg + 'static,

Spawn an async task that may allocate an executor thread to execute a possibly long-running and thread-blocking function to completion, then sending the result to the passed actor reference.

It can be called from any context but creating a dedicated thread to run the thread-blocking function, is inefficient and should be done sparingly.

Trait Implementations§

Source§

impl Debug for AsyncRuntime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for AsyncRuntime

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.