Crate minitask

Crate minitask 

Source
Expand description

§Minitask

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(()))))
  ));
});

Structs§

BackgroundTasksalloc
Map to register BackgroundTask and Stream the TaskUpdates form said tasks.
MessageBusalloc or portable-atomic
Combined Receiver and Sender for inbound and outbound messages respectivly.
TaskSenderalloc
Sender for task’s inbound messages.

Enums§

TaskUpdatealloc
Update message from BackgroundTasks

Traits§

BackgroundTaskalloc or portable-atomic
Main wrapper trait for starting and handling Tasks