| Crates.io | minitask |
| lib.rs | minitask |
| version | 0.1.2 |
| created_at | 2025-04-23 20:03:36.063815+00 |
| updated_at | 2025-08-01 17:36:38.922878+00 |
| description | MiniTask a simple wrapper for async tasks |
| homepage | |
| repository | https://github.com/DmitryDodzin/minitask |
| max_upload_size | |
| id | 1646265 |
| size | 37,843 |
Small wrapper for spawning and observing async BackgroundTasks with a "messaging" interface.
The idea is to help with collecting multiple background task updates
use minitask::{BackgroundTask, BackgroundTasks, MessageBus, TaskUpdate};
use smol::stream::StreamExt;
struct Ping;
struct Pong;
struct PingPongTask;
impl BackgroundTask for PingPongTask {
type Task = smol::Task<Result<(), async_channel::SendError<Pong>>>;
type MessageIn = Ping;
type MessageOut = Pong;
fn run(self, message_bus: MessageBus<Self::MessageIn, Self::MessageOut>) -> Self::Task {
smol::spawn(async move {
while let Ok(message) = message_bus.recv().await {
message_bus.send(Pong).await?;
}
Ok(())
})
}
}
let mut tasks = BackgroundTasks::default();
let ping_pong = tasks.register("ping-pong", PingPongTask);
smol::spawn(async move { ping_pong.send(Ping).await }).detach();
smol::block_on(async move {
let first_update = tasks.next().await;
assert!(matches!(
first_update,
Some(("ping-pong", TaskUpdate::Message(Pong)))
));
let second_update = tasks.next().await;
assert!(matches!(
second_update,
Some(("ping-pong", TaskUpdate::Finished(Ok(()))))
));
});