From 436ba2c1294d088ca39d2e5b1fb94b2e8bd05fcd Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Wed, 23 Dec 2020 14:19:24 +0900 Subject: [PATCH] chore: use #[non_exhaustive] instead of private unit field --- examples/tinyhttp.rs | 5 +++-- tokio-stream/src/timeout.rs | 11 +++-------- tokio-util/src/codec/bytes_codec.rs | 5 +++-- tokio-util/src/codec/framed.rs | 8 +------- tokio-util/src/codec/length_delimited.rs | 9 ++++----- tokio/src/io/async_fd.rs | 7 ++++--- tokio/src/io/util/empty.rs | 7 +++---- tokio/src/io/util/sink.rs | 7 +++---- tokio/src/runtime/handle.rs | 5 +++-- tokio/src/sync/batch_semaphore.rs | 6 ++++-- tokio/src/sync/mpsc/error.rs | 3 ++- tokio/src/sync/mutex.rs | 7 ++++--- tokio/src/task/task_local.rs | 7 +++---- tokio/src/time/error.rs | 9 ++------- tokio/src/time/timeout.rs | 2 +- 15 files changed, 43 insertions(+), 55 deletions(-) diff --git a/examples/tinyhttp.rs b/examples/tinyhttp.rs index e86305f367e..86b7e3597cb 100644 --- a/examples/tinyhttp.rs +++ b/examples/tinyhttp.rs @@ -224,12 +224,13 @@ mod date { use time::{self, Duration}; - pub struct Now(()); + #[non_exhaustive] + pub struct Now; /// Returns a struct, which when formatted, renders an appropriate `Date` /// header value. pub fn now() -> Now { - Now(()) + Now } // Gee Alex, doesn't this seem like premature optimization. Well you see diff --git a/tokio-stream/src/timeout.rs b/tokio-stream/src/timeout.rs index 9ebbaa2332c..51dad240496 100644 --- a/tokio-stream/src/timeout.rs +++ b/tokio-stream/src/timeout.rs @@ -24,7 +24,8 @@ pin_project! { /// Error returned by `Timeout`. #[derive(Debug, PartialEq)] -pub struct Elapsed(()); +#[non_exhaustive] +pub struct Elapsed; impl Timeout { pub(super) fn new(stream: S, duration: Duration) -> Self { @@ -61,7 +62,7 @@ impl Stream for Timeout { if *me.poll_deadline { ready!(me.deadline.poll(cx)); *me.poll_deadline = false; - return Poll::Ready(Some(Err(Elapsed::new()))); + return Poll::Ready(Some(Err(Elapsed))); } Poll::Pending @@ -74,12 +75,6 @@ impl Stream for Timeout { // ===== impl Elapsed ===== -impl Elapsed { - pub(crate) fn new() -> Self { - Elapsed(()) - } -} - impl fmt::Display for Elapsed { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { "deadline has elapsed".fmt(fmt) diff --git a/tokio-util/src/codec/bytes_codec.rs b/tokio-util/src/codec/bytes_codec.rs index 275031c066c..2c20bb7b1e6 100644 --- a/tokio-util/src/codec/bytes_codec.rs +++ b/tokio-util/src/codec/bytes_codec.rs @@ -42,12 +42,13 @@ use std::io; /// ``` /// #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Default)] -pub struct BytesCodec(()); +#[non_exhaustive] +pub struct BytesCodec; impl BytesCodec { /// Creates a new `BytesCodec` for shipping around raw bytes. pub fn new() -> BytesCodec { - BytesCodec(()) + BytesCodec } } diff --git a/tokio-util/src/codec/framed.rs b/tokio-util/src/codec/framed.rs index adfe06380e0..8f57280c284 100644 --- a/tokio-util/src/codec/framed.rs +++ b/tokio-util/src/codec/framed.rs @@ -219,7 +219,6 @@ impl Framed { codec: self.inner.codec, read_buf: self.inner.state.read.buffer, write_buf: self.inner.state.write.buffer, - _priv: (), } } } @@ -282,7 +281,7 @@ where /// /// [`Framed`]: crate::codec::Framed #[derive(Debug)] -#[allow(clippy::manual_non_exhaustive)] +#[non_exhaustive] pub struct FramedParts { /// The inner transport used to read bytes to and write bytes to pub io: T, @@ -295,10 +294,6 @@ pub struct FramedParts { /// A buffer with unprocessed data which are not written yet. pub write_buf: BytesMut, - - /// This private field allows us to add additional fields in the future in a - /// backwards compatible way. - _priv: (), } impl FramedParts { @@ -312,7 +307,6 @@ impl FramedParts { codec, read_buf: BytesMut::new(), write_buf: BytesMut::new(), - _priv: (), } } } diff --git a/tokio-util/src/codec/length_delimited.rs b/tokio-util/src/codec/length_delimited.rs index ef706dfabbd..589f803f03d 100644 --- a/tokio-util/src/codec/length_delimited.rs +++ b/tokio-util/src/codec/length_delimited.rs @@ -409,9 +409,8 @@ pub struct Builder { } /// An error when the number of bytes read is more than max frame length. -pub struct LengthDelimitedCodecError { - _priv: (), -} +#[non_exhaustive] +pub struct LengthDelimitedCodecError; /// A codec for frames delimited by a frame head specifying their lengths. /// @@ -496,7 +495,7 @@ impl LengthDelimitedCodec { if n > self.builder.max_frame_len as u64 { return Err(io::Error::new( io::ErrorKind::InvalidData, - LengthDelimitedCodecError { _priv: () }, + LengthDelimitedCodecError, )); } @@ -586,7 +585,7 @@ impl Encoder for LengthDelimitedCodec { if n > self.builder.max_frame_len { return Err(io::Error::new( io::ErrorKind::InvalidInput, - LengthDelimitedCodecError { _priv: () }, + LengthDelimitedCodecError, )); } diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index 08d7b91f57d..c14319a275b 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -530,7 +530,7 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyGuard<'a, Inner> { } match result { - Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(TryIoError(())), + Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(TryIoError), result => Ok(result), } } @@ -591,7 +591,7 @@ impl<'a, Inner: AsRawFd> AsyncFdReadyMutGuard<'a, Inner> { } match result { - Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(TryIoError(())), + Err(err) if err.kind() == io::ErrorKind::WouldBlock => Err(TryIoError), result => Ok(result), } } @@ -620,4 +620,5 @@ impl<'a, T: std::fmt::Debug + AsRawFd> std::fmt::Debug for AsyncFdReadyMutGuard< /// [`WouldBlock`]: std::io::ErrorKind::WouldBlock /// [`try_io`]: method@AsyncFdReadyGuard::try_io #[derive(Debug)] -pub struct TryIoError(()); +#[non_exhaustive] +pub struct TryIoError; diff --git a/tokio/src/io/util/empty.rs b/tokio/src/io/util/empty.rs index f964d18e6ef..099b3011b39 100644 --- a/tokio/src/io/util/empty.rs +++ b/tokio/src/io/util/empty.rs @@ -15,9 +15,8 @@ cfg_io_util! { /// /// [`empty`]: fn@empty /// [std]: std::io::empty - pub struct Empty { - _p: (), - } + #[non_exhaustive] + pub struct Empty; /// Creates a new empty async reader. /// @@ -42,7 +41,7 @@ cfg_io_util! { /// } /// ``` pub fn empty() -> Empty { - Empty { _p: () } + Empty } } diff --git a/tokio/src/io/util/sink.rs b/tokio/src/io/util/sink.rs index 05ee773fa38..ecb34ce9a5f 100644 --- a/tokio/src/io/util/sink.rs +++ b/tokio/src/io/util/sink.rs @@ -15,9 +15,8 @@ cfg_io_util! { /// /// [sink]: sink() /// [std]: std::io::Sink - pub struct Sink { - _p: (), - } + #[non_exhaustive] + pub struct Sink; /// Creates an instance of an async writer which will successfully consume all /// data. @@ -45,7 +44,7 @@ cfg_io_util! { /// } /// ``` pub fn sink() -> Sink { - Sink { _p: () } + Sink } } diff --git a/tokio/src/runtime/handle.rs b/tokio/src/runtime/handle.rs index 6ff3c393020..6dbc74bbd2d 100644 --- a/tokio/src/runtime/handle.rs +++ b/tokio/src/runtime/handle.rs @@ -106,7 +106,7 @@ impl Handle { /// /// Contrary to `current`, this never panics pub fn try_current() -> Result { - context::current().ok_or(TryCurrentError(())) + context::current().ok_or(TryCurrentError) } /// Spawn a future onto the Tokio runtime. @@ -203,7 +203,8 @@ impl Handle { } /// Error returned by `try_current` when no Runtime has been started -pub struct TryCurrentError(()); +#[non_exhaustive] +pub struct TryCurrentError; impl fmt::Debug for TryCurrentError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/tokio/src/sync/batch_semaphore.rs b/tokio/src/sync/batch_semaphore.rs index 803f2a18c6d..8c32f62d9f6 100644 --- a/tokio/src/sync/batch_semaphore.rs +++ b/tokio/src/sync/batch_semaphore.rs @@ -54,6 +54,7 @@ pub enum TryAcquireError { /// The semaphore has no available permits. NoPermits, } + /// Error returned from the [`Semaphore::acquire`] function. /// /// An `acquire` operation can only fail if the semaphore has been @@ -62,7 +63,8 @@ pub enum TryAcquireError { /// [closed]: crate::sync::Semaphore::close /// [`Semaphore::acquire`]: crate::sync::Semaphore::acquire #[derive(Debug)] -pub struct AcquireError(()); +#[non_exhaustive] +pub struct AcquireError; pub(crate) struct Acquire<'a> { node: Waiter, @@ -521,7 +523,7 @@ unsafe impl Sync for Acquire<'_> {} impl AcquireError { fn closed() -> AcquireError { - AcquireError(()) + AcquireError } } diff --git a/tokio/src/sync/mpsc/error.rs b/tokio/src/sync/mpsc/error.rs index d23255b5aab..a85d706dd15 100644 --- a/tokio/src/sync/mpsc/error.rs +++ b/tokio/src/sync/mpsc/error.rs @@ -55,7 +55,8 @@ impl From> for TrySendError { /// Error returned by `Receiver`. #[derive(Debug)] -pub struct RecvError(()); +#[non_exhaustive] +pub struct RecvError; impl fmt::Display for RecvError { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 21e44ca932c..39861c220d5 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -167,7 +167,8 @@ unsafe impl Sync for OwnedMutexGuard where T: ?Sized + Send + Sync {} /// /// [`Mutex::try_lock`]: Mutex::try_lock #[derive(Debug)] -pub struct TryLockError(()); +#[non_exhaustive] +pub struct TryLockError; impl fmt::Display for TryLockError { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -323,7 +324,7 @@ impl Mutex { pub fn try_lock(&self) -> Result, TryLockError> { match self.s.try_acquire(1) { Ok(_) => Ok(MutexGuard { lock: self }), - Err(_) => Err(TryLockError(())), + Err(_) => Err(TryLockError), } } @@ -378,7 +379,7 @@ impl Mutex { pub fn try_lock_owned(self: Arc) -> Result, TryLockError> { match self.s.try_acquire(1) { Ok(_) => Ok(OwnedMutexGuard { lock: self }), - Err(_) => Err(TryLockError(())), + Err(_) => Err(TryLockError), } } diff --git a/tokio/src/task/task_local.rs b/tokio/src/task/task_local.rs index bc2e54a4e18..41bb48f67f6 100644 --- a/tokio/src/task/task_local.rs +++ b/tokio/src/task/task_local.rs @@ -156,7 +156,7 @@ impl LocalKey { if let Some(val) = v.borrow().as_ref() { Ok(f(val)) } else { - Err(AccessError { _private: () }) + Err(AccessError) } }) } @@ -223,9 +223,8 @@ impl StaticLifetime for T {} /// An error returned by [`LocalKey::try_with`](method@LocalKey::try_with). #[derive(Clone, Copy, Eq, PartialEq)] -pub struct AccessError { - _private: (), -} +#[non_exhaustive] +pub struct AccessError; impl fmt::Debug for AccessError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/tokio/src/time/error.rs b/tokio/src/time/error.rs index 8674febe98d..4a4cc0ed615 100644 --- a/tokio/src/time/error.rs +++ b/tokio/src/time/error.rs @@ -42,7 +42,8 @@ impl From for Error { /// Error returned by `Timeout`. #[derive(Debug, PartialEq)] -pub struct Elapsed(()); +#[non_exhaustive] +pub struct Elapsed; #[derive(Debug)] pub(crate) enum InsertError { @@ -99,12 +100,6 @@ impl fmt::Display for Error { // ===== impl Elapsed ===== -impl Elapsed { - pub(crate) fn new() -> Self { - Elapsed(()) - } -} - impl fmt::Display for Elapsed { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { "deadline has elapsed".fmt(fmt) diff --git a/tokio/src/time/timeout.rs b/tokio/src/time/timeout.rs index 9d15a7205cd..4e0868e2e1b 100644 --- a/tokio/src/time/timeout.rs +++ b/tokio/src/time/timeout.rs @@ -148,7 +148,7 @@ where // Now check the timer match me.delay.poll(cx) { - Poll::Ready(()) => Poll::Ready(Err(Elapsed::new())), + Poll::Ready(()) => Poll::Ready(Err(Elapsed)), Poll::Pending => Poll::Pending, } }