From 8f2d21dc0d69de134e0ed70bcf1a15f4cac4973a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 1 Apr 2013 18:25:20 -0700 Subject: [PATCH] core: Just reordering some code --- src/libcore/pipes.rs | 162 +++++++++++++++++++++---------------------- 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/src/libcore/pipes.rs b/src/libcore/pipes.rs index fd50ecc5274a..ba94c2ae66a2 100644 --- a/src/libcore/pipes.rs +++ b/src/libcore/pipes.rs @@ -669,87 +669,6 @@ pub fn wait_many(pkts: &[T]) -> uint { ready_packet } -/** Receives a message from one of two endpoints. - -The return value is `left` if the first endpoint received something, -or `right` if the second endpoint receives something. In each case, -the result includes the other endpoint as well so it can be used -again. Below is an example of using `select2`. - -~~~ -match select2(a, b) { - left((none, b)) { - // endpoint a was closed. - } - right((a, none)) { - // endpoint b was closed. - } - left((Some(_), b)) { - // endpoint a received a message - } - right(a, Some(_)) { - // endpoint b received a message. - } -} -~~~ - -Sometimes messages will be available on both endpoints at once. In -this case, `select2` may return either `left` or `right`. - -*/ -pub fn select2( - a: RecvPacketBuffered, - b: RecvPacketBuffered) - -> Either<(Option, RecvPacketBuffered), - (RecvPacketBuffered, Option)> -{ - let i = wait_many([a.header(), b.header()]); - - match i { - 0 => Left((try_recv(a), b)), - 1 => Right((a, try_recv(b))), - _ => fail!(~"select2 return an invalid packet") - } -} - -#[doc(hidden)] -pub trait Selectable { - fn header(&self) -> *PacketHeader; -} - -impl Selectable for *PacketHeader { - fn header(&self) -> *PacketHeader { *self } -} - -/// Returns the index of an endpoint that is ready to receive. -pub fn selecti(endpoints: &[T]) -> uint { - wait_many(endpoints) -} - -/// Returns 0 or 1 depending on which endpoint is ready to receive -pub fn select2i(a: &A, b: &B) -> - Either<(), ()> { - match wait_many([a.header(), b.header()]) { - 0 => Left(()), - 1 => Right(()), - _ => fail!(~"wait returned unexpected index") - } -} - -/** Waits on a set of endpoints. Returns a message, its index, and a - list of the remaining endpoints. - -*/ -pub fn select(endpoints: ~[RecvPacketBuffered]) - -> (uint, Option, ~[RecvPacketBuffered]) -{ - let ready = wait_many(endpoints.map(|p| p.header())); - let mut remaining = endpoints; - let port = remaining.swap_remove(ready); - let result = try_recv(port); - (ready, result, remaining) -} - /** The sending end of a pipe. It can be used to send exactly one message. @@ -901,6 +820,87 @@ pub fn entangle() -> (SendPacket, RecvPacket) { (SendPacket(p), RecvPacket(p)) } +/** Receives a message from one of two endpoints. + +The return value is `left` if the first endpoint received something, +or `right` if the second endpoint receives something. In each case, +the result includes the other endpoint as well so it can be used +again. Below is an example of using `select2`. + +~~~ +match select2(a, b) { + left((none, b)) { + // endpoint a was closed. + } + right((a, none)) { + // endpoint b was closed. + } + left((Some(_), b)) { + // endpoint a received a message + } + right(a, Some(_)) { + // endpoint b received a message. + } +} +~~~ + +Sometimes messages will be available on both endpoints at once. In +this case, `select2` may return either `left` or `right`. + +*/ +pub fn select2( + a: RecvPacketBuffered, + b: RecvPacketBuffered) + -> Either<(Option, RecvPacketBuffered), + (RecvPacketBuffered, Option)> +{ + let i = wait_many([a.header(), b.header()]); + + match i { + 0 => Left((try_recv(a), b)), + 1 => Right((a, try_recv(b))), + _ => fail!(~"select2 return an invalid packet") + } +} + +#[doc(hidden)] +pub trait Selectable { + fn header(&self) -> *PacketHeader; +} + +impl Selectable for *PacketHeader { + fn header(&self) -> *PacketHeader { *self } +} + +/// Returns the index of an endpoint that is ready to receive. +pub fn selecti(endpoints: &[T]) -> uint { + wait_many(endpoints) +} + +/// Returns 0 or 1 depending on which endpoint is ready to receive +pub fn select2i(a: &A, b: &B) -> + Either<(), ()> { + match wait_many([a.header(), b.header()]) { + 0 => Left(()), + 1 => Right(()), + _ => fail!(~"wait returned unexpected index") + } +} + +/** Waits on a set of endpoints. Returns a message, its index, and a + list of the remaining endpoints. + +*/ +pub fn select(endpoints: ~[RecvPacketBuffered]) + -> (uint, Option, ~[RecvPacketBuffered]) +{ + let ready = wait_many(endpoints.map(|p| p.header())); + let mut remaining = endpoints; + let port = remaining.swap_remove(ready); + let result = try_recv(port); + (ready, result, remaining) +} + pub mod rt { use option::{None, Option, Some};