From 055a1f4ac7260356716e755df14a23b33679076c Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 25 Oct 2022 13:13:45 +0100 Subject: [PATCH 1/4] Move 5.7 beta APIs to NIOCore Motivation: As 5.7 has shipped we no longer need to keep these APIs in _NIOBeta. We're going to do a two-stage removal: first we're going to move the APIs to NIOCore and keep them in _NIOBeta with deprecations on them. In a later release, we'll remove the APIs from _NIOBeta entirely. Modifications: - Move the TimeAmount + Duration APIs to NIOCore - Deprecate the APIs in _NIOBeta. Result: We're on a path to remove _NIOBeta --- Sources/NIOCore/TimeAmount+Duration.swift | 56 ++++++++++++++++++++++ Sources/_NIOBeta/TimeAmount+Duration.swift | 3 ++ 2 files changed, 59 insertions(+) create mode 100644 Sources/NIOCore/TimeAmount+Duration.swift diff --git a/Sources/NIOCore/TimeAmount+Duration.swift b/Sources/NIOCore/TimeAmount+Duration.swift new file mode 100644 index 0000000000..7b48ae7982 --- /dev/null +++ b/Sources/NIOCore/TimeAmount+Duration.swift @@ -0,0 +1,56 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +#if (os(macOS) && swift(>=5.7.1)) || (!os(macOS) && swift(>=5.7)) +extension TimeAmount { + @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) + /// Creates a new `TimeAmount` for the given `Duration`, truncating and clamping if necessary. + /// + /// - returns: `TimeAmount`, truncated to nanosecond precision, and clamped to `Int64.max` nanoseconds. + public init(_ duration: Swift.Duration) { + self = .nanoseconds(duration.nanosecondsClamped) + } +} + +@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) +extension Swift.Duration { + /// Construct a `Duration` given a number of nanoseconds represented as a `TimeAmount`. + /// + /// - returns: A `Duration` representing a given number of nanoseconds. + public init(_ timeAmount: TimeAmount) { + self = .nanoseconds(timeAmount.nanoseconds) + } +} + +@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) +internal extension Swift.Duration { + /// The duration represented as nanoseconds, clamped to maximum expressible value. + var nanosecondsClamped: Int64 { + let components = self.components + + let secondsComponentNanos = components.seconds.multipliedReportingOverflow(by: 1_000_000_000) + let attosCompononentNanos = components.attoseconds / 1_000_000_000 + let combinedNanos = secondsComponentNanos.partialValue.addingReportingOverflow(attosCompononentNanos) + + guard + !secondsComponentNanos.overflow, + !combinedNanos.overflow + else { + return .max + } + + return combinedNanos.partialValue + } +} +#endif // (os(macOS) && swift(>=5.7.1)) || (!os(macOS) && swift(>=5.7)) diff --git a/Sources/_NIOBeta/TimeAmount+Duration.swift b/Sources/_NIOBeta/TimeAmount+Duration.swift index 50f3543b35..6ec863717e 100644 --- a/Sources/_NIOBeta/TimeAmount+Duration.swift +++ b/Sources/_NIOBeta/TimeAmount+Duration.swift @@ -17,6 +17,7 @@ import NIOCore #if (os(macOS) && swift(>=5.7.1)) || (!os(macOS) && swift(>=5.7)) extension TimeAmount { @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) + @available(*, deprecated, message: "This API has been moved to NIOCore and will be removed in a future update.") /// Creates a new `TimeAmount` for the given `Duration`, truncating and clamping if necessary. /// /// - returns: `TimeAmount`, truncated to nanosecond precision, and clamped to `Int64.max` nanoseconds. @@ -30,6 +31,7 @@ extension Swift.Duration { /// Construct a `Duration` given a number of nanoseconds represented as a `TimeAmount`. /// /// - returns: A `Duration` representing a given number of nanoseconds. + @available(*, deprecated, message: "This API has been moved to NIOCore and will be removed in a future update.") public init(_ timeAmount: TimeAmount) { self = .nanoseconds(timeAmount.nanoseconds) } @@ -38,6 +40,7 @@ extension Swift.Duration { @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) internal extension Swift.Duration { /// The duration represented as nanoseconds, clamped to maximum expressible value. + @available(*, deprecated, message: "This API has been moved to NIOCore and will be removed in a future update.") var nanosecondsClamped: Int64 { let components = self.components From 734b3ca52e82aeb87801577588e582eb93bf6020 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 25 Oct 2022 13:23:26 +0100 Subject: [PATCH 2/4] Move the tests too --- Package.swift | 2 -- Tests/LinuxMain.swift | 1 - .../TimeAmount+DurationTests+XCTest.swift | 2 +- .../TimeAmount+DurationTests.swift | 1 - 4 files changed, 1 insertion(+), 5 deletions(-) rename Tests/{NIOBetaTests => NIOCoreTests}/TimeAmount+DurationTests+XCTest.swift (94%) rename Tests/{NIOBetaTests => NIOCoreTests}/TimeAmount+DurationTests.swift (99%) diff --git a/Package.swift b/Package.swift index 40ba04af3b..8e8619e525 100644 --- a/Package.swift +++ b/Package.swift @@ -102,8 +102,6 @@ var targets: [PackageDescription.Target] = [ dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]), .testTarget(name: "NIOCoreTests", dependencies: ["NIOCore", "NIOEmbedded", "NIOFoundationCompat"]), - .testTarget(name: "NIOBetaTests", - dependencies: ["_NIOBeta"]), .testTarget(name: "NIOEmbeddedTests", dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]), .testTarget(name: "NIOPosixTests", diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index f4633f20a0..9e2c0e3261 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -24,7 +24,6 @@ import XCTest #if !compiler(>=5.5) #if os(Linux) || os(FreeBSD) || os(Android) - @testable import NIOBetaTests @testable import NIOConcurrencyHelpersTests @testable import NIOCoreTests @testable import NIODataStructuresTests diff --git a/Tests/NIOBetaTests/TimeAmount+DurationTests+XCTest.swift b/Tests/NIOCoreTests/TimeAmount+DurationTests+XCTest.swift similarity index 94% rename from Tests/NIOBetaTests/TimeAmount+DurationTests+XCTest.swift rename to Tests/NIOCoreTests/TimeAmount+DurationTests+XCTest.swift index 783ada7248..7bc4d3baff 100644 --- a/Tests/NIOBetaTests/TimeAmount+DurationTests+XCTest.swift +++ b/Tests/NIOCoreTests/TimeAmount+DurationTests+XCTest.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftNIO open source project // -// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors +// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information diff --git a/Tests/NIOBetaTests/TimeAmount+DurationTests.swift b/Tests/NIOCoreTests/TimeAmount+DurationTests.swift similarity index 99% rename from Tests/NIOBetaTests/TimeAmount+DurationTests.swift rename to Tests/NIOCoreTests/TimeAmount+DurationTests.swift index 044183661c..6d0a13d1c1 100644 --- a/Tests/NIOBetaTests/TimeAmount+DurationTests.swift +++ b/Tests/NIOCoreTests/TimeAmount+DurationTests.swift @@ -12,7 +12,6 @@ // //===----------------------------------------------------------------------===// import NIOCore -@testable import _NIOBeta import XCTest class TimeAmountDurationTests: XCTestCase { From c1e4061a0c7625abcb365b166d094ec3f2ad4e31 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 25 Oct 2022 13:27:38 +0100 Subject: [PATCH 3/4] Add @testable import --- Tests/NIOCoreTests/TimeAmount+DurationTests+XCTest.swift | 2 +- Tests/NIOCoreTests/TimeAmount+DurationTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/NIOCoreTests/TimeAmount+DurationTests+XCTest.swift b/Tests/NIOCoreTests/TimeAmount+DurationTests+XCTest.swift index 7bc4d3baff..783ada7248 100644 --- a/Tests/NIOCoreTests/TimeAmount+DurationTests+XCTest.swift +++ b/Tests/NIOCoreTests/TimeAmount+DurationTests+XCTest.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftNIO open source project // -// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors +// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information diff --git a/Tests/NIOCoreTests/TimeAmount+DurationTests.swift b/Tests/NIOCoreTests/TimeAmount+DurationTests.swift index 6d0a13d1c1..41a09066e2 100644 --- a/Tests/NIOCoreTests/TimeAmount+DurationTests.swift +++ b/Tests/NIOCoreTests/TimeAmount+DurationTests.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -import NIOCore +@testable import NIOCore import XCTest class TimeAmountDurationTests: XCTestCase { From fda9ec0cbb7b26667ef05bc91b52297f1704c8c8 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 25 Oct 2022 13:41:51 +0100 Subject: [PATCH 4/4] Fix up 5.5 Package.swift --- Package@swift-5.5.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/Package@swift-5.5.swift b/Package@swift-5.5.swift index e6a5d619d0..0507e62745 100644 --- a/Package@swift-5.5.swift +++ b/Package@swift-5.5.swift @@ -102,8 +102,6 @@ var targets: [PackageDescription.Target] = [ dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]), .testTarget(name: "NIOCoreTests", dependencies: ["NIOCore", "NIOEmbedded", "NIOFoundationCompat"]), - .testTarget(name: "NIOBetaTests", - dependencies: ["_NIOBeta"]), .testTarget(name: "NIOEmbeddedTests", dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]), .testTarget(name: "NIOPosixTests",