Skip to content

rustup for clone trait #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,29 @@ pub trait Header: Any + Send + Sync {
/// A trait for any object that will represent a header field and value.
///
/// This trait represents the formatting of a Header for output to a TcpStream.
pub trait HeaderFormat: Clone + Any + Send + Sync {
pub trait HeaderFormat: HeaderClone + Any + Send + Sync {
/// Format a header to be output into a TcpStream.
///
/// This method is not allowed to introduce an Err not produced
/// by the passed-in Formatter.
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result;

#[doc(hidden)]
}

#[doc(hidden)]
pub trait HeaderClone {
fn clone_box(&self) -> Box<HeaderFormat + Sync + Send>;
}

impl<T: HeaderFormat + Send + Sync + Clone> HeaderClone for T {
#[inline]
fn clone_box(&self) -> Box<HeaderFormat + Sync + Send> { box self.clone() }
fn clone_box(&self) -> Box<HeaderFormat + Sync + Send> {
box self.clone()
}
}

impl HeaderFormat {
#[inline]
fn is<T: 'static>(&self) -> bool {
self.get_type_id() == TypeId::of::<T>()
}
Expand All @@ -85,6 +95,7 @@ impl<'a> UncheckedAnyMutDowncast<'a> for &'a mut HeaderFormat {
}

impl Clone for Box<HeaderFormat + Send + Sync> {
#[inline]
fn clone(&self) -> Box<HeaderFormat + Send + Sync> {
self.clone_box()
}
Expand Down
14 changes: 11 additions & 3 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ pub trait NetworkAcceptor<S: NetworkStream>: Acceptor<S> + Clone + Send {
}

/// An abstraction over streams that a Server can utilize.
pub trait NetworkStream: Stream + Any + Clone + Send {
pub trait NetworkStream: Stream + Any + StreamClone + Send {
/// Get the remote address of the underlying connection.
fn peer_name(&mut self) -> IoResult<SocketAddr>;
}

#[doc(hidden)]
pub trait StreamClone {
fn clone_box(&self) -> Box<NetworkStream + Send>;
}

#[doc(hidden)]
impl<T: NetworkStream + Send + Clone> StreamClone for T {
#[inline]
fn clone_box(&self) -> Box<NetworkStream + Send> { box self.clone() }
fn clone_box(&self) -> Box<NetworkStream + Send> {
box self.clone()
}
}

/// A connector creates a NetworkStream.
Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<L: NetworkListener<S, A>, S: NetworkStream, A: NetworkAcceptor<S>> Server<L
/// something other than the provided HttpStream, HttpAcceptor, and HttpListener.
pub fn listen_network<H, S, A, L>(self, handler: H, threads: uint) -> HttpResult<Listening<A>>
where H: Handler,
S: NetworkStream,
S: NetworkStream + Clone,
A: NetworkAcceptor<S>,
L: NetworkListener<S, A>, {
debug!("binding to {}:{}", self.ip, self.port);
Expand Down