From d7958b7c330f4ab771b8d19d660974c816f2209f Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 17 Sep 2025 09:31:20 +0200 Subject: [PATCH 1/4] server: make StartHandshake fields public --- src/server.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/server.rs b/src/server.rs index 3ebb6a85..d13d878f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -195,10 +195,11 @@ where /// [`ClientHello`](rustls::server::ClientHello) data, /// and all the state required to continue the TLS handshake (e.g. via /// [`StartHandshake::into_stream`]). +#[non_exhaustive] #[derive(Debug)] pub struct StartHandshake { - accepted: rustls::server::Accepted, - io: IO, + pub accepted: rustls::server::Accepted, + pub io: IO, } impl StartHandshake @@ -213,11 +214,6 @@ where } } - /// Consume this object and return the underlying components. - pub fn into_parts(self) -> (IO, rustls::server::Accepted) { - (self.io, self.accepted) - } - pub fn client_hello(&self) -> rustls::server::ClientHello<'_> { self.accepted.client_hello() } From 2ffa6fc97e15f08fce7656b61a40c3df3e5cf3c8 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 17 Sep 2025 09:31:52 +0200 Subject: [PATCH 2/4] server: align argument order with fields --- src/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.rs b/src/server.rs index d13d878f..1eeabc62 100644 --- a/src/server.rs +++ b/src/server.rs @@ -207,7 +207,7 @@ where IO: AsyncRead + AsyncWrite + Unpin, { /// Create a new object from an `IO` transport and prior TLS metadata. - pub fn from_parts(transport: IO, accepted: rustls::server::Accepted) -> Self { + pub fn from_parts(accepted: rustls::server::Accepted, transport: IO) -> Self { Self { accepted, io: transport, From 32e4d657cf85bb76d023dcfb6b56ad1f98e02f8f Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 17 Sep 2025 09:33:01 +0200 Subject: [PATCH 3/4] Warn on clippy::use_self --- src/client.rs | 6 +++--- src/common/handshake.rs | 10 +++++----- src/common/mod.rs | 14 +++++++------- src/lib.rs | 34 +++++++++++++++++----------------- src/server.rs | 4 ++-- tests/utils.rs | 2 +- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/client.rs b/src/client.rs index c2ecd755..128e840e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -31,7 +31,7 @@ impl TlsConnector { /// If you want to use 0-RTT, /// You must also set `ClientConfig.enable_early_data` to `true`. #[cfg(feature = "early-data")] - pub fn early_data(mut self, flag: bool) -> TlsConnector { + pub fn early_data(mut self, flag: bool) -> Self { self.early_data = flag; self } @@ -114,8 +114,8 @@ impl TlsConnector { } impl From> for TlsConnector { - fn from(inner: Arc) -> TlsConnector { - TlsConnector { + fn from(inner: Arc) -> Self { + Self { inner, #[cfg(feature = "early-data")] early_data: false, diff --git a/src/common/handshake.rs b/src/common/handshake.rs index e9cc65c2..a772c00b 100644 --- a/src/common/handshake.rs +++ b/src/common/handshake.rs @@ -45,16 +45,16 @@ where fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.get_mut(); - let mut stream = match mem::replace(this, MidHandshake::End) { - MidHandshake::Handshaking(stream) => stream, - MidHandshake::SendAlert { + let mut stream = match mem::replace(this, Self::End) { + Self::Handshaking(stream) => stream, + Self::SendAlert { mut io, mut alert, error, } => loop { match alert.write(&mut SyncWriteAdapter { io: &mut io, cx }) { Err(e) if e.kind() == io::ErrorKind::WouldBlock => { - *this = MidHandshake::SendAlert { io, error, alert }; + *this = Self::SendAlert { io, error, alert }; return Poll::Pending; } Err(_) | Ok(0) => return Poll::Ready(Err((error, io))), @@ -62,7 +62,7 @@ where }; }, // Starting the handshake returned an error; fail the future immediately. - MidHandshake::Error { io, error } => return Poll::Ready(Err((error, io))), + Self::Error { io, error } => return Poll::Ready(Err((error, io))), _ => panic!("unexpected polling after handshake"), }; diff --git a/src/common/mod.rs b/src/common/mod.rs index 7d68069b..07e173b5 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -23,33 +23,33 @@ impl TlsState { #[inline] pub(crate) fn shutdown_read(&mut self) { match *self { - TlsState::WriteShutdown | TlsState::FullyShutdown => *self = TlsState::FullyShutdown, - _ => *self = TlsState::ReadShutdown, + Self::WriteShutdown | Self::FullyShutdown => *self = Self::FullyShutdown, + _ => *self = Self::ReadShutdown, } } #[inline] pub(crate) fn shutdown_write(&mut self) { match *self { - TlsState::ReadShutdown | TlsState::FullyShutdown => *self = TlsState::FullyShutdown, - _ => *self = TlsState::WriteShutdown, + Self::ReadShutdown | Self::FullyShutdown => *self = Self::FullyShutdown, + _ => *self = Self::WriteShutdown, } } #[inline] pub(crate) fn writeable(&self) -> bool { - !matches!(*self, TlsState::WriteShutdown | TlsState::FullyShutdown) + !matches!(*self, Self::WriteShutdown | Self::FullyShutdown) } #[inline] pub(crate) fn readable(&self) -> bool { - !matches!(*self, TlsState::ReadShutdown | TlsState::FullyShutdown) + !matches!(*self, Self::ReadShutdown | Self::FullyShutdown) } #[inline] #[cfg(feature = "early-data")] pub(crate) fn is_early_data(&self) -> bool { - matches!(self, TlsState::EarlyData(..)) + matches!(self, Self::EarlyData(..)) } #[inline] diff --git a/src/lib.rs b/src/lib.rs index d80ca86d..c1d11cd9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ //! //! see -#![warn(unreachable_pub)] +#![warn(unreachable_pub, clippy::use_self)] use std::io; #[cfg(unix)] @@ -150,8 +150,8 @@ where buf: &mut ReadBuf<'_>, ) -> Poll> { match self.get_mut() { - TlsStream::Client(x) => Pin::new(x).poll_read(cx, buf), - TlsStream::Server(x) => Pin::new(x).poll_read(cx, buf), + Self::Client(x) => Pin::new(x).poll_read(cx, buf), + Self::Server(x) => Pin::new(x).poll_read(cx, buf), } } } @@ -163,16 +163,16 @@ where #[inline] fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.get_mut() { - TlsStream::Client(x) => Pin::new(x).poll_fill_buf(cx), - TlsStream::Server(x) => Pin::new(x).poll_fill_buf(cx), + Self::Client(x) => Pin::new(x).poll_fill_buf(cx), + Self::Server(x) => Pin::new(x).poll_fill_buf(cx), } } #[inline] fn consume(self: Pin<&mut Self>, amt: usize) { match self.get_mut() { - TlsStream::Client(x) => Pin::new(x).consume(amt), - TlsStream::Server(x) => Pin::new(x).consume(amt), + Self::Client(x) => Pin::new(x).consume(amt), + Self::Server(x) => Pin::new(x).consume(amt), } } } @@ -188,8 +188,8 @@ where buf: &[u8], ) -> Poll> { match self.get_mut() { - TlsStream::Client(x) => Pin::new(x).poll_write(cx, buf), - TlsStream::Server(x) => Pin::new(x).poll_write(cx, buf), + Self::Client(x) => Pin::new(x).poll_write(cx, buf), + Self::Server(x) => Pin::new(x).poll_write(cx, buf), } } @@ -200,32 +200,32 @@ where bufs: &[io::IoSlice<'_>], ) -> Poll> { match self.get_mut() { - TlsStream::Client(x) => Pin::new(x).poll_write_vectored(cx, bufs), - TlsStream::Server(x) => Pin::new(x).poll_write_vectored(cx, bufs), + Self::Client(x) => Pin::new(x).poll_write_vectored(cx, bufs), + Self::Server(x) => Pin::new(x).poll_write_vectored(cx, bufs), } } #[inline] fn is_write_vectored(&self) -> bool { match self { - TlsStream::Client(x) => x.is_write_vectored(), - TlsStream::Server(x) => x.is_write_vectored(), + Self::Client(x) => x.is_write_vectored(), + Self::Server(x) => x.is_write_vectored(), } } #[inline] fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.get_mut() { - TlsStream::Client(x) => Pin::new(x).poll_flush(cx), - TlsStream::Server(x) => Pin::new(x).poll_flush(cx), + Self::Client(x) => Pin::new(x).poll_flush(cx), + Self::Server(x) => Pin::new(x).poll_flush(cx), } } #[inline] fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.get_mut() { - TlsStream::Client(x) => Pin::new(x).poll_shutdown(cx), - TlsStream::Server(x) => Pin::new(x).poll_shutdown(cx), + Self::Client(x) => Pin::new(x).poll_shutdown(cx), + Self::Server(x) => Pin::new(x).poll_shutdown(cx), } } } diff --git a/src/server.rs b/src/server.rs index 1eeabc62..e8cada38 100644 --- a/src/server.rs +++ b/src/server.rs @@ -21,8 +21,8 @@ pub struct TlsAcceptor { } impl From> for TlsAcceptor { - fn from(inner: Arc) -> TlsAcceptor { - TlsAcceptor { inner } + fn from(inner: Arc) -> Self { + Self { inner } } } diff --git a/tests/utils.rs b/tests/utils.rs index 34ab0cce..80339918 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -82,7 +82,7 @@ mod utils { impl FlushWrapper { #[allow(dead_code)] pub(crate) fn new(stream: S) -> Self { - FlushWrapper { + Self { stream, buf: VecDeque::new(), queued: Vec::new(), From 2102fbb44d17582d8b1f356d970a20fd8f993d52 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 17 Sep 2025 09:33:42 +0200 Subject: [PATCH 4/4] Fix warning about potentially incomplete writes --- src/common/test_stream.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/test_stream.rs b/src/common/test_stream.rs index 353f65dc..e0ed8189 100644 --- a/src/common/test_stream.rs +++ b/src/common/test_stream.rs @@ -356,7 +356,7 @@ async fn stream_write_zero() -> io::Result<()> { let mut io = Eof; let mut stream = Stream::new(&mut io, &mut client); - stream.write(b"1").await.unwrap(); + stream.write_all(b"1").await.unwrap(); let result = stream.flush().await; assert_eq!( result.err().map(|e| e.kind()),