From 4b932ba2385eb8b99d87623e65bf5f32904c607f Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sun, 17 Dec 2023 23:56:10 -0500 Subject: [PATCH] Move from unit type to internal `NoOffset` type --- time/src/date_time.rs | 37 ++++++++++++++++++++++--------------- time/src/quickcheck.rs | 8 +++++++- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/time/src/date_time.rs b/time/src/date_time.rs index f036012a8..43ef0215d 100644 --- a/time/src/date_time.rs +++ b/time/src/date_time.rs @@ -38,17 +38,20 @@ pub(crate) mod offset_kind { pub enum Fixed {} } -pub(crate) use sealed::MaybeOffset; use sealed::*; +pub(crate) use sealed::{MaybeOffset, NoOffset}; mod sealed { use super::*; - /// A type that is guaranteed to be either `()` or [`UtcOffset`]. + #[derive(Debug, Clone, Copy)] + pub struct NoOffset; + + /// A type that is guaranteed to be either [`NoOffset`] or [`UtcOffset`]. /// /// **Do not** add any additional implementations of this trait. #[allow(unreachable_pub)] // intentional pub trait MaybeOffsetType {} - impl MaybeOffsetType for () {} + impl MaybeOffsetType for NoOffset {} impl MaybeOffsetType for UtcOffset {} pub trait MaybeOffset: Sized { @@ -88,21 +91,25 @@ mod sealed { pub trait HasLogicalOffset: MaybeOffset {} impl> HasLogicalOffset for T {} - pub trait NoLogicalOffset: MaybeOffset {} - impl> NoLogicalOffset for T {} + pub trait NoLogicalOffset: MaybeOffset {} + impl> NoLogicalOffset for T {} // Traits to indicate whether a `MaybeOffset` has a memory offset type of `UtcOffset` or not. pub trait HasMemoryOffset: MaybeOffset {} impl> HasMemoryOffset for T {} - pub trait NoMemoryOffset: MaybeOffset {} - impl> NoMemoryOffset for T {} + pub trait NoMemoryOffset: MaybeOffset {} + impl> NoMemoryOffset for T {} // Traits to indicate backing type being implemented. pub trait IsOffsetKindNone: - MaybeOffset + MaybeOffset< + Self_ = offset_kind::None, + MemoryOffsetType = NoOffset, + LogicalOffsetType = NoOffset, + > { } impl IsOffsetKindNone for offset_kind::None {} @@ -119,16 +126,16 @@ mod sealed { } impl MaybeOffset for offset_kind::None { - type MemoryOffsetType = (); - type LogicalOffsetType = (); + type MemoryOffsetType = NoOffset; + type LogicalOffsetType = NoOffset; type Self_ = Self; const STATIC_OFFSET: Option = None; #[cfg(feature = "parsing")] - fn try_from_parsed(_: Parsed) -> Result<(), error::TryFromParsed> { - Ok(()) + fn try_from_parsed(_: Parsed) -> Result { + Ok(NoOffset) } } @@ -225,13 +232,13 @@ impl DateTime { pub const MIN: Self = Self { date: Date::MIN, time: Time::MIN, - offset: (), + offset: NoOffset, }; pub const MAX: Self = Self { date: Date::MAX, time: Time::MAX, - offset: (), + offset: NoOffset, }; } @@ -252,7 +259,7 @@ impl DateTime { Self { date, time, - offset: (), + offset: NoOffset, } } diff --git a/time/src/quickcheck.rs b/time/src/quickcheck.rs index 02cc62282..51ed0a41f 100644 --- a/time/src/quickcheck.rs +++ b/time/src/quickcheck.rs @@ -38,7 +38,7 @@ use alloc::boxed::Box; use quickcheck::{empty_shrinker, single_shrinker, Arbitrary, Gen}; -use crate::date_time::{DateTime, MaybeOffset}; +use crate::date_time::{DateTime, MaybeOffset, NoOffset}; use crate::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday}; /// Obtain an arbitrary value between the minimum and maximum inclusive. @@ -220,3 +220,9 @@ impl Arbitrary for Month { } } } + +impl Arbitrary for NoOffset { + fn arbitrary(_: &mut Gen) -> Self { + Self + } +}