Skip to content

Instantly share code, notes, and snippets.

@apoelstra
Created July 9, 2014 00:49
Show Gist options
  • Save apoelstra/69c7615377c2b43a6eed to your computer and use it in GitHub Desktop.
Save apoelstra/69c7615377c2b43a6eed to your computer and use it in GitHub Desktop.
Select interface (look ma, no macros!)
/// A Select is a merge of multiple Receivers; you wait on a Select
/// and it blocks until any one of the attached Receivers sees
/// something.
struct Select<T>;
/// Identifier for Receivers attached to a Select
type RecvId = uint;
impl<T:Default> Select<T> {
/// Creates a new `Select` which receives objects of type `T`
fn new() -> Select<T>;
/// Adds a `Receiver` to the `Select`. Notice that this takes the `Receiver`;
/// `Receiver` implements `Clone` so this is no inconvienience to the user.
/// Returns an ID which can be used to remove the `Receiver`.
fn add<U>(&mut self, rx: Receiver<U>, callback: |U| -> T) -> RecvId;
/// Removes a `Reciever` with the given `id` from the `Select`.
fn remove(&mut self, id: RecvId);
/// Blocks until one of the contained receivers is ready to receive. Then
/// receive that data, call the corresponding callback, and return whatever
/// the callback does.
fn recv(&mut self) -> T {};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment