Skip to content

Commit c3eb828

Browse files
committed
Merge pull request #516 from hyperium/demut-connector
feat(net): remove mut requirement for NetworkConnector.connect()
2 parents 7bc4e83 + 1b31872 commit c3eb828

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

benches/client.rs

+2-2
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

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

src/client/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ struct ConnAdapter<C: NetworkConnector + Send>(C);
140140
impl<C: NetworkConnector<Stream=S> + Send, S: NetworkStream + Send> NetworkConnector for ConnAdapter<C> {
141141
type Stream = Box<NetworkStream + Send>;
142142
#[inline]
143-
fn connect(&mut self, host: &str, port: u16, scheme: &str)
143+
fn connect(&self, host: &str, port: u16, scheme: &str)
144144
-> ::Result<Box<NetworkStream + Send>> {
145145
Ok(try!(self.0.connect(host, port, scheme)).into())
146146
}
@@ -155,7 +155,7 @@ struct Connector(Box<NetworkConnector<Stream=Box<NetworkStream + Send>> + Send>)
155155
impl NetworkConnector for Connector {
156156
type Stream = Box<NetworkStream + Send>;
157157
#[inline]
158-
fn connect(&mut self, host: &str, port: u16, scheme: &str)
158+
fn connect(&self, host: &str, port: u16, scheme: &str)
159159
-> ::Result<Box<NetworkStream + Send>> {
160160
Ok(try!(self.0.connect(host, port, scheme)).into())
161161
}
@@ -170,7 +170,7 @@ impl NetworkConnector for Connector {
170170
/// One of these will be built for you if you use one of the convenience
171171
/// methods, such as `get()`, `post()`, etc.
172172
pub struct RequestBuilder<'a, U: IntoUrl> {
173-
client: &'a mut Client,
173+
client: &'a Client,
174174
url: U,
175175
headers: Option<Headers>,
176176
method: Method,
@@ -225,7 +225,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
225225
};
226226

227227
loop {
228-
let mut req = try!(Request::with_connector(method.clone(), url.clone(), &mut client.connector));
228+
let mut req = try!(Request::with_connector(method.clone(), url.clone(), &client.connector));
229229
headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter()));
230230

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

src/client/pool.rs

+3-3
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;
@@ -203,7 +203,7 @@ mod tests {
203203

204204
#[test]
205205
fn test_connect_and_drop() {
206-
let mut pool = mocked!();
206+
let pool = mocked!();
207207
let key = key("127.0.0.1", 3000, "http");
208208
pool.connect("127.0.0.1", 3000, "http").unwrap().is_drained = true;
209209
{
@@ -221,7 +221,7 @@ mod tests {
221221

222222
#[test]
223223
fn test_closed() {
224-
let mut pool = mocked!();
224+
let pool = mocked!();
225225
let mut stream = pool.connect("127.0.0.1", 3000, "http").unwrap();
226226
stream.close(Shutdown::Both).unwrap();
227227
drop(stream);

src/client/request.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Request<Fresh> {
4747
}
4848

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

src/mock.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct MockConnector;
7474
impl NetworkConnector for MockConnector {
7575
type Stream = MockStream;
7676

77-
fn connect(&mut self, _host: &str, _port: u16, _scheme: &str) -> ::Result<MockStream> {
77+
fn connect(&self, _host: &str, _port: u16, _scheme: &str) -> ::Result<MockStream> {
7878
Ok(MockStream::new())
7979
}
8080

@@ -122,7 +122,7 @@ macro_rules! mock_connector (
122122

123123
impl ::net::NetworkConnector for $name {
124124
type Stream = ::mock::MockStream;
125-
fn connect(&mut self, host: &str, port: u16, scheme: &str) -> $crate::Result<::mock::MockStream> {
125+
fn connect(&self, host: &str, port: u16, scheme: &str) -> $crate::Result<::mock::MockStream> {
126126
use std::collections::HashMap;
127127
use std::io::Cursor;
128128
debug!("MockStream::connect({:?}, {:?}, {:?})", host, port, scheme);

src/net.rs

+4-4
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
/// Sets the given `ContextVerifier` to be used when verifying the SSL context
7474
/// on the establishment of a new connection.
7575
fn set_ssl_verifier(&mut self, verifier: ContextVerifier);
@@ -317,12 +317,12 @@ impl NetworkStream for HttpStream {
317317
pub struct HttpConnector(pub Option<ContextVerifier>);
318318

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

322322
impl NetworkConnector for HttpConnector {
323323
type Stream = HttpStream;
324324

325-
fn connect(&mut self, host: &str, port: u16, scheme: &str) -> ::Result<HttpStream> {
325+
fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<HttpStream> {
326326
let addr = &(host, port);
327327
Ok(try!(match scheme {
328328
"http" => {
@@ -333,7 +333,7 @@ impl NetworkConnector for HttpConnector {
333333
debug!("https scheme");
334334
let stream = CloneTcpStream(try!(TcpStream::connect(addr)));
335335
let mut context = try!(SslContext::new(Sslv23));
336-
if let Some(ref mut verifier) = self.0 {
336+
if let Some(ref verifier) = self.0 {
337337
verifier(&mut context);
338338
}
339339
let ssl = try!(Ssl::new(&context));

0 commit comments

Comments
 (0)