Skip to content

Commit

Permalink
Rename Connector to TcpConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
zu1k committed Dec 10, 2023
1 parent 3b5a805 commit 940e9f9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions examples/bind_connect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use socket2::{Domain, Socket, Type};
use std::net::SocketAddr;
use ureq::Connector;
use ureq::TcpConnector;

#[derive(Debug)]
pub(crate) struct BindConnector {
Expand All @@ -13,7 +13,7 @@ impl BindConnector {
}
}

impl Connector for BindConnector {
impl TcpConnector for BindConnector {
fn connect(&self, addr: &std::net::SocketAddr) -> std::io::Result<std::net::TcpStream> {
let socket = Socket::new(Domain::for_address(addr.to_owned()), Type::STREAM, None)?;
socket.bind(&self.bind_addr.into())?;
Expand All @@ -35,7 +35,7 @@ impl Connector for BindConnector {

pub fn main() {
let agent = ureq::builder()
.connector(BindConnector::new_bind("127.0.0.1:54321".parse().unwrap()))
.tcp_connector(BindConnector::new_bind("127.0.0.1:54321".parse().unwrap()))
.build();

let result = agent.get("http://127.0.0.1:8080/").call();
Expand Down
20 changes: 10 additions & 10 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use std::time::Duration;
use url::Url;

use crate::connect::{ArcConnector, StdTcpConnector};
use crate::connect::{ArcTcpConnector, StdTcpConnector};
use crate::middleware::Middleware;
use crate::pool::ConnectionPool;
use crate::proxy::Proxy;
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct AgentBuilder {
#[cfg(feature = "cookies")]
cookie_store: Option<CookieStore>,
resolver: ArcResolver,
connector: ArcConnector,
tcp_connector: ArcTcpConnector,
middleware: Vec<Box<dyn Middleware>>,
}

Expand Down Expand Up @@ -128,7 +128,7 @@ pub(crate) struct AgentState {
#[cfg(feature = "cookies")]
pub(crate) cookie_tin: CookieTin,
pub(crate) resolver: ArcResolver,
pub(crate) connector: ArcConnector,
pub(crate) tcp_connector: ArcTcpConnector,
pub(crate) middleware: Vec<Box<dyn Middleware>>,
}

Expand Down Expand Up @@ -274,7 +274,7 @@ impl AgentBuilder {
max_idle_connections: DEFAULT_MAX_IDLE_CONNECTIONS,
max_idle_connections_per_host: DEFAULT_MAX_IDLE_CONNECTIONS_PER_HOST,
resolver: StdResolver.into(),
connector: StdTcpConnector.into(),
tcp_connector: StdTcpConnector.into(),
#[cfg(feature = "cookies")]
cookie_store: None,
middleware: vec![],
Expand Down Expand Up @@ -302,7 +302,7 @@ impl AgentBuilder {
#[cfg(feature = "cookies")]
cookie_tin: CookieTin::new(self.cookie_store.unwrap_or_else(CookieStore::default)),
resolver: self.resolver,
connector: self.connector,
tcp_connector: self.tcp_connector,
middleware: self.middleware,
}),
}
Expand Down Expand Up @@ -407,13 +407,13 @@ impl AgentBuilder {
self
}

/// Configures a custom connector to be used by this agent. By default,
/// tcp-connect is done by std::net::TcpStream. This allows you
/// to override that connection with your own alternative.
/// Configures a custom TCP connector to be used by this agent.
/// By default, tcp-connect is done by std::net::TcpStream.
/// This allows you to override that connection with your own alternative.
///
/// See `examples/bind_connect.rs` for example.
pub fn connector(mut self, connector: impl crate::Connector + 'static) -> Self {
self.connector = connector.into();
pub fn tcp_connector(mut self, tcp_connector: impl crate::TcpConnector + 'static) -> Self {
self.tcp_connector = tcp_connector.into();
self
}

Expand Down
18 changes: 9 additions & 9 deletions src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use std::time::Duration;

/// A custom Connector to override the default TcpStream connector.
pub trait Connector: Send + Sync {
pub trait TcpConnector: Send + Sync {
fn connect(&self, addr: &SocketAddr) -> IoResult<TcpStream> {
TcpStream::connect(addr)
}
Expand All @@ -18,28 +18,28 @@ pub trait Connector: Send + Sync {
#[derive(Debug)]
pub(crate) struct StdTcpConnector;

impl Connector for StdTcpConnector {}
impl TcpConnector for StdTcpConnector {}

#[derive(Clone)]
pub(crate) struct ArcConnector(Arc<dyn Connector>);
pub(crate) struct ArcTcpConnector(Arc<dyn TcpConnector>);

impl<R> From<R> for ArcConnector
impl<R> From<R> for ArcTcpConnector
where
R: Connector + 'static,
R: TcpConnector + 'static,
{
fn from(r: R) -> Self {
Self(Arc::new(r))
}
}

impl fmt::Debug for ArcConnector {
impl fmt::Debug for ArcTcpConnector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ArcConnector(...)")
write!(f, "ArcTcpConnector(...)")
}
}

impl std::ops::Deref for ArcConnector {
type Target = dyn Connector;
impl std::ops::Deref for ArcTcpConnector {
type Target = dyn TcpConnector;

fn deref(&self) -> &Self::Target {
self.0.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ mod http_crate;
pub use crate::agent::Agent;
pub use crate::agent::AgentBuilder;
pub use crate::agent::RedirectAuthHeaders;
pub use crate::connect::Connector;
pub use crate::connect::TcpConnector;
pub use crate::error::{Error, ErrorKind, OrAnyStatus, Transport};
pub use crate::middleware::{Middleware, MiddlewareNext};
pub use crate::proxy::Proxy;
Expand Down
4 changes: 2 additions & 2 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ pub(crate) fn connect_host(
proto.unwrap(),
)
} else if let Some(timeout) = timeout {
unit.connector().connect_timeout(&sock_addr, timeout)
unit.tcp_connector().connect_timeout(&sock_addr, timeout)
} else {
unit.connector().connect(&sock_addr)
unit.tcp_connector().connect(&sock_addr)
};

if let Ok(stream) = stream {
Expand Down
6 changes: 3 additions & 3 deletions src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use cookie::Cookie;

use crate::agent::RedirectAuthHeaders;
use crate::body::{self, BodySize, Payload, SizedReader};
use crate::connect::ArcConnector;
use crate::connect::ArcTcpConnector;
use crate::error::{Error, ErrorKind};
use crate::header;
use crate::header::{get_header, Header};
Expand Down Expand Up @@ -116,8 +116,8 @@ impl Unit {
self.agent.state.resolver.clone()
}

pub fn connector(&self) -> ArcConnector {
self.agent.state.connector.clone()
pub fn tcp_connector(&self) -> ArcTcpConnector {
self.agent.state.tcp_connector.clone()
}

#[cfg(test)]
Expand Down

0 comments on commit 940e9f9

Please sign in to comment.