Skip to main content

Crate toys

Crate toys 

Source
Expand description

§toys

Lightweight application primitives for async Rust, built on Tokio.

Each primitive lives behind a feature flag so you only compile what you need.

§Feature flags

FlagDefaultEnables
eventTyped global broadcast event bus and the [asynchronous] attribute macro

§Event system

The event system decouples producers and consumers of typed events through a process-wide broadcast channel. Any number of handlers can subscribe to the same event type independently.

                    event::<E>().dispatch(e)
                             │
                             ▼
                  ┌──────────────────────┐
                  │    EventChannel<E>   │  ← one per type, process-global
                  └──────────┬───────────┘
                             │ broadcast
              ┌──────────────┼──────────────┐
              ▼              ▼              ▼
         Handler A       Handler B       Handler C

§Minimal example

use toys::event::{event, EventHandler, EventLoop};
use toys::asynchronous;
use std::sync::Arc;

pub enum Signal { Ping }

struct Logger;

#[asynchronous]
impl EventHandler<Signal> for Logger {
    async fn handle(self: Arc<Self>, _event: Signal) {
        println!("ping received");
    }
}

let logger = Arc::new(Logger) as Arc<dyn EventHandler<Signal>>;
EventLoop::<Signal>::new().dispatch(&[logger]).await;
event::<Signal>().dispatch(Signal::Ping).await.unwrap();