https://github.com/polyphony-chat/pubserve
Simple, generic observer trait.
https://github.com/polyphony-chat/pubserve
design-patterns observer-pattern rust
Last synced: 8 months ago
JSON representation
Simple, generic observer trait.
- Host: GitHub
- URL: https://github.com/polyphony-chat/pubserve
- Owner: polyphony-chat
- License: mpl-2.0
- Created: 2024-07-12T23:07:02.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-03T09:18:52.000Z (almost 2 years ago)
- Last Synced: 2025-06-25T23:35:21.068Z (12 months ago)
- Topics: design-patterns, observer-pattern, rust
- Language: Rust
- Homepage: https://crates.io/crates/pubserve
- Size: 31.3 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> [!IMPORTANT]
> 📦️ We moved! Come check out our new place over on [Codeberg](https://codeberg.org/polyphony)! ([Repository link](https://codeberg.org/polyphony/pubserve))
>
> We have moved to [Codeberg](https://codeberg.org/polyphony) as our primary Git Forge, and the project is *alive and well* over there! :purple_heart: The GitHub repositories will be archived and potentially deleted in the future.
# 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:
```rust
use pubserve::Publisher;
let publisher = Publisher::::new();
```
Then, create a subscriber that will listen to the publisher:
```rust
use pubserve::Subscriber;
struct MySubscriber;
impl Subscriber for MySubscriber {
fn update(&self, message: &i32) {
// Do whatever you want with the data!
println!("Received data: {}", message);
}
}
```
Finally, subscribe the observer to the publisher:
```rust
use pubserve::ReferenceCounted;
let observer = MySubscriber;
let reference = ReferenceCounted::new(&observer);
publisher.subscribe(reference);
```
Now, whenever you publish data, all subscribed observers will be notified:
```rust
publisher.publish(42);
// STDOUT: "Received data: 42"
```
Unsubscribing is just as easy:
```rust
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](https://crates.io/crates/async-trait). 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](LICENSE) file for more information.
By committing to this repository, you agree to license your contributions under the MPL-2.0 license.