From f98bb562306fcaddb5832c82540aca37e97def2b Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Fri, 1 Dec 2023 23:22:58 -0600 Subject: [PATCH 1/3] Add missing platform specifications --- Package.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Package.swift b/Package.swift index b52ea72..5c521f7 100644 --- a/Package.swift +++ b/Package.swift @@ -4,6 +4,12 @@ import PackageDescription let package = Package( name: "swift-http-types", + platforms: [ + .macOS("10.15"), + .iOS("13.0"), + .watchOS("6.0"), + .tvOS("13.0"), + ], products: [ .library(name: "HTTPTypes", targets: ["HTTPTypes"]), .library(name: "HTTPTypesFoundation", targets: ["HTTPTypesFoundation"]), From 28a5f3aaeb6a28b44164922494939c779ecba805 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Sat, 2 Dec 2023 00:50:23 -0600 Subject: [PATCH 2/3] Per PR discussion, remove platform specifiers and discontinue use of `some` rather than forcing other packages to change their manifests. --- Package.swift | 6 --- Sources/HTTPTypes/HTTPField.swift | 8 ++-- Sources/HTTPTypes/HTTPFields.swift | 38 +++++++++---------- Sources/HTTPTypes/ISOLatin1String.swift | 4 +- .../HTTPTypesFoundation/HTTPRequest+URL.swift | 2 +- Tests/HTTPTypesTests/HTTPTypesTests.swift | 2 +- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/Package.swift b/Package.swift index 5c521f7..b52ea72 100644 --- a/Package.swift +++ b/Package.swift @@ -4,12 +4,6 @@ import PackageDescription let package = Package( name: "swift-http-types", - platforms: [ - .macOS("10.15"), - .iOS("13.0"), - .watchOS("6.0"), - .tvOS("13.0"), - ], products: [ .library(name: "HTTPTypes", targets: ["HTTPTypes"]), .library(name: "HTTPTypesFoundation", targets: ["HTTPTypesFoundation"]), diff --git a/Sources/HTTPTypes/HTTPField.swift b/Sources/HTTPTypes/HTTPField.swift index 306600a..038cbc8 100644 --- a/Sources/HTTPTypes/HTTPField.swift +++ b/Sources/HTTPTypes/HTTPField.swift @@ -63,7 +63,7 @@ public struct HTTPField: Sendable, Hashable { /// - Parameters: /// - name: The HTTP field name. /// - value: The HTTP field value. Invalid bytes are converted into space characters. - public init(name: Name, value: some Collection) { + public init(name: Name, value: C) where C.Element == UInt8 { self.name = name self.rawValue = Self.legalizeValue(ISOLatin1String(value)) } @@ -111,7 +111,7 @@ public struct HTTPField: Sendable, Hashable { var rawValue: ISOLatin1String - private static func _isValidValue(_ bytes: some Sequence) -> Bool { + private static func _isValidValue(_ bytes: S) -> Bool where S.Element == UInt8 { var iterator = bytes.makeIterator() guard var byte = iterator.next() else { // Empty string is allowed. @@ -178,7 +178,7 @@ public struct HTTPField: Sendable, Hashable { /// /// - Parameter value: The byte collection to validate. /// - Returns: Whether the byte collection is valid. - public static func isValidValue(_ value: some Collection) -> Bool { + public static func isValidValue(_ value: C) -> Bool where C.Element == UInt8 { self._isValidValue(value) } } @@ -227,7 +227,7 @@ extension HTTPField: Codable { } extension HTTPField { - static func isValidToken(_ token: some StringProtocol) -> Bool { + static func isValidToken(_ token: S) -> Bool { !token.isEmpty && token.utf8.allSatisfy { switch $0 { case 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2A, 0x2B, 0x2D, 0x2E, 0x5E, 0x5F, 0x60, 0x7C, 0x7E: diff --git a/Sources/HTTPTypes/HTTPFields.swift b/Sources/HTTPTypes/HTTPFields.swift index e371232..27839b6 100644 --- a/Sources/HTTPTypes/HTTPFields.swift +++ b/Sources/HTTPTypes/HTTPFields.swift @@ -216,35 +216,35 @@ public struct HTTPFields: Sendable, Hashable { } } - private func fields(for name: HTTPField.Name) -> some Sequence { - struct HTTPFieldSequence: Sequence { - let fields: [(field: HTTPField, next: UInt16)] - let index: UInt16 + private struct HTTPFieldSequence: Sequence { + let fields: [(field: HTTPField, next: UInt16)] + let index: UInt16 - struct Iterator: IteratorProtocol { - let fields: [(field: HTTPField, next: UInt16)] - var index: UInt16 + struct Iterator: IteratorProtocol { + let fields: [(field: HTTPField, next: UInt16)] + var index: UInt16 - mutating func next() -> HTTPField? { - if self.index == .max { - return nil - } - let (field, next) = self.fields[Int(self.index)] - self.index = next - return field + mutating func next() -> HTTPField? { + if self.index == .max { + return nil } + let (field, next) = self.fields[Int(self.index)] + self.index = next + return field } + } - func makeIterator() -> Iterator { - Iterator(fields: self.fields, index: self.index) - } + func makeIterator() -> Iterator { + Iterator(fields: self.fields, index: self.index) } + } + private func fields(for name: HTTPField.Name) -> HTTPFieldSequence { let index = self._storage.ensureIndex[name.canonicalName]?.first ?? .max return HTTPFieldSequence(fields: self._storage.fields, index: index) } - private mutating func setFields(_ fieldSequence: some Sequence, for name: HTTPField.Name) { + private mutating func setFields(_ fieldSequence: S, for name: HTTPField.Name) where S.Element == HTTPField { if !isKnownUniquelyReferenced(&self._storage) { self._storage = self._storage.copy() } @@ -384,7 +384,7 @@ extension HTTPFields: Codable { extension Array { // `removalIndices` must be ordered. - mutating func remove(at removalIndices: some Sequence) { + mutating func remove(at removalIndices: S) where S.Element == Index { var offset = 0 var iterator = removalIndices.makeIterator() var nextToRemoveOptional = iterator.next() diff --git a/Sources/HTTPTypes/ISOLatin1String.swift b/Sources/HTTPTypes/ISOLatin1String.swift index 696f6c2..c6919a6 100644 --- a/Sources/HTTPTypes/ISOLatin1String.swift +++ b/Sources/HTTPTypes/ISOLatin1String.swift @@ -21,7 +21,7 @@ extension String { struct ISOLatin1String: Sendable, Hashable { let _storage: String - private static func transcodeSlowPath(from bytes: some Collection) -> String { + private static func transcodeSlowPath(from bytes: C) -> String where C.Element == UInt8 { let scalars = bytes.lazy.map { UnicodeScalar(UInt32($0))! } var string = "" string.unicodeScalars.append(contentsOf: scalars) @@ -46,7 +46,7 @@ struct ISOLatin1String: Sendable, Hashable { } } - init(_ bytes: some Collection) { + init(_ bytes: C) where C.Element == UInt8 { let ascii = bytes.allSatisfy { $0 & 0x80 == 0 } if ascii { self._storage = String(decoding: bytes, as: UTF8.self) diff --git a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift index e8f3b88..4c5a89d 100644 --- a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift +++ b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift @@ -65,7 +65,7 @@ extension HTTPRequest { } extension URL { - fileprivate init?(scheme: some Collection, authority: some Collection, path: some Collection) { + fileprivate init?(scheme: C1, authority: C2, path: C3) where C1.Element == UInt8, C2.Element == UInt8, C3.Element == UInt8 { var buffer = [UInt8]() buffer.reserveCapacity(scheme.count + 3 + authority.count + path.count) buffer.append(contentsOf: scheme) diff --git a/Tests/HTTPTypesTests/HTTPTypesTests.swift b/Tests/HTTPTypesTests/HTTPTypesTests.swift index c5dc1cf..77efeb0 100644 --- a/Tests/HTTPTypesTests/HTTPTypesTests.swift +++ b/Tests/HTTPTypesTests/HTTPTypesTests.swift @@ -117,7 +117,7 @@ final class HTTPTypesTests: XCTestCase { } func testSendable() { - func isSendable(_ value: some Sendable) -> Bool { true } + func isSendable(_ value: T) -> Bool { true } func isSendable(_ value: Any) -> Bool { false } let field: HTTPField = .init(name: .userAgent, value: "") From fee45742962f41b636d17ab3db1895423ceafd0f Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Sat, 2 Dec 2023 13:03:51 -0800 Subject: [PATCH 3/3] Revert argument changes --- CONTRIBUTORS.txt | 2 ++ Sources/HTTPTypes/HTTPField.swift | 8 ++++---- Sources/HTTPTypes/HTTPFields.swift | 4 ++-- Sources/HTTPTypes/ISOLatin1String.swift | 4 ++-- Sources/HTTPTypesFoundation/HTTPRequest+URL.swift | 2 +- Tests/HTTPTypesTests/HTTPTypesTests.swift | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index b4883d0..98ab39b 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -13,8 +13,10 @@ needs to be listed here. - Franz Busch - Guoye Zhang +- Gwynne Raskind - Jager-yoo - Sergey Dmitriev +- Tim Condon <0xTim@users.noreply.github.com> - Tomohiro Kumagai **Updating this list** diff --git a/Sources/HTTPTypes/HTTPField.swift b/Sources/HTTPTypes/HTTPField.swift index 038cbc8..306600a 100644 --- a/Sources/HTTPTypes/HTTPField.swift +++ b/Sources/HTTPTypes/HTTPField.swift @@ -63,7 +63,7 @@ public struct HTTPField: Sendable, Hashable { /// - Parameters: /// - name: The HTTP field name. /// - value: The HTTP field value. Invalid bytes are converted into space characters. - public init(name: Name, value: C) where C.Element == UInt8 { + public init(name: Name, value: some Collection) { self.name = name self.rawValue = Self.legalizeValue(ISOLatin1String(value)) } @@ -111,7 +111,7 @@ public struct HTTPField: Sendable, Hashable { var rawValue: ISOLatin1String - private static func _isValidValue(_ bytes: S) -> Bool where S.Element == UInt8 { + private static func _isValidValue(_ bytes: some Sequence) -> Bool { var iterator = bytes.makeIterator() guard var byte = iterator.next() else { // Empty string is allowed. @@ -178,7 +178,7 @@ public struct HTTPField: Sendable, Hashable { /// /// - Parameter value: The byte collection to validate. /// - Returns: Whether the byte collection is valid. - public static func isValidValue(_ value: C) -> Bool where C.Element == UInt8 { + public static func isValidValue(_ value: some Collection) -> Bool { self._isValidValue(value) } } @@ -227,7 +227,7 @@ extension HTTPField: Codable { } extension HTTPField { - static func isValidToken(_ token: S) -> Bool { + static func isValidToken(_ token: some StringProtocol) -> Bool { !token.isEmpty && token.utf8.allSatisfy { switch $0 { case 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2A, 0x2B, 0x2D, 0x2E, 0x5E, 0x5F, 0x60, 0x7C, 0x7E: diff --git a/Sources/HTTPTypes/HTTPFields.swift b/Sources/HTTPTypes/HTTPFields.swift index 27839b6..e2f8d2d 100644 --- a/Sources/HTTPTypes/HTTPFields.swift +++ b/Sources/HTTPTypes/HTTPFields.swift @@ -244,7 +244,7 @@ public struct HTTPFields: Sendable, Hashable { return HTTPFieldSequence(fields: self._storage.fields, index: index) } - private mutating func setFields(_ fieldSequence: S, for name: HTTPField.Name) where S.Element == HTTPField { + private mutating func setFields(_ fieldSequence: some Sequence, for name: HTTPField.Name) { if !isKnownUniquelyReferenced(&self._storage) { self._storage = self._storage.copy() } @@ -384,7 +384,7 @@ extension HTTPFields: Codable { extension Array { // `removalIndices` must be ordered. - mutating func remove(at removalIndices: S) where S.Element == Index { + mutating func remove(at removalIndices: some Sequence) { var offset = 0 var iterator = removalIndices.makeIterator() var nextToRemoveOptional = iterator.next() diff --git a/Sources/HTTPTypes/ISOLatin1String.swift b/Sources/HTTPTypes/ISOLatin1String.swift index c6919a6..696f6c2 100644 --- a/Sources/HTTPTypes/ISOLatin1String.swift +++ b/Sources/HTTPTypes/ISOLatin1String.swift @@ -21,7 +21,7 @@ extension String { struct ISOLatin1String: Sendable, Hashable { let _storage: String - private static func transcodeSlowPath(from bytes: C) -> String where C.Element == UInt8 { + private static func transcodeSlowPath(from bytes: some Collection) -> String { let scalars = bytes.lazy.map { UnicodeScalar(UInt32($0))! } var string = "" string.unicodeScalars.append(contentsOf: scalars) @@ -46,7 +46,7 @@ struct ISOLatin1String: Sendable, Hashable { } } - init(_ bytes: C) where C.Element == UInt8 { + init(_ bytes: some Collection) { let ascii = bytes.allSatisfy { $0 & 0x80 == 0 } if ascii { self._storage = String(decoding: bytes, as: UTF8.self) diff --git a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift index 4c5a89d..e8f3b88 100644 --- a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift +++ b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift @@ -65,7 +65,7 @@ extension HTTPRequest { } extension URL { - fileprivate init?(scheme: C1, authority: C2, path: C3) where C1.Element == UInt8, C2.Element == UInt8, C3.Element == UInt8 { + fileprivate init?(scheme: some Collection, authority: some Collection, path: some Collection) { var buffer = [UInt8]() buffer.reserveCapacity(scheme.count + 3 + authority.count + path.count) buffer.append(contentsOf: scheme) diff --git a/Tests/HTTPTypesTests/HTTPTypesTests.swift b/Tests/HTTPTypesTests/HTTPTypesTests.swift index 77efeb0..c5dc1cf 100644 --- a/Tests/HTTPTypesTests/HTTPTypesTests.swift +++ b/Tests/HTTPTypesTests/HTTPTypesTests.swift @@ -117,7 +117,7 @@ final class HTTPTypesTests: XCTestCase { } func testSendable() { - func isSendable(_ value: T) -> Bool { true } + func isSendable(_ value: some Sendable) -> Bool { true } func isSendable(_ value: Any) -> Bool { false } let field: HTTPField = .init(name: .userAgent, value: "")