Struct AsyncRuntime

Source
pub struct AsyncRuntime { /* private fields */ }
Expand description

§AsyncRuntime

An asynchonous runtime that provides simple API for integrating async functions into your synchronous code.

This struct utilizes tokio’s rt and rt-multi-thread features.

§Polling & Executing

  • poll - for polling futures to completion
  • execute - pushes the task into the runtime for scheduling and execution

§Examples

use thread_runner::{AsyncRuntime, AsyncFlavor};
use std::time::Duration;

// Create a new runtime that executes all tasks in the current thread.
let runtime = AsyncRuntime::new(AsyncFlavor::CurrentThread);

// Spawn a future on the runtime.
runtime.execute(async {
    // Do some asynchronous work here...
});

// Poll a future on the runtime and block until it completes.
let result = runtime.poll(async {
    // Do some asynchronous work here and return a value...
    42
});

// Shut down the runtime after a specified timeout duration.
runtime.terminate(Duration::from_secs(1));

§Note

The Runtime automatically shutsdown after completing all the futures but some futures may be indefinite. Such is when termiate becomes usefull

Implementations§

Source§

impl AsyncRuntime

Source

pub fn new(properties: AsyncFlavor) -> Self

Creates a new AsyncRuntime instance based on the given properties.

§Arguments
  • properties - A AsyncFlavor enum that specifies the type of runtime to create.
§Returns

A new AsyncRuntime instance with the specified configuration.

Source

pub fn execute<F: Send + 'static + Future>(&self, f: F)
where F::Output: Send + 'static,

Schedules the given future F to be executed on the runtime.

The execute method spawns a new task in the runtime and runs it asynchronously.

This function is non-blocking.

§Examples
use thread_runner::{AsyncRuntime, AsyncFlavor};

let runtime = AsyncRuntime::new(AsyncFlavor::CurrentThread);
runtime.execute(async {
    println!("This will execute on a single thread runtime.");
});
Source

pub fn poll<T, F: Future<Output = T>>(&self, f: F) -> T

Polls the Future to completion.

The poll method blocks the current thread and waits for the completion of the future.

§Arguments
  • f the future to execute
§Returns
  • T the Output of f

f: F: Future<Output = T>

§Examples
use thread_runner::{AsyncRuntime, AsyncFlavor};

let runtime = AsyncRuntime::new(AsyncFlavor::WorkerThreads(3));
let result = runtime.poll(async {
    42
});
assert_eq!(result, 42);
Source

pub fn terminate(self, timeout: Duration)

Terminate the runtime and wait for all remaining tasks to complete.

The terminate method initiates a graceful shutdown of the runtime, giving all active tasks a chance to complete before shutting down. If any task fails to complete within the specified timeout duration, the runtime will forcibly shut down.

§Examples
use thread_runner::{AsyncRuntime, AsyncFlavor};
use std::time::Duration;

let runtime = AsyncRuntime::new(AsyncFlavor::AllThreads);
runtime.execute(async {
    println!("This will execute on a single thread runtime.");
});
runtime.terminate(Duration::from_secs(1));
§Note

By default, the runtime will wait for all futures to complete before shutting down, which can be unnecessarily time-consuming in some situations. For these cases, it’s best to use the terminate method to specify a timeout for the shutdown.

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.