51 releases (27 breaking)
0.29.0 | Jun 7, 2020 |
---|---|
0.27.0 | Apr 15, 2020 |
0.14.1 | Mar 31, 2020 |
0.5.2 | Dec 28, 2019 |
#3 in #stacked
105 downloads per month
31KB
732 lines
Synchronous and recursive event system.
Introduction
An event system is a collection of objects that can receive and send signals.
In revent
we construct something called a [Node] containing an object of interest. This
object can invoke objects in other node
s, which in turn can invoke other objects - including
the originator - safely. This is done by suspending the &mut self
to the contents of a node.
Example
use revent::Node;
let number = Node::new(123);
number.emit(|n| {
println!("{}", *n);
*n = 100;
println!("{}", *n);
});
See the documentation for [Channel] and [Slot] and [Suspend] for examples.
Intent
This library is intended to be used as a "suspendable RefCell
" by a bunch of objects which
wish to communicate with each other without using a central mediator object.
For instance, one can create a revent::Channel<dyn MyTrait>
which contains objects of
interest, where each object can inside its own handler do something along the lines of:
use revent::{Channel, Suspend};
trait MyTrait {
fn function(&mut self, channel: &Channel<dyn MyTrait>);
}
struct MyObject;
impl MyTrait for MyObject {
fn function(&mut self, channel: &Channel<dyn MyTrait>) {
// Do something...
self.suspend(|| {
channel.emit(|x| {
x.function(channel);
});
});
// Do something else...
}
}
The above allows the object to emit a signal on a channel it is part of, even calling itself
recursively without mutably aliasing by suspending &mut self
.
Dependencies
~105KB