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:
Jonathan Reem
2014-09-09 16:51:32 -07:00
parent 8026867334
commit ed491655dd
5 changed files with 31 additions and 15 deletions

View File

@@ -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> {