Giter VIP home page Giter VIP logo

pubsubhub-rs's Introduction

PubSubHub-rs

A library that implements a publish/subscribe system in rust, as a learning exercise.

Subscribers

This library provides a trait called Subscriber with a type parameter A. A type that implements Subscriber<A> provides a method called receive which is called by a pubsub system and passed a reference to an instance of A.

Here's an example subscriber implementation:

struct Food {
    amount: i32,
}

struct Dog {
    total_eaten: i32,
}
impl Subscriber<Food> for Dog {
    pub fn receive(&mut self, event: &Food) {
        self.total_eaten += amount;
    }
    as_any!()
}

When a Dog is subscribed to a pubsub, and a Food event is published, the receive method will be called.

PubSub

The library doesn't actually define a particular PubSub struct, but allows you to turn any struct into a pubsub system with a macro listing the types it should publish.

struct Food { amount: i32 }
struct Sleep { time: i32 }

#[publishes(Food, Sleep)]
struct PubSubHub {
    /* Any other struct fields you want in your pubsub system */
}

The macro will fill in new methods that can be used to publish and subscribe to the events supported by that struct. So, for example, you could use the PubSubHub to publish_Food or publish_Sleep:

let mut p = PubSubHub::new();
let d = Dog { } // Assume dog implements Subscriber<Food> and Subscriber<Sleep>

p.subscribe_Food(Box::new(d));
p.subscribe_Sleep(Box::new(d));

let f = Food { amount: 80 }
p.publish_Food(&f) // Will call the receive method for the Subscriber<Food> implementation on Dog

pubsubhub-rs's People

Contributors

ballen-affirma avatar

Watchers

Benjamin Allen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.