Appliance is a lightweight Rust framework for building highly customizable asynchronous components adapted for message-based intercommunications. This project is an attempt to simplify actix-like approach exempting a user from using predefined execution runtimes. With the library you can design any composition logic for async agents avoiding data races and unnecessary locks.
```rust use appliance::Appliance;
type PingAppliance = Appliance
enum PingMessage { RegisterPong(PongAppliance), Send(i16), }
struct PingState {
pong: Option
fn pinghandler(state: &mut PingState, message: PingMessage) { match message { PingMessage::RegisterPong(pong) => state.pong = Some(pong), PingMessage::Send(c) => { if let Some(pong) = &state.pong { println!("ping received {}", c); let _ = pong.handle(PongMessage::Send(c+1)); std::thread::sleep(std::time::Duration::fromsecs(1)); } } } }
enum PongMessage { RegisterPing(PingAppliance), Send(i16), }
struct PongState {
ping: Option
fn ponghandler(state: &mut PongState, message: PongMessage) { match message { PongMessage::RegisterPing(ping) => state.ping = Some(ping), PongMessage::Send(c) => { if let Some(ping) = &state.ping { println!("pong received {}", c); let _ = ping.handle(PingMessage::Send(c+1)); std::thread::sleep(std::time::Duration::fromsecs(1)); } } } }
fn main() { // Get the default (fair) executor. let executor = &appliance::DEFAULTEXECUTOR; // Create a ping appliance. let pingstate = PingState { pong: None }; let ping = Appliance::new(executor, pingstate, pinghandler, None); // Create a pong appliance. let pongstate = PongState { ping: None }; let pong = Appliance::new(executor, pongstate, ponghandler, None); // Register appliances at one another. let _ = ping.handle(PingMessage::RegisterPong(pong.clone())); let _ = pong.handle(PongMessage::RegisterPing(ping.clone())); // Ignite the ping-pong interaction. let _ = ping.handle(PingMessage::Send(0)); // Wait a bit to let the appliances talk. std::thread::sleep(std::time::Duration::fromsecs(10)); }
```
The recommended way to use this library is to add it as a dependency in your Cargo.toml
file:
[dependencies]
appliance = "0.1.0"