Skip to content

Commit

Permalink
Add a NO_FOUNDATION_COMPAT build setting
Browse files Browse the repository at this point in the history
  • Loading branch information
karwa committed Nov 4, 2021
1 parent 2f864b6 commit 78bdada
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 39 deletions.
28 changes: 26 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@

import PackageDescription

// This package recognizes the conditional compilation flags listed below.
// To use enable them, uncomment the corresponding lines or define them
// from the package manager command line:
//
// swift build -Xswiftc -DNO_FOUNDATION_COMPAT
var settings: [SwiftSetting]? = [

// Do not depend on Foundation.
//
// This removes:
// - UUID <-> UniqueID conversion APIs.
// - UUID timestamps as 'Date' (but Date will soon join the standard library).
//.define("NO_FOUNDATION_COMPAT"),

]

if settings?.isEmpty == true { settings = nil }

let package = Package(
name: "UniqueID",
products: [
Expand All @@ -25,7 +43,13 @@ let package = Package(
.package(name: "swift-atomics", url: "https://github.com/apple/swift-atomics", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(name: "UniqueID", dependencies: [.product(name: "Atomics", package: "swift-atomics")]),
.testTarget(name: "UniqueIDTests", dependencies: ["UniqueID"]),
.target(
name: "UniqueID",
dependencies: [.product(name: "Atomics", package: "swift-atomics")],
swiftSettings: settings),
.testTarget(
name: "UniqueIDTests",
dependencies: ["UniqueID"],
swiftSettings: settings),
]
)
52 changes: 28 additions & 24 deletions Sources/UniqueID/Foundation+UniqueID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
#if !NO_FOUNDATION_COMPAT

extension UUID {
import Foundation

/// Losslessly convert a `UniqueID` to a Foundation `UUID`.
///
@inlinable
public init(_ uniqueID: UniqueID) {
self.init(uuid: uniqueID.bytes)
extension UUID {

/// Losslessly convert a `UniqueID` to a Foundation `UUID`.
///
@inlinable
public init(_ uniqueID: UniqueID) {
self.init(uuid: uniqueID.bytes)
}
}
}

extension UniqueID {
extension UniqueID {

/// Losslessly convert a Foundation `UUID` to a `UniqueID`.
///
@inlinable
public init(_ uuid: Foundation.UUID) {
self.init(bytes: uuid.uuid)
/// Losslessly convert a Foundation `UUID` to a `UniqueID`.
///
@inlinable
public init(_ uuid: Foundation.UUID) {
self.init(bytes: uuid.uuid)
}
}
}

// Note: 'Date' will move in to the standard library and increase precision to capture this timestamp exactly.
// https://forums.swift.org/t/pitch-clock-instant-date-and-duration/52451
// Note: 'Date' will move in to the standard library and increase precision to capture this timestamp exactly.
// https://forums.swift.org/t/pitch-clock-instant-date-and-duration/52451

extension UniqueID.TimeOrdered {
extension UniqueID.TimeOrdered {

/// The timestamp of the UUID. Note that this has at most 100ns precision.
///
@inlinable
public var timestamp: Date {
Date(timeIntervalSince1970: TimeInterval(_uuid_timestamp_to_unix(timestamp: rawTimestamp)) / 10_000_000)
/// The timestamp of the UUID. Note that this has at most 100ns precision.
///
@inlinable
public var timestamp: Date {
Date(timeIntervalSince1970: TimeInterval(_uuid_timestamp_to_unix(timestamp: rawTimestamp)) / 10_000_000)
}
}
}

#endif // NO_FOUNDATION_COMPAT
6 changes: 4 additions & 2 deletions Tests/UniqueIDTests/UUIDv6Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ extension UUIDv6Tests {
XCTFail("Not a valid UUIDv6")
continue
}
XCTAssertGreaterThan(components.timestamp, beforeCreate)
#if !NO_FOUNDATION_COMPAT
XCTAssertGreaterThan(components.timestamp, beforeCreate)
XCTAssertLessThan(components.timestamp, afterCreate)
#endif // NO_FOUNDATION_COMPAT
XCTAssertGreaterThan(components.rawTimestamp, beforeCreateRaw)
XCTAssertLessThan(components.timestamp, afterCreate)
XCTAssertLessThan(components.rawTimestamp, afterCreateRaw)
}
}
Expand Down
26 changes: 15 additions & 11 deletions Tests/UniqueIDTests/UniqueIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,18 +291,22 @@ extension UniqueIDTests {

extension UniqueIDTests {

func testFoundationCompat() {
do {
let uuid: UUID = UUID(.timeOrdered())
let uniqueID: UniqueID = UniqueID(uuid)
XCTAssertEqual(uniqueID.description, uuid.description)
}
do {
let uuid = UUID()
let uniqueID = UniqueID(uuid)
XCTAssertEqual(uniqueID.description, uuid.description)
#if !NO_FOUNDATION_COMPAT

func testFoundationCompat() {
do {
let uuid: UUID = UUID(.timeOrdered())
let uniqueID: UniqueID = UniqueID(uuid)
XCTAssertEqual(uniqueID.description, uuid.description)
}
do {
let uuid = UUID()
let uniqueID = UniqueID(uuid)
XCTAssertEqual(uniqueID.description, uuid.description)
}
}
}

#endif // NO_FOUNDATION_COMPAT

func testParseHexTable() {
XCTAssertEqual(_parseHex_table.count, Int(UInt8.max))
Expand Down

0 comments on commit 78bdada

Please sign in to comment.