Box<dyn ReadWrite> for TlsConnector::connect

This commit is contained in:
Martin Algesten
2022-04-30 12:50:08 +02:00
parent 101467f13f
commit 65371c966c
6 changed files with 90 additions and 51 deletions

View File

@@ -11,12 +11,15 @@ pub(crate) fn default_tls_config() -> std::sync::Arc<dyn TlsConnector> {
}
impl TlsConnector for native_tls::TlsConnector {
fn connect(&self, dns_name: &str, tcp_stream: TcpStream) -> Result<Box<dyn ReadWrite>, Error> {
fn connect(&self, dns_name: &str, io: Box<dyn ReadWrite>) -> Result<Box<dyn ReadWrite>, Error> {
let stream =
native_tls::TlsConnector::connect(self, dns_name, tcp_stream).map_err(|e| {
ErrorKind::ConnectionFailed
native_tls::TlsConnector::connect(self, dns_name, io).map_err(|e| match e {
native_tls::HandshakeError::Failure(e) => ErrorKind::ConnectionFailed
.msg("native_tls connect failed")
.src(e)
.src(e),
native_tls::HandshakeError::WouldBlock(_) => {
ErrorKind::Io.msg("Unexpected native_tls::HandshakeError::WouldBlock")
}
})?;
Ok(Box::new(stream))
@@ -24,8 +27,11 @@ impl TlsConnector for native_tls::TlsConnector {
}
#[cfg(feature = "native-tls")]
impl ReadWrite for native_tls::TlsStream<TcpStream> {
impl ReadWrite for native_tls::TlsStream<Box<dyn ReadWrite>> {
fn socket(&self) -> Option<&TcpStream> {
Some(self.get_ref())
self.get_ref().socket()
}
fn is_poolable(&self) -> bool {
self.get_ref().is_poolable()
}
}