Use dynamic dispatch for client Request and Response through Box<NetworkStream>
Also adds a convenience `abstract` method to NetworkStream for creating Box<NetworkStream + Send> from a NetworkStream.
This commit is contained in:
16
src/net.rs
16
src/net.rs
@@ -28,6 +28,22 @@ pub trait NetworkStream: Stream + Clone + Send {
|
||||
|
||||
/// Connect to a remote address.
|
||||
fn connect(host: &str, port: Port) -> IoResult<Self>;
|
||||
|
||||
/// Turn this into an appropriately typed trait object.
|
||||
#[inline]
|
||||
fn abstract(self) -> Box<NetworkStream + Send> {
|
||||
box self as Box<NetworkStream + Send>
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
// Hack to work around lack of Clone impl for Box<Clone>
|
||||
fn clone_box(&self) -> Box<NetworkStream + Send> { self.clone().abstract() }
|
||||
}
|
||||
|
||||
impl Clone for Box<NetworkStream + Send> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Box<NetworkStream + Send> { self.clone_box() }
|
||||
}
|
||||
|
||||
impl Reader for Box<NetworkStream + Send> {
|
||||
|
||||
Reference in New Issue
Block a user