Skip to content

Commit abdd4c5

Browse files
author
Danyel Bayraktar
committed
feat(net): Implement NetworkConnector for closure to be more flexible
1 parent 421422b commit abdd4c5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/net.rs

+28
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,34 @@ impl NetworkConnector for HttpConnector {
255255
}
256256
}
257257

258+
/// A closure as a connector used to generate TcpStreams per request
259+
///
260+
/// # Example
261+
///
262+
/// Basic example:
263+
///
264+
/// ```norun
265+
/// Client::with_connector(|addr: &str, port: u16, scheme: &str| {
266+
/// TcpStream::connect(&(addr, port))
267+
/// });
268+
/// ```
269+
///
270+
/// Example using TcpBuilder from the net2 crate if you want to configure your source socket:
271+
///
272+
/// ```norun
273+
/// Client::with_connector(|addr: &str, port: u16, scheme: &str| {
274+
/// let b = try!(TcpBuilder::new_v4());
275+
/// try!(b.bind("127.0.0.1:0"));
276+
/// b.connect(&(addr, port))
277+
/// });
278+
/// ```
279+
impl<F> NetworkConnector for F where F: Fn(&str, u16, &str) -> io::Result<TcpStream> {
280+
type Stream = HttpStream;
281+
282+
fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<HttpStream> {
283+
Ok(HttpStream(try!((*self)(host, port, scheme))))
284+
}
285+
}
258286

259287
/// An abstraction to allow any SSL implementation to be used with HttpsStreams.
260288
pub trait Ssl {

0 commit comments

Comments
 (0)