Skip to main content

Runtime

Struct Runtime 

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

A runtime: worker shards and a blocking pool. Dropping it stops the workers, drops the tasks they still hold, and waits for the threads to exit; then, on the dropping thread, for what the handles those tasks held left behind (the bytes a socket’s write-behind holds, a file’s writes) to land, each for at most a minute, so nothing they accepted is lost.

Implementations§

Source§

impl Runtime

Source

pub fn enter(&self) -> EnterGuard<'_>

Sets this runtime on the calling thread until the guard drops, so the free functions use it outside block_on.

let runtime = nuclei::Runtime::builder().worker_threads(1).build().unwrap();
let _guard = runtime.enter();
let task = nuclei::spawn(async { 2 + 2 });
assert_eq!(runtime.block_on(task), 4);
Source

pub fn id(&self) -> RuntimeId

The runtime’s id.

Source

pub fn shutdown_background(self)

Stops the workers and returns at once: the worker threads drop their tasks and exit on their own, without being waited for, and what the handles of those tasks left behind is delivered on threads of its own.

Source§

impl Runtime

Source

pub fn stats(&self) -> Stats

A snapshot of this runtime’s counters: Stats::shards lists its workers (stopped ones included) by place, and the blocking counts are its pool’s. The errors of threads that run no shard and the receive buffers missing are the process’s, as in stats(): they belong to no runtime. The shard of a thread inside this runtime’s block_on serves every runtime the thread enters, so only stats() lists it.

Source§

impl Runtime

Source

pub fn builder() -> Builder

A builder with every setting at its default.

Source

pub fn new() -> Result<Self>

Starts a runtime with every setting at its default.

§Errors

As for Builder::build.

Source

pub fn handle(&self) -> RuntimeHandle

A handle to spawn on this runtime from anywhere.

Source

pub fn block_on<F: Future>(&self, future: F) -> F::Output

Runs future to its end on the calling thread; see block_on.

Source

pub fn spawn<F>(&self, future: F) -> Task<F::Output> ⓘ
where F: Future + Send + 'static, F::Output: Send + 'static,

Spawns a task on one of the runtime’s shards; see spawn.

Source

pub fn spawn_blocking<F, T>(&self, f: F) -> Task<T> ⓘ
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

Runs f on the runtime’s blocking pool; see spawn_blocking.

Source

pub fn shutdown_timeout(self, timeout: Duration)

Stops the workers and waits for them up to timeout; workers still running a task then are left to exit on their own. What the handles of the tasks dropped left behind gets the rest of timeout to land, on this thread; what has not by then is given up.

Trait Implementations§

Source§

impl Debug for Runtime

Source§

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

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

impl Drop for Runtime

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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.

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

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.