Skip to content

Commit

Permalink
Merge pull request #113 from tayloraswift/millisecond-as-instantprotocol
Browse files Browse the repository at this point in the history
finally admit that BSON.Millisecond is an InstantProtocol
  • Loading branch information
tayloraswift authored Jan 16, 2024
2 parents 5af36cb + 8922cbe commit 24f0fef
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 34 deletions.
21 changes: 10 additions & 11 deletions Sources/BSONABI/Primitives/BSON.Millisecond.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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) }
}
16 changes: 16 additions & 0 deletions Sources/BSON_Durations/BSON.Millisecond (ext).swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
File renamed without changes.
9 changes: 0 additions & 9 deletions Sources/Durations/ExpressibleByIntegerLiteral.swift

This file was deleted.

22 changes: 10 additions & 12 deletions Sources/Durations/QuantizedDuration.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
public
protocol QuantizedDuration<RawValue>:DurationProtocol,
CustomStringConvertible,
RawRepresentable,
Hashable,
Sendable
protocol QuantizedDuration<RawValue>:DurationProtocol, RawRepresentable, Hashable, Sendable
where RawValue:BinaryInteger
{
static
Expand All @@ -13,20 +9,22 @@ protocol QuantizedDuration<RawValue>: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
Expand Down
5 changes: 4 additions & 1 deletion Sources/Durations/Units/Microseconds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension Microseconds:QuantizedDuration
{
@inlinable public static
var unit:String { "µs" }

@inlinable public
init(truncating duration:Duration)
{
Expand All @@ -25,3 +25,6 @@ extension Microseconds:QuantizedDuration
extension Microseconds:ExpressibleByIntegerLiteral
{
}
extension Microseconds:CustomStringConvertible
{
}
3 changes: 3 additions & 0 deletions Sources/Durations/Units/Milliseconds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ extension Milliseconds:QuantizedDuration
extension Milliseconds:ExpressibleByIntegerLiteral
{
}
extension Milliseconds:CustomStringConvertible
{
}
3 changes: 3 additions & 0 deletions Sources/Durations/Units/Minutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ extension Minutes:QuantizedDuration
extension Minutes:ExpressibleByIntegerLiteral
{
}
extension Minutes:CustomStringConvertible
{
}
extension Minutes
{
@inlinable public
Expand Down
3 changes: 3 additions & 0 deletions Sources/Durations/Units/Nanoseconds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ extension Nanoseconds:QuantizedDuration
extension Nanoseconds:ExpressibleByIntegerLiteral
{
}
extension Nanoseconds:CustomStringConvertible
{
}
5 changes: 4 additions & 1 deletion Sources/Durations/Units/Seconds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension Seconds:QuantizedDuration
{
@inlinable public static
var unit:String { "s" }

@inlinable public
init(truncating duration:Duration)
{
Expand All @@ -24,6 +24,9 @@ extension Seconds:QuantizedDuration
extension Seconds:ExpressibleByIntegerLiteral
{
}
extension Seconds:CustomStringConvertible
{
}
extension Seconds
{
@inlinable public
Expand Down

0 comments on commit 24f0fef

Please sign in to comment.