Last active
August 29, 2015 14:01
-
-
Save thehydroimpulse/3349102717e135c47d7b to your computer and use it in GitHub Desktop.
This file contains 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
use uuid::Uuid; | |
/// Broadcast represents a single bi-directional communication with two | |
/// nodes within the cluster. The communication does **not** need to be | |
/// bi-directional. Responses are completely optional. | |
/// | |
/// Each broadcast is tagged with a unique ID so that we may track | |
/// which node has received a given broadcast. | |
pub struct Broadcast<'a, T, R> { | |
/// A unique id (uuidv4) representing the broadcast. This will allow us to keep | |
/// track of it when dealing with many broadcasts and we receive them in | |
/// different orders. | |
id: Uuid, | |
/// Request is an arbitrary type. This allows users | |
/// to specify their own custom broadcasts to be sent and received. | |
request: T, | |
/// Each broadcast may have an **optional** response of a different | |
/// type than the request. The closure will be called once we receive | |
/// the full response. | |
/// | |
/// The closure is not guaranteed to be ran on a specific thread. | |
response: Option<|response: R|: 'a> | |
} | |
impl<'a, T, R> Broadcast<'a, T, R> { | |
pub fn new(message: T) -> Broadcast<'a, T, R> { | |
Broadcast { | |
id: Uuid::new_v4(), | |
request: message, | |
response: None | |
} | |
} | |
} | |
#[cfg(test)] | |
mod test { | |
use super::*; | |
#[test] | |
fn have_no_response() { | |
// error: cannot determine a type for this local variable: unconstrained type | |
let broadcast = Broadcast::new(123); | |
assert!(broadcast.response.is_none()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment