6
3
Fork
You've already forked pubserve
0
Simple, generic observer trait. https://crates.io/crates/pubserve
  • Rust 100%
2024年07月13日 16:09:03 +02:00
.github/workflows Create rust.yml 2024年07月13日 13:25:35 +02:00
src impl Clone and PartialEq for Publisher 2024年07月13日 16:06:17 +02:00
tests impl Clone and PartialEq for Publisher 2024年07月13日 16:06:17 +02:00
.gitignore Initial commit 2024年07月13日 01:08:43 +02:00
Cargo.lock Version 1.1.0 stable 2024年07月13日 16:06:36 +02:00
Cargo.toml Version 1.1.0 stable 2024年07月13日 16:06:36 +02:00
LICENSE Initial commit 2024年07月13日 01:07:02 +02:00
README.md Rename crate 2024年07月13日 13:20:30 +02:00

pubserve

Publish and observe.

Simple, generic observer pattern implementation for Rust.

Usage

pubserve is really simple to use. Start by creating a Publisher for your data type:

usepubserve::Publisher;letpublisher=Publisher::<i32>::new();

Then, create a subscriber that will listen to the publisher:

usepubserve::Subscriber;struct MySubscriber;implSubscriber<i32>forMySubscriber{fn update(&self,message: &i32){// Do whatever you want with the data!
println!("Received data: {}",message);}}

Finally, subscribe the observer to the publisher:

usepubserve::ReferenceCounted;letobserver=MySubscriber;letreference=ReferenceCounted::new(&observer);publisher.subscribe(reference);

Now, whenever you publish data, all subscribed observers will be notified:

publisher.publish(42);// STDOUT: "Received data: 42"

Unsubscribing is just as easy:

publisher.unsubscribe(reference);publisher.publish(42);// Nothing will be printed

It is of course possible to have multiple implementations of Subscriber be subscribed to the same Publisher. All of them will be notified immediately when data is published.

Features

No features are enabled by default. The following features are available:

Feature Description
send Makes the Subscriber trait Send and thus thread-safe.
async Makes the Subscriber trait Sync and async via the async_trait crate. Additionally, makes the publishing method async by extension

The send and async features can both be enabled at the same time, but do not have to be, should you only need one of them.

License

This project is licensed under the MPL-2.0 license. See the LICENSE file for more information. By committing to this repository, you agree to license your contributions under the MPL-2.0 license.