From 1b481017acfc51063d6ab69de6e310634ceb6804 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 25 Nov 2012 14:09:53 -0800 Subject: [PATCH] Minor cleanups to pipes and serialization --- src/libcore/pipes.rs | 48 +++++++++++++++++++++++++++---------------- src/libstd/comm.rs | 13 ++++++++---- src/libstd/ebml.rs | 5 ++--- src/libstd/net_tcp.rs | 4 ++-- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/libcore/pipes.rs b/src/libcore/pipes.rs index ec204ca6736..48b0b8d4d2b 100644 --- a/src/libcore/pipes.rs +++ b/src/libcore/pipes.rs @@ -527,7 +527,7 @@ pub pure fn peek(p: &RecvPacketBuffered) -> bool { } } -impl RecvPacketBuffered { +impl RecvPacketBuffered: Peekable { pure fn peek() -> bool { peek(&self) } @@ -928,32 +928,32 @@ proto! streamp ( ) /// A trait for things that can send multiple messages. -pub trait Channel { - // It'd be nice to call this send, but it'd conflict with the - // built in send kind. - +pub trait GenericChan { /// Sends a message. fn send(x: T); +} +/// Things that can send multiple messages and can detect when the receiver +/// is closed +pub trait GenericSmartChan { /// Sends a message, or report if the receiver has closed the connection. fn try_send(x: T) -> bool; } /// A trait for things that can receive multiple messages. -pub trait Recv { +pub trait GenericPort { /// Receives a message, or fails if the connection closes. fn recv() -> T; - /** Receives a message if one is available, or returns `none` if - the connection is closed. - + /** Receives a message, or returns `none` if + the connection is closed or closes. */ fn try_recv() -> Option; +} - /** Returns true if a message is available or the connection is - closed. - - */ +/// Ports that can `peek` +pub trait Peekable { + /// Returns true if a message is available pure fn peek() -> bool; } @@ -984,13 +984,16 @@ pub fn stream() -> (Chan, Port) { (Chan_({ mut endp: Some(move c) }), Port_({ mut endp: Some(move s) })) } -impl Chan: Channel { +impl Chan: GenericChan { fn send(x: T) { let mut endp = None; endp <-> self.endp; self.endp = Some( streamp::client::data(unwrap(move endp), move x)) } +} + +impl Chan: GenericSmartChan { fn try_send(x: T) -> bool { let mut endp = None; @@ -1005,7 +1008,7 @@ impl Chan: Channel { } } -impl Port: Recv { +impl Port: GenericPort { fn recv() -> T { let mut endp = None; endp <-> self.endp; @@ -1025,7 +1028,9 @@ impl Port: Recv { None => None } } +} +impl Port: Peekable { pure fn peek() -> bool unsafe { let mut endp = None; endp <-> self.endp; @@ -1071,7 +1076,7 @@ impl PortSet { } } -impl PortSet : Recv { +impl PortSet : GenericPort { fn try_recv() -> Option { let mut result = None; @@ -1099,6 +1104,9 @@ impl PortSet : Recv { option::unwrap_expect(self.try_recv(), "port_set: endpoints closed") } +} + +impl PortSet : Peekable { pure fn peek() -> bool { // It'd be nice to use self.port.each, but that version isn't // pure. @@ -1112,7 +1120,7 @@ impl PortSet : Recv { /// A channel that can be shared between many senders. pub type SharedChan = private::Exclusive>; -impl SharedChan: Channel { +impl SharedChan: GenericChan { fn send(x: T) { let mut xx = Some(move x); do self.with_imm |chan| { @@ -1121,7 +1129,9 @@ impl SharedChan: Channel { chan.send(option::unwrap(move x)) } } +} +impl SharedChan: GenericSmartChan { fn try_send(x: T) -> bool { let mut xx = Some(move x); do self.with_imm |chan| { @@ -1145,7 +1155,9 @@ pub trait Select2 { fn select() -> Either; } -impl, Right: Selectable Recv> +impl, + Right: Selectable GenericPort> (Left, Right): Select2 { fn select() -> Either { diff --git a/src/libstd/comm.rs b/src/libstd/comm.rs index f8b24389d3c..d8c5b6e5944 100644 --- a/src/libstd/comm.rs +++ b/src/libstd/comm.rs @@ -17,25 +17,28 @@ Higher level communication abstractions. // NB: transitionary, de-mode-ing. #[forbid(deprecated_mode)]; -use pipes::{Channel, Recv, Chan, Port, Selectable}; +use pipes::{GenericChan, GenericSmartChan, GenericPort, + Chan, Port, Selectable, Peekable}; /// An extension of `pipes::stream` that allows both sending and receiving. pub struct DuplexStream { priv chan: Chan, - priv port: Port , + priv port: Port, } -impl DuplexStream : Channel { +impl DuplexStream : GenericChan { fn send(x: T) { self.chan.send(move x) } +} +impl DuplexStream : GenericSmartChan { fn try_send(x: T) -> bool { self.chan.try_send(move x) } } -impl DuplexStream : Recv { +impl DuplexStream : GenericPort { fn recv() -> U { self.port.recv() } @@ -43,7 +46,9 @@ impl DuplexStream : Recv { fn try_recv() -> Option { self.port.try_recv() } +} +impl DuplexStream : Peekable { pure fn peek() -> bool { self.port.peek() } diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index d439eef7cce..1c6384f87d4 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -157,7 +157,6 @@ pub mod reader { pub fn doc_data(d: Doc) -> ~[u8] { vec::slice::(*d.data, d.start, d.end) } - pub fn with_doc_data(d: Doc, f: fn(x: &[u8]) -> T) -> T { f(vec::view(*d.data, d.start, d.end)) } @@ -190,7 +189,7 @@ pub mod reader { pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 } - struct Deserializer { + pub struct Deserializer { priv mut parent: Doc, priv mut pos: uint, } @@ -388,7 +387,7 @@ pub mod reader { pub mod writer { // ebml writing - struct Serializer { + pub struct Serializer { writer: io::Writer, priv mut size_positions: ~[uint], } diff --git a/src/libstd/net_tcp.rs b/src/libstd/net_tcp.rs index 4ae2601ebe7..624824dede4 100644 --- a/src/libstd/net_tcp.rs +++ b/src/libstd/net_tcp.rs @@ -35,7 +35,7 @@ extern mod rustrt { * underlying libuv data structures when it goes out of scope. This is the * data structure that is used for read/write operations over a TCP stream. */ -struct TcpSocket { +pub struct TcpSocket { socket_data: @TcpSocketData, } @@ -59,7 +59,7 @@ pub fn TcpSocket(socket_data: @TcpSocketData) -> TcpSocket { * It is created with a call to `net::tcp::socket_buf()` and has impls that * satisfy both the `io::reader` and `io::writer` traits. */ -struct TcpSocketBuf { +pub struct TcpSocketBuf { data: @TcpBufferedSocketData, mut end_of_stream: bool, }