Crates.io | nysa |
lib.rs | nysa |
version | 0.2.2 |
source | src |
created_at | 2021-09-14 11:57:37.279152 |
updated_at | 2021-09-19 15:04:45.874434 |
description | A bus for passing messages around between independent subsystems of an application |
homepage | |
repository | https://github.com/liquidev/nysa |
max_upload_size | |
id | 451185 |
size | 21,876 |
A bus for passing messages around between independent subsystems of an application.
use std::time::Duration;
use nysa::global as bus;
enum Add {
Two(i32, i32),
Quit,
}
struct AdditionResult(i32);
fn main() {
let adder = std::thread::spawn(move || loop {
match bus::wait_for::<Add>().consume() {
Add::Two(a, b) => bus::push(AdditionResult(a + b)),
Add::Quit => break,
}
});
bus::push(Add::Two(1, 2));
std::thread::sleep(Duration::from_secs(1));
bus::push(Add::Two(4, 5));
std::thread::sleep(Duration::from_secs(1));
bus::push(Add::Quit);
adder.join().unwrap();
for message in &bus::retrieve_all::<AdditionResult>() {
let AdditionResult(x) = message.consume();
println!("{}", x);
}
}
Nysa is a thread-safe message bus for applications.
It exposes a safe and simple to use API while abstracting away all the dirty details of mutexes and condvars.
The main usage of nysa is desktop applications which rely on a lot of subsystems communicating with each other to download, process, convert, compute data. Each subsystem can wait for messages to arrive on the bus, and then push more messages onto the bus.
The core idea of nysa is to keep it simple, stupid. For nysa, code readability is more important than blazing fast performance. Making an application use multiple threads should not be a very difficult task.
This is also why nysa exposes a "default", static bus, available in the nysa::global
module.
Using this bus alone should be enough for most applications, but if performance ever becomes a
problem, it's possible to create multiple smaller buses for orchestrating sub-subsystems in
subsystems.
data race:
,
please report a bug.The name is a throwback to an old Polish van, ZSD Nysa, manufactured back in the times of the Polish People's Republic. One of its variants was a minibus, and that's what this library's name refers to.