Skip to content

Commit

Permalink
Make downcasting inherent methods on NetworkStream.
Browse files Browse the repository at this point in the history
  • Loading branch information
reem committed Jan 6, 2015
1 parent f0fe878 commit 66aa08e
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/net.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! A collection of traits abstracting over Listeners and Streams.
use std::any::{Any, AnyRefExt};
use std::boxed::BoxAny;
use std::any::Any;
use std::fmt;
use std::intrinsics::TypeId;
use std::io::{IoResult, IoError, ConnectionAborted, InvalidInput, OtherIoError,
Stream, Listener, Acceptor};
use std::io::net::ip::{SocketAddr, ToSocketAddr, Port};
use std::io::net::tcp::{TcpStream, TcpListener, TcpAcceptor};
use std::mem::{self, transmute, transmute_copy};
use std::mem;
use std::raw::{self, TraitObject};

use uany::UnsafeAnyExt;
Expand Down Expand Up @@ -105,46 +104,54 @@ impl<'a> Writer for &'a mut NetworkStream {
fn flush(&mut self) -> IoResult<()> { (**self).flush() }
}

impl UnsafeAnyExt for NetworkStream + Send {
impl UnsafeAnyExt for NetworkStream {
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
mem::transmute(mem::transmute::<&(NetworkStream + Send),
mem::transmute(mem::transmute::<&NetworkStream,
raw::TraitObject>(self).data)
}

unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
mem::transmute(mem::transmute::<&mut (NetworkStream + Send),
mem::transmute(mem::transmute::<&mut NetworkStream,
raw::TraitObject>(self).data)
}

unsafe fn downcast_unchecked<T: 'static>(self: Box<NetworkStream + Send>) -> Box<T> {
mem::transmute(mem::transmute::<Box<NetworkStream + Send>,
unsafe fn downcast_unchecked<T: 'static>(self: Box<NetworkStream>) -> Box<T> {
mem::transmute(mem::transmute::<Box<NetworkStream>,
raw::TraitObject>(self).data)
}
}

impl<'a> AnyRefExt<'a> for &'a (NetworkStream + 'static) {
impl NetworkStream {
/// Is the underlying type in this trait object a T?
#[inline]
fn is<T: 'static>(self) -> bool {
pub fn is<T: 'static>(&self) -> bool {
self.get_type_id() == TypeId::of::<T>()
}

/// If the underlying type is T, get a reference to the contained data.
#[inline]
fn downcast_ref<T: 'static>(self) -> Option<&'a T> {
pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let to: TraitObject = transmute_copy(&self);
// Extract the data pointer
Some(transmute(to.data))
}
Some(unsafe { self.downcast_ref_unchecked() })
} else {
None
}
}

/// If the underlying type is T, get a mutable reference to the contained
/// data.
#[inline]
pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
Some(unsafe { self.downcast_mut_unchecked() })
} else {
None
}
}
}

impl BoxAny for Box<NetworkStream + Send> {
fn downcast<T: 'static>(self) -> Result<Box<T>, Box<NetworkStream + Send>> {
/// If the underlying type is T, extract it.
pub fn downcast<T: 'static>(self: Box<NetworkStream>)
-> Result<Box<T>, Box<NetworkStream>> {
if self.is::<T>() {
Ok(unsafe { self.downcast_unchecked() })
} else {
Expand Down Expand Up @@ -310,7 +317,7 @@ mod tests {
fn test_downcast_box_stream() {
let stream = box MockStream::new() as Box<NetworkStream + Send>;

let mock = stream.downcast::<MockStream>().unwrap();
let mock = stream.downcast::<MockStream>().ok().unwrap();
assert_eq!(mock, box MockStream::new());

}
Expand Down

0 comments on commit 66aa08e

Please sign in to comment.