Skip to content

Commit 5f40dab

Browse files
committed
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.
1 parent 38f40c7 commit 5f40dab

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

benches/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct MockConnector;
7979

8080
impl net::NetworkConnector for MockConnector {
8181
type Stream = MockStream;
82-
fn connect(&mut self, _: &str, _: u16, _: &str) -> hyper::Result<MockStream> {
82+
fn connect(&self, _: &str, _: u16, _: &str) -> hyper::Result<MockStream> {
8383
Ok(MockStream::new())
8484
}
8585

@@ -90,7 +90,7 @@ fn bench_mock_hyper(b: &mut test::Bencher) {
9090
let url = "http://127.0.0.1:1337/";
9191
b.iter(|| {
9292
let mut req = hyper::client::Request::with_connector(
93-
hyper::Get, hyper::Url::parse(url).unwrap(), &mut MockConnector
93+
hyper::Get, hyper::Url::parse(url).unwrap(), &MockConnector
9494
).unwrap();
9595
req.headers_mut().set(Foo);
9696

src/client/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct ConnAdapter<C: NetworkConnector + Send>(C);
143143
impl<C: NetworkConnector<Stream=S> + Send, S: NetworkStream + Send> NetworkConnector for ConnAdapter<C> {
144144
type Stream = Box<NetworkStream + Send>;
145145
#[inline]
146-
fn connect(&mut self, host: &str, port: u16, scheme: &str)
146+
fn connect(&self, host: &str, port: u16, scheme: &str)
147147
-> ::Result<Box<NetworkStream + Send>> {
148148
Ok(try!(self.0.connect(host, port, scheme)).into())
149149
}
@@ -154,7 +154,7 @@ struct Connector(Box<NetworkConnector<Stream=Box<NetworkStream + Send>> + Send>)
154154
impl NetworkConnector for Connector {
155155
type Stream = Box<NetworkStream + Send>;
156156
#[inline]
157-
fn connect(&mut self, host: &str, port: u16, scheme: &str)
157+
fn connect(&self, host: &str, port: u16, scheme: &str)
158158
-> ::Result<Box<NetworkStream + Send>> {
159159
Ok(try!(self.0.connect(host, port, scheme)).into())
160160
}
@@ -165,7 +165,7 @@ impl NetworkConnector for Connector {
165165
/// One of these will be built for you if you use one of the convenience
166166
/// methods, such as `get()`, `post()`, etc.
167167
pub struct RequestBuilder<'a, U: IntoUrl> {
168-
client: &'a mut Client,
168+
client: &'a Client,
169169
url: U,
170170
headers: Option<Headers>,
171171
method: Method,
@@ -220,7 +220,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
220220
};
221221

222222
loop {
223-
let mut req = try!(Request::with_connector(method.clone(), url.clone(), &mut client.connector));
223+
let mut req = try!(Request::with_connector(method.clone(), url.clone(), &client.connector));
224224
headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter()));
225225

226226
match (can_have_body, body.as_ref()) {

src/client/pool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<S> PoolImpl<S> {
9898

9999
impl<C: NetworkConnector<Stream=S>, S: NetworkStream + Send> NetworkConnector for Pool<C> {
100100
type Stream = PooledStream<S>;
101-
fn connect(&mut self, host: &str, port: u16, scheme: &str) -> ::Result<PooledStream<S>> {
101+
fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<PooledStream<S>> {
102102
let key = key(host, port, scheme);
103103
let mut locked = self.inner.lock().unwrap();
104104
let mut should_remove = false;
@@ -197,7 +197,7 @@ mod tests {
197197

198198
#[test]
199199
fn test_connect_and_drop() {
200-
let mut pool = mocked!();
200+
let pool = mocked!();
201201
let key = key("127.0.0.1", 3000, "http");
202202
pool.connect("127.0.0.1", 3000, "http").unwrap().is_drained = true;
203203
{
@@ -215,7 +215,7 @@ mod tests {
215215

216216
#[test]
217217
fn test_closed() {
218-
let mut pool = mocked!();
218+
let pool = mocked!();
219219
let mut stream = pool.connect("127.0.0.1", 3000, "http").unwrap();
220220
stream.close(Shutdown::Both).unwrap();
221221
drop(stream);

src/client/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Request<Fresh> {
4848
}
4949

5050
/// Create a new client request with a specific underlying NetworkStream.
51-
pub fn with_connector<C, S>(method: method::Method, url: Url, connector: &mut C)
51+
pub fn with_connector<C, S>(method: method::Method, url: Url, connector: &C)
5252
-> ::Result<Request<Fresh>> where
5353
C: NetworkConnector<Stream=S>,
5454
S: Into<Box<NetworkStream + Send>> {

src/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct MockConnector;
7373
impl NetworkConnector for MockConnector {
7474
type Stream = MockStream;
7575

76-
fn connect(&mut self, _host: &str, _port: u16, _scheme: &str) -> ::Result<MockStream> {
76+
fn connect(&self, _host: &str, _port: u16, _scheme: &str) -> ::Result<MockStream> {
7777
Ok(MockStream::new())
7878
}
7979
}
@@ -88,7 +88,7 @@ macro_rules! mock_connector (
8888

8989
impl ::net::NetworkConnector for $name {
9090
type Stream = ::mock::MockStream;
91-
fn connect(&mut self, host: &str, port: u16, scheme: &str) -> $crate::Result<::mock::MockStream> {
91+
fn connect(&self, host: &str, port: u16, scheme: &str) -> $crate::Result<::mock::MockStream> {
9292
use std::collections::HashMap;
9393
use std::io::Cursor;
9494
debug!("MockStream::connect({:?}, {:?}, {:?})", host, port, scheme);

src/net.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub trait NetworkConnector {
6969
/// Type of Stream to create
7070
type Stream: Into<Box<NetworkStream + Send>>;
7171
/// Connect to a remote address.
72-
fn connect(&mut self, host: &str, port: u16, scheme: &str) -> ::Result<Self::Stream>;
72+
fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<Self::Stream>;
7373
}
7474

7575
impl<T: NetworkStream + Send> From<T> for Box<NetworkStream + Send> {
@@ -314,12 +314,12 @@ impl NetworkStream for HttpStream {
314314
pub struct HttpConnector(pub Option<ContextVerifier>);
315315

316316
/// A method that can set verification methods on an SSL context
317-
pub type ContextVerifier = Box<FnMut(&mut SslContext) -> () + Send>;
317+
pub type ContextVerifier = Box<Fn(&mut SslContext) -> () + Send>;
318318

319319
impl NetworkConnector for HttpConnector {
320320
type Stream = HttpStream;
321321

322-
fn connect(&mut self, host: &str, port: u16, scheme: &str) -> ::Result<HttpStream> {
322+
fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<HttpStream> {
323323
let addr = &(host, port);
324324
Ok(try!(match scheme {
325325
"http" => {
@@ -330,7 +330,7 @@ impl NetworkConnector for HttpConnector {
330330
debug!("https scheme");
331331
let stream = CloneTcpStream(try!(TcpStream::connect(addr)));
332332
let mut context = try!(SslContext::new(Sslv23));
333-
if let Some(ref mut verifier) = self.0 {
333+
if let Some(ref verifier) = self.0 {
334334
verifier(&mut context);
335335
}
336336
let ssl = try!(Ssl::new(&context));

0 commit comments

Comments
 (0)