Runtime

Struct Runtime 

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

Simple single-threaded futures runtime for Wasm

This is only available if building with the with_async feature flag:

[dependencies]
ark-api = { version = "*", features = ["with_async"] }

§Usage

To use this runtime to run async code call the spawn method on a future and then call poll on every frame.

If you are writing an applet you may want to create a new Runtime in your new method and call poll on every frame in the update method.


struct Module {
    runtime: Runtime,
    channel: Receiver<String>,
}

impl Applet for Module {
    fn new() -> Self {
        let mut runtime = Runtime::new();

        // Call an async function to get a future
        let future = some_async_function();
        // Run the future on the runtime, getting back a channel for the final result
        let channel = runtime.spawn(future);

        Self { channel, runtime }
    }

    fn update(&mut self) {
        // Poll the runtime to run the future
        self.runtime.poll();

        // Check the channel to see if the future has resolved yet
        match self.channel.try_recv() {
            Ok(Some(value)) => println!("The future resolved returning {}", value),
            _ => println!("Still waiting for the future"),
        };
    }
}

Here we are adding the future to the runtime with the spawn method in the new method, but futures can be added to the runtime at any point in your Ark module.

Implementations§

Source§

impl Runtime

Source

pub fn new() -> Self

Creates new runtime

Source

pub fn poll(&mut self)

Poll the runtime to update futures. This should be called on every frame until all futures managed by the runtime have resolved.

Source

pub fn spawn<T: Send + 'static>( &mut self, future: impl Future<Output = T> + 'static + Send, ) -> Receiver<T>

Spawn a future, returning a channel that will receive the result of the future once it resolves.

Ensure to use .try_recv rather than .recv when checking if the channel contains the result of the future as .recv will block until the future resolves, resulting in poor player experience and performance.

Source

pub fn block_on<T>(&mut self, future: impl Future<Output = T>) -> T

Drives a future to completion and returns the result.

Note this function is asynchronous and will block until the future has resolved. Using this function in your game’s update loop will likely result in poor player experience and performance.

Trait Implementations§

Source§

impl Default for Runtime

Source§

fn default() -> Runtime

Returns the “default value” for a 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.