Struct pasts::Loop

source ·
pub struct Loop<S: Unpin, T, F: Stateful<S, T>> { /* private fields */ }
Expand description

Composable asynchronous event loop.

Selecting on Futures:

Select first completed future.

use async_main::{async_main, LocalSpawner};
use pasts::{prelude::*, Loop};

struct Exit;

struct App<'a> {
    tasks: &'a mut [BoxNotify<'static, &'static str>],
}

impl App<'_> {
    fn completion(&mut self, (id, val): (usize, &str)) -> Poll<Exit> {
        println!("Received message from {id}, completed task: {val}");

        Ready(Exit)
    }
}

#[async_main]
async fn main(_spawner: LocalSpawner) {
    let tasks: &mut [BoxNotify<'_, _>] = &mut [
        Box::pin(async { "Hello" }.fuse()),
        Box::pin(async { "World" }.fuse()),
    ];
    let mut app = App { tasks };

    // First task will complete first.
    Loop::new(&mut app).on(|s| s.tasks, App::completion).await;
}

Task spawning

Spawns tasks in a Vec, and removes them as they complete.

use async_main::{async_main, LocalSpawner};
use pasts::{prelude::*, Loop};

struct Exit;

struct App {
    tasks: Vec<BoxNotify<'static, &'static str>>,
}

impl App {
    fn completion(&mut self, (id, val): (usize, &str)) -> Poll<Exit> {
        println!("Received message from completed task: {val}");

        self.tasks.swap_remove(id);

        if self.tasks.is_empty() {
            Ready(Exit)
        } else {
            Pending
        }
    }
}

#[async_main]
async fn main(_spawner: LocalSpawner) {
    let mut app = App {
        tasks: vec![
            Box::pin(async { "Hello" }.fuse()),
            Box::pin(async { "World" }.fuse()),
        ],
    };

    Loop::new(&mut app)
        .on(|s| &mut s.tasks[..], App::completion)
        .await;
}

Implementations§

source§

impl<'a, S: Unpin, T> Loop<S, T, Never<'a, S>>

source

pub fn new(state: &'a mut S) -> Self

Create an empty event loop.

source§

impl<S: Unpin, T, F: Stateful<S, T>> Loop<S, T, F>

source

pub fn on<N: Notify + Unpin + ?Sized>( self, noti: impl for<'a> FnMut(&'a mut S) -> &'a mut N + Unpin, then: fn(_: &mut S, _: N::Event) -> Poll<T> ) -> Loop<S, T, impl Stateful<S, T>>

Register an event handler.

Trait Implementations§

source§

impl<S: Debug + Unpin, T: Debug, F: Debug + Stateful<S, T>> Debug for Loop<S, T, F>

source§

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

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

impl<S: Unpin, T: Unpin, F: Stateful<S, T>> Future for Loop<S, T, F>

§

type Output = T

The type of value produced on completion.
source§

fn poll(self: Pin<&mut Self>, t: &mut Task<'_>) -> Poll<T>

Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations§

§

impl<S, T, F> RefUnwindSafe for Loop<S, T, F>where F: RefUnwindSafe, S: RefUnwindSafe, T: RefUnwindSafe,

§

impl<S, T, F> Send for Loop<S, T, F>where F: Send, S: Send, T: Send,

§

impl<S, T, F> Sync for Loop<S, T, F>where F: Sync, S: Sync, T: Sync,

§

impl<S, T, F> Unpin for Loop<S, T, F>where T: Unpin,

§

impl<S, T, F> UnwindSafe for Loop<S, T, F>where F: UnwindSafe, S: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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<F> Fuse for Fwhere F: Future,

source§

fn fuse(self) -> Option<F>

Fuse the Future
source§

impl<T, U> Into<U> for Twhere 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<F> IntoFuture for Fwhere F: Future,

§

type Output = <F as Future>::Output

The output that the future will produce on completion.
§

type IntoFuture = F

Which kind of future are we turning this into?
source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.