chore(all): Move downcasting to a stable implementation.
This commit is contained in:
		| @@ -22,6 +22,8 @@ rustc-serialize = "*" | ||||
| time = "*" | ||||
| unicase = "*" | ||||
| url = "*" | ||||
| traitobject = "*" | ||||
| typeable = "*" | ||||
|  | ||||
| [dev-dependencies] | ||||
| env_logger = "*" | ||||
|   | ||||
| @@ -6,14 +6,12 @@ | ||||
| //! are already provided, such as `Host`, `ContentType`, `UserAgent`, and others. | ||||
| use std::any::Any; | ||||
| use std::borrow::{Cow, ToOwned}; | ||||
| use std::fmt; | ||||
| use std::collections::HashMap; | ||||
| use std::collections::hash_map::{Iter, Entry}; | ||||
| use std::iter::{FromIterator, IntoIterator}; | ||||
| use std::raw::TraitObject; | ||||
| use std::{mem, raw}; | ||||
| use std::{mem, fmt}; | ||||
|  | ||||
| use httparse; | ||||
| use {httparse, traitobject}; | ||||
| use unicase::UniCase; | ||||
|  | ||||
| use self::internals::Item; | ||||
| @@ -76,12 +74,12 @@ impl<T: HeaderFormat + Send + Sync + Clone> HeaderClone for T { | ||||
| impl HeaderFormat + Send + Sync { | ||||
|     #[inline] | ||||
|     unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T { | ||||
|         mem::transmute(mem::transmute::<&HeaderFormat, raw::TraitObject>(self).data) | ||||
|         mem::transmute(traitobject::data(self)) | ||||
|     } | ||||
|  | ||||
|     #[inline] | ||||
|     unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T { | ||||
|         mem::transmute(mem::transmute::<&mut HeaderFormat, raw::TraitObject>(self).data) | ||||
|         mem::transmute(traitobject::data_mut(self)) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -5,6 +5,7 @@ | ||||
| #![cfg_attr(test, feature(test))] | ||||
|  | ||||
| //! # Hyper | ||||
| //! | ||||
| //! Hyper is a fast, modern HTTP implementation written in and for Rust. It | ||||
| //! is a low-level typesafe abstraction over raw HTTP, providing an elegant | ||||
| //! layer over "stringly-typed" HTTP. | ||||
| @@ -134,6 +135,8 @@ extern crate cookie; | ||||
| extern crate unicase; | ||||
| extern crate httparse; | ||||
| extern crate num_cpus; | ||||
| extern crate traitobject; | ||||
| extern crate typeable; | ||||
|  | ||||
| #[macro_use] | ||||
| extern crate log; | ||||
|   | ||||
							
								
								
									
										22
									
								
								src/net.rs
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								src/net.rs
									
									
									
									
									
								
							| @@ -5,7 +5,6 @@ use std::io::{self, Read, Write}; | ||||
| use std::net::{SocketAddr, ToSocketAddrs, TcpStream, TcpListener}; | ||||
| use std::mem; | ||||
| use std::path::Path; | ||||
| use std::raw::{self, TraitObject}; | ||||
| use std::sync::Arc; | ||||
| use std::marker::Reflect; | ||||
|  | ||||
| @@ -15,6 +14,9 @@ use openssl::ssl::SslMethod::Sslv23; | ||||
| use openssl::ssl::error::{SslError, StreamError, OpenSslErrors, SslSessionClosed}; | ||||
| use openssl::x509::X509FileType; | ||||
|  | ||||
| use typeable::Typeable; | ||||
| use {traitobject}; | ||||
|  | ||||
| macro_rules! try_some { | ||||
|     ($expr:expr) => (match $expr { | ||||
|         Some(val) => { return Err(val); }, | ||||
| @@ -62,7 +64,7 @@ impl<'a, N: NetworkListener + 'a> Iterator for NetworkConnections<'a, N> { | ||||
|  | ||||
|  | ||||
| /// An abstraction over streams that a Server can utilize. | ||||
| pub trait NetworkStream: Read + Write + Any + StreamClone + Send { | ||||
| pub trait NetworkStream: Read + Write + Any + StreamClone + Send + Typeable { | ||||
|     /// Get the remote address of the underlying connection. | ||||
|     fn peer_addr(&mut self) -> io::Result<SocketAddr>; | ||||
| } | ||||
| @@ -100,31 +102,29 @@ impl Clone for Box<NetworkStream + Send> { | ||||
|  | ||||
| impl NetworkStream + Send { | ||||
|     unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T { | ||||
|         mem::transmute(mem::transmute::<&NetworkStream, | ||||
|                                         raw::TraitObject>(self).data) | ||||
|         mem::transmute(traitobject::data(self)) | ||||
|     } | ||||
|  | ||||
|     unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T { | ||||
|         mem::transmute(mem::transmute::<&mut NetworkStream, | ||||
|                                         raw::TraitObject>(self).data) | ||||
|         mem::transmute(traitobject::data_mut(self)) | ||||
|     } | ||||
|  | ||||
|     unsafe fn downcast_unchecked<T: 'static>(self: Box<NetworkStream + Send>) -> Box<T>  { | ||||
|         mem::transmute(mem::transmute::<Box<NetworkStream + Send>, | ||||
|                                         raw::TraitObject>(self).data) | ||||
|         let raw: *mut NetworkStream = mem::transmute(self); | ||||
|         mem::transmute(traitobject::data_mut(raw)) | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl NetworkStream + Send { | ||||
|     /// Is the underlying type in this trait object a T? | ||||
|     #[inline] | ||||
|     pub fn is<T: Reflect + 'static>(&self) -> bool { | ||||
|         self.get_type_id() == TypeId::of::<T>() | ||||
|     pub fn is<T: Any>(&self) -> bool { | ||||
|         (*self).get_type() == TypeId::of::<T>() | ||||
|     } | ||||
|  | ||||
|     /// If the underlying type is T, get a reference to the contained data. | ||||
|     #[inline] | ||||
|     pub fn downcast_ref<T: Reflect + 'static>(&self) -> Option<&T> { | ||||
|     pub fn downcast_ref<T: Any>(&self) -> Option<&T> { | ||||
|         if self.is::<T>() { | ||||
|             Some(unsafe { self.downcast_ref_unchecked() }) | ||||
|         } else { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user