From 8922cbef795369e48b893c1b5b118de6c4fa21a5 Mon Sep 17 00:00:00 2001 From: taylorswift Date: Tue, 16 Jan 2024 01:21:52 +0000 Subject: [PATCH] finally admit that BSON.Millisecond is an InstantProtocol --- .../BSONABI/Primitives/BSON.Millisecond.swift | 21 +++++++++--------- .../BSON.Millisecond (ext).swift | 16 ++++++++++++++ .../{Duration.swift => Duration (ext).swift} | 0 .../ExpressibleByIntegerLiteral.swift | 9 -------- Sources/Durations/QuantizedDuration.swift | 22 +++++++++---------- Sources/Durations/Units/Microseconds.swift | 5 ++++- Sources/Durations/Units/Milliseconds.swift | 3 +++ Sources/Durations/Units/Minutes.swift | 3 +++ Sources/Durations/Units/Nanoseconds.swift | 3 +++ Sources/Durations/Units/Seconds.swift | 5 ++++- 10 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 Sources/BSON_Durations/BSON.Millisecond (ext).swift rename Sources/Durations/{Duration.swift => Duration (ext).swift} (100%) delete mode 100644 Sources/Durations/ExpressibleByIntegerLiteral.swift diff --git a/Sources/BSONABI/Primitives/BSON.Millisecond.swift b/Sources/BSONABI/Primitives/BSON.Millisecond.swift index ee249c66..a70d6d2f 100644 --- a/Sources/BSONABI/Primitives/BSON.Millisecond.swift +++ b/Sources/BSONABI/Primitives/BSON.Millisecond.swift @@ -2,13 +2,10 @@ extension BSON { /// A number of UTC milliseconds since the Unix epoch. /// - /// This library does not have access to calender-aware facilities. - /// Therefore, UTC milliseconds are not ``Comparable``, at least when importing - /// this module alone. - /// - /// Take caution when using this type’s ``Hashable`` and ``Equatable`` conformances. - /// Two equivalent `Millisecond` values do not necessarily reference the same - /// instant in time. + /// This library does not have access to calender-aware facilities. When using this type’s + /// ``Equatable``, ``Hashable``, or ``Comparable`` features, keep in mind that a + /// `Millisecond` is just a number, and is only as meaningful as the clock (if any) that + /// produced it. @frozen public struct Millisecond:Hashable, Equatable, Sendable { @@ -22,11 +19,13 @@ extension BSON } } } +extension BSON.Millisecond:Comparable +{ + @inlinable public static + func < (a:Self, b:Self) -> Bool { a.value < b.value } +} extension BSON.Millisecond:ExpressibleByIntegerLiteral { @inlinable public - init(integerLiteral:Int64) - { - self.init(integerLiteral) - } + init(integerLiteral:Int64) { self.init(integerLiteral) } } diff --git a/Sources/BSON_Durations/BSON.Millisecond (ext).swift b/Sources/BSON_Durations/BSON.Millisecond (ext).swift new file mode 100644 index 00000000..56a66335 --- /dev/null +++ b/Sources/BSON_Durations/BSON.Millisecond (ext).swift @@ -0,0 +1,16 @@ +import BSON +import Durations + +extension BSON.Millisecond:InstantProtocol +{ + @inlinable public + func advanced(by duration:Milliseconds) -> Self + { + .init(self.value + duration.rawValue) + } + @inlinable public + func duration(to other:Self) -> Milliseconds + { + .init(rawValue: other.value - self.value) + } +} diff --git a/Sources/Durations/Duration.swift b/Sources/Durations/Duration (ext).swift similarity index 100% rename from Sources/Durations/Duration.swift rename to Sources/Durations/Duration (ext).swift diff --git a/Sources/Durations/ExpressibleByIntegerLiteral.swift b/Sources/Durations/ExpressibleByIntegerLiteral.swift deleted file mode 100644 index 170208d8..00000000 --- a/Sources/Durations/ExpressibleByIntegerLiteral.swift +++ /dev/null @@ -1,9 +0,0 @@ -extension ExpressibleByIntegerLiteral - where Self:QuantizedDuration, RawValue:_ExpressibleByBuiltinIntegerLiteral -{ - @inlinable public - init(integerLiteral:RawValue) - { - self.init(rawValue: integerLiteral) - } -} diff --git a/Sources/Durations/QuantizedDuration.swift b/Sources/Durations/QuantizedDuration.swift index f37cc29d..4e7674bf 100644 --- a/Sources/Durations/QuantizedDuration.swift +++ b/Sources/Durations/QuantizedDuration.swift @@ -1,9 +1,5 @@ public -protocol QuantizedDuration:DurationProtocol, - CustomStringConvertible, - RawRepresentable, - Hashable, - Sendable +protocol QuantizedDuration:DurationProtocol, RawRepresentable, Hashable, Sendable where RawValue:BinaryInteger { static @@ -13,20 +9,22 @@ protocol QuantizedDuration:DurationProtocol, /// Rounds the given attosecond-resolution duration towards zero. init(truncating duration:Duration) } -extension QuantizedDuration +extension QuantizedDuration where Self:CustomStringConvertible { @inlinable public - var description:String - { - "\(self.rawValue) \(Self.unit)" - } + var description:String { "\(self.rawValue) \(Self.unit)" } } extension QuantizedDuration where RawValue:FixedWidthInteger { @inlinable public static - var max:Self + var max:Self { .init(rawValue: .max) } +} +extension QuantizedDuration where Self:ExpressibleByIntegerLiteral +{ + @inlinable public + init(integerLiteral:RawValue) { - .init(rawValue: .max) + self.init(rawValue: integerLiteral) } } extension QuantizedDuration where Self == Minutes diff --git a/Sources/Durations/Units/Microseconds.swift b/Sources/Durations/Units/Microseconds.swift index 8f07b972..b842a685 100644 --- a/Sources/Durations/Units/Microseconds.swift +++ b/Sources/Durations/Units/Microseconds.swift @@ -14,7 +14,7 @@ extension Microseconds:QuantizedDuration { @inlinable public static var unit:String { "µs" } - + @inlinable public init(truncating duration:Duration) { @@ -25,3 +25,6 @@ extension Microseconds:QuantizedDuration extension Microseconds:ExpressibleByIntegerLiteral { } +extension Microseconds:CustomStringConvertible +{ +} diff --git a/Sources/Durations/Units/Milliseconds.swift b/Sources/Durations/Units/Milliseconds.swift index be052cd2..b04ca7ba 100644 --- a/Sources/Durations/Units/Milliseconds.swift +++ b/Sources/Durations/Units/Milliseconds.swift @@ -25,3 +25,6 @@ extension Milliseconds:QuantizedDuration extension Milliseconds:ExpressibleByIntegerLiteral { } +extension Milliseconds:CustomStringConvertible +{ +} diff --git a/Sources/Durations/Units/Minutes.swift b/Sources/Durations/Units/Minutes.swift index 821ff4f5..0b33415f 100644 --- a/Sources/Durations/Units/Minutes.swift +++ b/Sources/Durations/Units/Minutes.swift @@ -24,6 +24,9 @@ extension Minutes:QuantizedDuration extension Minutes:ExpressibleByIntegerLiteral { } +extension Minutes:CustomStringConvertible +{ +} extension Minutes { @inlinable public diff --git a/Sources/Durations/Units/Nanoseconds.swift b/Sources/Durations/Units/Nanoseconds.swift index 8d350ae2..6216fb64 100644 --- a/Sources/Durations/Units/Nanoseconds.swift +++ b/Sources/Durations/Units/Nanoseconds.swift @@ -25,3 +25,6 @@ extension Nanoseconds:QuantizedDuration extension Nanoseconds:ExpressibleByIntegerLiteral { } +extension Nanoseconds:CustomStringConvertible +{ +} diff --git a/Sources/Durations/Units/Seconds.swift b/Sources/Durations/Units/Seconds.swift index 23cba4a0..c1ce5fd6 100644 --- a/Sources/Durations/Units/Seconds.swift +++ b/Sources/Durations/Units/Seconds.swift @@ -14,7 +14,7 @@ extension Seconds:QuantizedDuration { @inlinable public static var unit:String { "s" } - + @inlinable public init(truncating duration:Duration) { @@ -24,6 +24,9 @@ extension Seconds:QuantizedDuration extension Seconds:ExpressibleByIntegerLiteral { } +extension Seconds:CustomStringConvertible +{ +} extension Seconds { @inlinable public