Skip to content

Commit 45f613f

Browse files
authored
Adopt new AsyncStreaming types (#37)
This PR updates the `HTTPRequestConcludingAsyncReader` and `HTTPResponseConcludingAsyncWriter` to use the new AsyncStreaming (reader and writer) APIs. These types are currently being developed in https://github.com/apple/swift-http-api-proposal but until that repository is public, we have to vendor in the code in this repo.
1 parent 4431697 commit 45f613f

30 files changed

+1720
-424
lines changed

Package.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PackageDescription
55
let extraSettings: [SwiftSetting] = [
66
.enableExperimentalFeature("SuppressedAssociatedTypes"),
77
.enableExperimentalFeature("LifetimeDependence"),
8+
.enableExperimentalFeature("Lifetimes"),
89
.enableUpcomingFeature("LifetimeDependence"),
910
.enableUpcomingFeature("NonisolatedNonsendingByDefault"),
1011
.enableUpcomingFeature("InferIsolatedConformances"),
@@ -21,7 +22,10 @@ let package = Package(
2122
targets: ["HTTPServer"])
2223
],
2324
dependencies: [
24-
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
25+
.package(
26+
url: "https://github.com/FranzBusch/swift-collections.git",
27+
branch: "fb-async"
28+
),
2529
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.0.0"),
2630
.package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "1.0.0"),
2731
.package(url: "https://github.com/apple/swift-certificates.git", from: "1.16.0"),
@@ -48,7 +52,9 @@ let package = Package(
4852
.target(
4953
name: "HTTPServer",
5054
dependencies: [
55+
"AsyncStreaming",
5156
.product(name: "DequeModule", package: "swift-collections"),
57+
.product(name: "BasicContainers", package: "swift-collections"),
5258
.product(name: "X509", package: "swift-certificates"),
5359
.product(name: "HTTPTypes", package: "swift-http-types"),
5460
.product(name: "NIOCore", package: "swift-nio"),
@@ -71,6 +77,13 @@ let package = Package(
7177
],
7278
swiftSettings: extraSettings
7379
),
80+
.target(
81+
name: "AsyncStreaming",
82+
dependencies: [
83+
.product(name: "BasicContainers", package: "swift-collections")
84+
],
85+
swiftSettings: extraSettings
86+
),
7487
.testTarget(
7588
name: "HTTPServerTests",
7689
dependencies: [
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift HTTP API Proposal open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift HTTP API Proposal project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift HTTP API Proposal project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
/// An enumeration that represents one of two possible error types.
16+
///
17+
/// ``EitherError`` provides a type-safe way to represent errors that can be one of two distinct
18+
/// error types.
19+
public enum EitherError<First: Error, Second: Error>: Error {
20+
/// An error of the first type.
21+
///
22+
/// The associated value contains the specific error instance of type `First`.
23+
case first(First)
24+
25+
/// An error of the second type.
26+
///
27+
/// The associated value contains the specific error instance of type `Second`.
28+
case second(Second)
29+
30+
/// Throws the underlying error by unwrapping this either error.
31+
///
32+
/// This method extracts and throws the actual error contained within the either error,
33+
/// whether it's the first or second type. This is useful when you need to propagate
34+
/// the original error without the either error wrapper.
35+
///
36+
/// - Throws: The underlying error, either of type `First` or `Second`.
37+
///
38+
/// ## Example
39+
///
40+
/// ```swift
41+
/// do {
42+
/// // Some operation that returns EitherError
43+
/// let result = try await operation()
44+
/// } catch let eitherError as EitherError<NetworkError, ParseError> {
45+
/// try eitherError.unwrap() // Throws the original error
46+
/// }
47+
/// ```
48+
public func unwrap() throws {
49+
switch self {
50+
case .first(let first):
51+
throw first
52+
case .second(let second):
53+
throw second
54+
}
55+
}
56+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift HTTP API Proposal open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift HTTP API Proposal project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift HTTP API Proposal project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
16+
extension InlineArray where Element: ~Copyable {
17+
package static func one(value: consuming Element) -> InlineArray<1, Element> {
18+
return InlineArray<1, Element>(first: value) { _ in fatalError() }
19+
}
20+
21+
package static func zero(of elementType: Element.Type = Element.self) -> InlineArray<0, Element> {
22+
return InlineArray<0, Element> { _ in }
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift HTTP API Proposal open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift HTTP API Proposal project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift HTTP API Proposal project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
extension Optional where Wrapped: ~Copyable {
16+
@inlinable
17+
mutating func takeSending() -> sending Self {
18+
let result = consume self
19+
self = nil
20+
return result
21+
}
22+
}

0 commit comments

Comments
 (0)