Created
November 16, 2014 16:13
-
-
Save rrichardson/f6bef1a31e332046bdde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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