Skip to content

Instantly share code, notes, and snippets.

@rrichardson
Created November 16, 2014 16:13
Show Gist options
  • Save rrichardson/f6bef1a31e332046bdde to your computer and use it in GitHub Desktop.
Save rrichardson/f6bef1a31e332046bdde to your computer and use it in GitHub Desktop.
trait Strategy<Output> {
fn send(&self, item: Result<&Output>);
fn accept(&mut self, sub: &Subscriber<Output>) -> Option<uint>;
}
type Sub<I> = (Subscription, Subscriber<I>);
//This is the most basic strategy which only accepts a single subscriber
struct SingleStrategy<O> {
subscriber: Option<Sub<O>>,
}
impl<O> SingleStrategy<O> {
fn new() -> SingleStrategy<O> { SingleStrategy { subscriber: None } }
}
impl<O> SingleStrategy for Strategy<O> {
pub fn send(&self, item: Result<&O>) {
self.subscriber.on_next(*item);
}
fn accept(&mut self, sub: &Subscriber<O>) -> Option<&Subscription> {
match self.subscriber {
Some(_) => { sub.on_error("SubscriptionsFull");
None }
None => { self.sub = (Subscription::new(), sub);
sub.on_subscribe(self.sub.ref0());
Some(self.sub.ref0) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment