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§
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere F: Future,
§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more