-
Notifications
You must be signed in to change notification settings - Fork 125
Make HTTPClientResponse.init
public
#632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bbbda70
Make `HTTPClientResponse.init` public
dnadoba 13e03cd
Align `AsyncSequenceFromSyncSequence` with `swift-async-algorithms`
dnadoba 7dfed60
Address review comments
dnadoba c2ffb93
SwiftFormat
dnadoba 09a5977
Rename file to match type name
dnadoba c04f621
Fix review comments
dnadoba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2022 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@usableFromInline | ||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
struct AnyAsyncSequence<Element>: Sendable, AsyncSequence { | ||
@usableFromInline typealias AsyncIteratorNextCallback = () async throws -> Element? | ||
|
||
@usableFromInline struct AsyncIterator: AsyncIteratorProtocol { | ||
@usableFromInline let nextCallback: AsyncIteratorNextCallback | ||
|
||
@inlinable init(nextCallback: @escaping AsyncIteratorNextCallback) { | ||
self.nextCallback = nextCallback | ||
} | ||
|
||
@inlinable mutating func next() async throws -> Element? { | ||
try await self.nextCallback() | ||
} | ||
} | ||
|
||
@usableFromInline var makeAsyncIteratorCallback: @Sendable () -> AsyncIteratorNextCallback | ||
|
||
@inlinable init<SequenceOfBytes>( | ||
_ asyncSequence: SequenceOfBytes | ||
) where SequenceOfBytes: AsyncSequence & Sendable, SequenceOfBytes.Element == Element { | ||
self.makeAsyncIteratorCallback = { | ||
var iterator = asyncSequence.makeAsyncIterator() | ||
return { | ||
try await iterator.next() | ||
} | ||
} | ||
} | ||
|
||
@inlinable func makeAsyncIterator() -> AsyncIterator { | ||
.init(nextCallback: self.makeAsyncIteratorCallback()) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Sources/AsyncHTTPClient/AsyncAwait/AsyncSequenceFromSyncSequence.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2022 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
@usableFromInline | ||
struct AsyncLazySequence<Base: Sequence>: AsyncSequence { | ||
@usableFromInline typealias Element = Base.Element | ||
@usableFromInline struct AsyncIterator: AsyncIteratorProtocol { | ||
@usableFromInline var iterator: Base.Iterator | ||
@inlinable init(iterator: Base.Iterator) { | ||
self.iterator = iterator | ||
} | ||
|
||
@inlinable mutating func next() async throws -> Base.Element? { | ||
self.iterator.next() | ||
} | ||
} | ||
|
||
@usableFromInline var base: Base | ||
|
||
@inlinable init(base: Base) { | ||
self.base = base | ||
} | ||
|
||
@inlinable func makeAsyncIterator() -> AsyncIterator { | ||
.init(iterator: self.base.makeIterator()) | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
extension AsyncLazySequence: Sendable where Base: Sendable {} | ||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
extension AsyncLazySequence.AsyncIterator: Sendable where Base.Iterator: Sendable {} | ||
|
||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
extension Sequence { | ||
/// Turns `self` into an `AsyncSequence` by vending each element of `self` asynchronously. | ||
@inlinable var async: AsyncLazySequence<Self> { | ||
.init(base: self) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
Sources/AsyncHTTPClient/AsyncAwait/SingleIteratorPrecondition.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2022 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Atomics | ||
|
||
/// Makes sure that a consumer of this `AsyncSequence` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure they what? |
||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
@usableFromInline struct SingleIteratorPrecondition<Base: AsyncSequence>: AsyncSequence { | ||
@usableFromInline let base: Base | ||
@usableFromInline let didCreateIterator: ManagedAtomic<Bool> = .init(false) | ||
@usableFromInline typealias Element = Base.Element | ||
@inlinable init(base: Base) { | ||
self.base = base | ||
} | ||
|
||
@inlinable func makeAsyncIterator() -> Base.AsyncIterator { | ||
precondition( | ||
self.didCreateIterator.exchange(true, ordering: .relaxed) == false | ||
) | ||
return self.base.makeAsyncIterator() | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
extension SingleIteratorPrecondition: @unchecked Sendable where Base: Sendable {} | ||
|
||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
extension AsyncSequence { | ||
@inlinable var singleIteratorPrecondition: SingleIteratorPrecondition<Self> { | ||
.init(base: self) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This modifier already exists, doesn't it? I see it is local here, but I assume that we want to make it public eventually...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to make it public. It exists in
swift-async-algorithms
. However, it doesn't yet have a 1.x release.