From 0617d580e6cfafb1fc662cfb9c9a3c23829fe2ae Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 16 Apr 2013 19:50:20 -0700 Subject: [PATCH] core::comm: Remove functions in favor of methods `send`, etc. are never used. I've left the functions for oneshot pipes because by-value methods don't work. --- src/libcore/comm.rs | 254 +++++++++++++++++--------------------------- 1 file changed, 96 insertions(+), 158 deletions(-) diff --git a/src/libcore/comm.rs b/src/libcore/comm.rs index f5e87d314eae..fe6dbddc75e4 100644 --- a/src/libcore/comm.rs +++ b/src/libcore/comm.rs @@ -132,25 +132,17 @@ pub mod streamp { } } -struct Chan_ { +/// An endpoint that can send many messages. +pub struct Chan { mut endp: Option> } -/// An endpoint that can send many messages. -pub enum Chan { - Chan_(Chan_) -} - -struct Port_ { +/// An endpoint that can receive many messages. +pub struct Port { mut endp: Option>, } -/// An endpoint that can receive many messages. -pub enum Port { - Port_(Port_) -} - -/** Creates a `(chan, port)` pair. +/** Creates a `(Port, Chan)` pair. These allow sending or receiving an unlimited number of messages. @@ -158,96 +150,70 @@ These allow sending or receiving an unlimited number of messages. pub fn stream() -> (Port, Chan) { let (c, s) = streamp::init(); - (Port_(Port_ { endp: Some(s) }), Chan_(Chan_{ endp: Some(c) })) -} - -// Add an inherent method so that imports of GenericChan are not -// required. -pub impl Chan { - fn send(&self, x: T) { chan_send(self, x) } - fn try_send(&self, x: T) -> bool { chan_try_send(self, x) } + (Port { endp: Some(s) }, Chan { endp: Some(c) }) } impl GenericChan for Chan { - fn send(&self, x: T) { chan_send(self, x) } -} - -#[inline(always)] -fn chan_send(self: &Chan, x: T) { - let mut endp = None; - endp <-> self.endp; - self.endp = Some( - streamp::client::data(endp.unwrap(), x)) + #[inline(always)] + fn send(&self, x: T) { + let mut endp = None; + endp <-> self.endp; + self.endp = Some( + streamp::client::data(endp.unwrap(), x)) + } } impl GenericSmartChan for Chan { + #[inline(always)] fn try_send(&self, x: T) -> bool { - chan_try_send(self, x) - } -} - -#[inline(always)] -fn chan_try_send(self: &Chan, x: T) -> bool { - let mut endp = None; - endp <-> self.endp; - match streamp::client::try_data(endp.unwrap(), x) { - Some(next) => { - self.endp = Some(next); - true + let mut endp = None; + endp <-> self.endp; + match streamp::client::try_data(endp.unwrap(), x) { + Some(next) => { + self.endp = Some(next); + true + } + None => false } - None => false } } -// Use an inherent impl so that imports are not required: -pub impl Port { - fn recv(&self) -> T { port_recv(self) } - fn try_recv(&self) -> Option { port_try_recv(self) } - fn peek(&self) -> bool { port_peek(self) } -} - impl GenericPort for Port { - // These two calls will prefer the inherent versions above: - fn recv(&self) -> T { port_recv(self) } - fn try_recv(&self) -> Option { port_try_recv(self) } -} + #[inline(always)] + fn recv(&self) -> T { + let mut endp = None; + endp <-> self.endp; + let streamp::data(x, endp) = recv(endp.unwrap()); + self.endp = Some(endp); + x + } -#[inline(always)] -fn port_recv(self: &Port) -> T { - let mut endp = None; - endp <-> self.endp; - let streamp::data(x, endp) = recv(endp.unwrap()); - self.endp = Some(endp); - x -} - -#[inline(always)] -fn port_try_recv(self: &Port) -> Option { - let mut endp = None; - endp <-> self.endp; - match try_recv(endp.unwrap()) { - Some(streamp::data(x, endp)) => { - self.endp = Some(endp); - Some(x) + #[inline(always)] + fn try_recv(&self) -> Option { + let mut endp = None; + endp <-> self.endp; + match try_recv(endp.unwrap()) { + Some(streamp::data(x, endp)) => { + self.endp = Some(endp); + Some(x) + } + None => None } - None => None } } impl Peekable for Port { - fn peek(&self) -> bool { port_peek(self) } -} - -#[inline(always)] -fn port_peek(self: &Port) -> bool { - let mut endp = None; - endp <-> self.endp; - let peek = match &endp { - &Some(ref endp) => peek(endp), - &None => fail!(~"peeking empty stream") - }; - self.endp <-> endp; - peek + #[inline(always)] + fn peek(&self) -> bool { + let mut endp = None; + endp <-> self.endp; + let peek = match &endp { + &Some(ref endp) => peek(endp), + &None => fail!(~"peeking empty stream") + }; + self.endp <-> endp; + peek + } } impl Selectable for Port { @@ -272,13 +238,6 @@ pub fn PortSet() -> PortSet{ } } -// Use an inherent impl so that imports are not required: -pub impl PortSet { - fn recv(&self) -> T { port_set_recv(self) } - fn try_recv(&self) -> Option { port_set_try_recv(self) } - fn peek(&self) -> bool { port_set_peek(self) } -} - pub impl PortSet { fn add(&self, port: Port) { self.ports.push(port) @@ -292,90 +251,69 @@ pub impl PortSet { } impl GenericPort for PortSet { - fn try_recv(&self) -> Option { port_set_try_recv(self) } - fn recv(&self) -> T { port_set_recv(self) } -} - -#[inline(always)] -fn port_set_recv(self: &PortSet) -> T { - port_set_try_recv(self).expect("port_set: endpoints closed") -} - -#[inline(always)] -fn port_set_try_recv(self: &PortSet) -> Option { - let mut result = None; - // we have to swap the ports array so we aren't borrowing - // aliasable mutable memory. - let mut ports = ~[]; - ports <-> self.ports; - while result.is_none() && ports.len() > 0 { - let i = wait_many(ports); - match ports[i].try_recv() { - Some(m) => { - result = Some(m); - } - None => { - // Remove this port. - let _ = ports.swap_remove(i); + fn try_recv(&self) -> Option { + let mut result = None; + // we have to swap the ports array so we aren't borrowing + // aliasable mutable memory. + let mut ports = ~[]; + ports <-> self.ports; + while result.is_none() && ports.len() > 0 { + let i = wait_many(ports); + match ports[i].try_recv() { + Some(m) => { + result = Some(m); + } + None => { + // Remove this port. + let _ = ports.swap_remove(i); + } } } + ports <-> self.ports; + result + } + fn recv(&self) -> T { + self.try_recv().expect("port_set: endpoints closed") } - ports <-> self.ports; - result } impl Peekable for PortSet { - fn peek(&self) -> bool { port_set_peek(self) } -} - -#[inline(always)] -fn port_set_peek(self: &PortSet) -> bool { - // It'd be nice to use self.port.each, but that version isn't - // pure. - for uint::range(0, vec::uniq_len(&const self.ports)) |i| { - // XXX: Botch pending demuting. - unsafe { - let port: &Port = cast::transmute(&mut self.ports[i]); - if port.peek() { return true } + fn peek(&self) -> bool { + // It'd be nice to use self.port.each, but that version isn't + // pure. + for uint::range(0, vec::uniq_len(&const self.ports)) |i| { + // XXX: Botch pending demuting. + unsafe { + let port: &Port = cast::transmute(&mut self.ports[i]); + if port.peek() { return true } + } } + false } - false } - /// A channel that can be shared between many senders. pub type SharedChan = unstable::Exclusive>; -pub impl SharedChan { - fn send(&self, x: T) { shared_chan_send(self, x) } - fn try_send(&self, x: T) -> bool { shared_chan_try_send(self, x) } -} - impl GenericChan for SharedChan { - fn send(&self, x: T) { shared_chan_send(self, x) } -} - -#[inline(always)] -fn shared_chan_send(self: &SharedChan, x: T) { - let mut xx = Some(x); - do self.with_imm |chan| { - let mut x = None; - x <-> xx; - chan.send(x.unwrap()) + fn send(&self, x: T) { + let mut xx = Some(x); + do self.with_imm |chan| { + let mut x = None; + x <-> xx; + chan.send(x.unwrap()) + } } } impl GenericSmartChan for SharedChan { - fn try_send(&self, x: T) -> bool { shared_chan_try_send(self, x) } -} - -#[inline(always)] -fn shared_chan_try_send(self: &SharedChan, x: T) -> bool { - let mut xx = Some(x); - do self.with_imm |chan| { - let mut x = None; - x <-> xx; - chan.try_send(x.unwrap()) + fn try_send(&self, x: T) -> bool { + let mut xx = Some(x); + do self.with_imm |chan| { + let mut x = None; + x <-> xx; + chan.try_send(x.unwrap()) + } } }