From 1b318724a5fd425366daddf15c5964d7c3cbc240 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 8 May 2015 20:54:05 -0700 Subject: [PATCH] feat(net): remove mut requirement for NetworkConnector.connect() BREAKING CHANGE: Any custom Connectors will need to change to &self in the connect method. Any Connectors that needed the mutablity need to figure out a synchronization strategy. Request::with_connector() takes a &NetworkConnector instead of &mut. Any uses of with_connector will need to change to passing &C. --- benches/client.rs | 4 ++-- src/client/mod.rs | 8 ++++---- src/client/pool.rs | 6 +++--- src/client/request.rs | 2 +- src/mock.rs | 4 ++-- src/net.rs | 8 ++++---- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/benches/client.rs b/benches/client.rs index 9e26d39b3e..984ce13191 100644 --- a/benches/client.rs +++ b/benches/client.rs @@ -79,7 +79,7 @@ struct MockConnector; impl net::NetworkConnector for MockConnector { type Stream = MockStream; - fn connect(&mut self, _: &str, _: u16, _: &str) -> hyper::Result { + fn connect(&self, _: &str, _: u16, _: &str) -> hyper::Result { Ok(MockStream::new()) } @@ -93,7 +93,7 @@ fn bench_mock_hyper(b: &mut test::Bencher) { let url = "http://127.0.0.1:1337/"; b.iter(|| { let mut req = hyper::client::Request::with_connector( - hyper::Get, hyper::Url::parse(url).unwrap(), &mut MockConnector + hyper::Get, hyper::Url::parse(url).unwrap(), &MockConnector ).unwrap(); req.headers_mut().set(Foo); diff --git a/src/client/mod.rs b/src/client/mod.rs index 57aae50826..fb95f305b8 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -140,7 +140,7 @@ struct ConnAdapter(C); impl + Send, S: NetworkStream + Send> NetworkConnector for ConnAdapter { type Stream = Box; #[inline] - fn connect(&mut self, host: &str, port: u16, scheme: &str) + fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result> { Ok(try!(self.0.connect(host, port, scheme)).into()) } @@ -155,7 +155,7 @@ struct Connector(Box> + Send>) impl NetworkConnector for Connector { type Stream = Box; #[inline] - fn connect(&mut self, host: &str, port: u16, scheme: &str) + fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result> { Ok(try!(self.0.connect(host, port, scheme)).into()) } @@ -170,7 +170,7 @@ impl NetworkConnector for Connector { /// One of these will be built for you if you use one of the convenience /// methods, such as `get()`, `post()`, etc. pub struct RequestBuilder<'a, U: IntoUrl> { - client: &'a mut Client, + client: &'a Client, url: U, headers: Option, method: Method, @@ -225,7 +225,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> { }; loop { - let mut req = try!(Request::with_connector(method.clone(), url.clone(), &mut client.connector)); + let mut req = try!(Request::with_connector(method.clone(), url.clone(), &client.connector)); headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter())); match (can_have_body, body.as_ref()) { diff --git a/src/client/pool.rs b/src/client/pool.rs index 89f9fd8711..45b3bb1a42 100644 --- a/src/client/pool.rs +++ b/src/client/pool.rs @@ -98,7 +98,7 @@ impl PoolImpl { impl, S: NetworkStream + Send> NetworkConnector for Pool { type Stream = PooledStream; - fn connect(&mut self, host: &str, port: u16, scheme: &str) -> ::Result> { + fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result> { let key = key(host, port, scheme); let mut locked = self.inner.lock().unwrap(); let mut should_remove = false; @@ -203,7 +203,7 @@ mod tests { #[test] fn test_connect_and_drop() { - let mut pool = mocked!(); + let pool = mocked!(); let key = key("127.0.0.1", 3000, "http"); pool.connect("127.0.0.1", 3000, "http").unwrap().is_drained = true; { @@ -221,7 +221,7 @@ mod tests { #[test] fn test_closed() { - let mut pool = mocked!(); + let pool = mocked!(); let mut stream = pool.connect("127.0.0.1", 3000, "http").unwrap(); stream.close(Shutdown::Both).unwrap(); drop(stream); diff --git a/src/client/request.rs b/src/client/request.rs index 9de706e496..d402af01ff 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -47,7 +47,7 @@ impl Request { } /// Create a new client request with a specific underlying NetworkStream. - pub fn with_connector(method: method::Method, url: Url, connector: &mut C) + pub fn with_connector(method: method::Method, url: Url, connector: &C) -> ::Result> where C: NetworkConnector, S: Into> { diff --git a/src/mock.rs b/src/mock.rs index 817aa22d14..470a8bb8e3 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -74,7 +74,7 @@ pub struct MockConnector; impl NetworkConnector for MockConnector { type Stream = MockStream; - fn connect(&mut self, _host: &str, _port: u16, _scheme: &str) -> ::Result { + fn connect(&self, _host: &str, _port: u16, _scheme: &str) -> ::Result { Ok(MockStream::new()) } @@ -122,7 +122,7 @@ macro_rules! mock_connector ( impl ::net::NetworkConnector for $name { type Stream = ::mock::MockStream; - fn connect(&mut self, host: &str, port: u16, scheme: &str) -> $crate::Result<::mock::MockStream> { + fn connect(&self, host: &str, port: u16, scheme: &str) -> $crate::Result<::mock::MockStream> { use std::collections::HashMap; use std::io::Cursor; debug!("MockStream::connect({:?}, {:?}, {:?})", host, port, scheme); diff --git a/src/net.rs b/src/net.rs index 792d011370..0e3580d0bb 100644 --- a/src/net.rs +++ b/src/net.rs @@ -69,7 +69,7 @@ pub trait NetworkConnector { /// Type of Stream to create type Stream: Into>; /// Connect to a remote address. - fn connect(&mut self, host: &str, port: u16, scheme: &str) -> ::Result; + fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result; /// Sets the given `ContextVerifier` to be used when verifying the SSL context /// on the establishment of a new connection. fn set_ssl_verifier(&mut self, verifier: ContextVerifier); @@ -317,12 +317,12 @@ impl NetworkStream for HttpStream { pub struct HttpConnector(pub Option); /// A method that can set verification methods on an SSL context -pub type ContextVerifier = Box () + Send>; +pub type ContextVerifier = Box () + Send>; impl NetworkConnector for HttpConnector { type Stream = HttpStream; - fn connect(&mut self, host: &str, port: u16, scheme: &str) -> ::Result { + fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result { let addr = &(host, port); Ok(try!(match scheme { "http" => { @@ -333,7 +333,7 @@ impl NetworkConnector for HttpConnector { debug!("https scheme"); let stream = CloneTcpStream(try!(TcpStream::connect(addr))); let mut context = try!(SslContext::new(Sslv23)); - if let Some(ref mut verifier) = self.0 { + if let Some(ref verifier) = self.0 { verifier(&mut context); } let ssl = try!(Ssl::new(&context));