Skip to content

[Concurrency] Revise Async- related files doc #58930

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 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions stdlib/public/Concurrency/AsyncCompactMapSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ extension AsyncSequence {
/// returns `nil` in this case, which `compactMap(_:)` omits from the
/// transformed asynchronous sequence.
///
/// let romanNumeralDict: [Int : String] =
/// let romanNumeralDict: [Int: String] =
/// [1: "I", 2: "II", 3: "III", 5: "V"]
///
/// let stream = Counter(howHigh: 5)
/// .compactMap { romanNumeralDict[$0] }
/// for await numeral in stream {
/// print("\(numeral) ", terminator: " ")
/// print(numeral, terminator: " ")
/// }
/// // Prints: I II III V
/// // Prints "I II III V"
///
/// - Parameter transform: A mapping closure. `transform` accepts an element
/// of this sequence as its parameter and returns a transformed value of the
Expand Down Expand Up @@ -141,4 +141,4 @@ extension AsyncCompactMapSequence: @unchecked Sendable
extension AsyncCompactMapSequence.Iterator: @unchecked Sendable
where Base.AsyncIterator: Sendable,
Base.Element: Sendable,
ElementOfResult: Sendable { }
ElementOfResult: Sendable { }
8 changes: 4 additions & 4 deletions stdlib/public/Concurrency/AsyncDropFirstSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ extension AsyncSequence {
///
/// In this example, an asynchronous sequence called `Counter` produces `Int`
/// values from `1` to `10`. The `dropFirst(_:)` method causes the modified
/// sequence to ignore the values `0` through `4`, and instead emit `5` through `10`:
/// sequence to ignore the values `1` through `3`, and instead emit `4` through `10`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. This way matches the actual output on line 30.

///
/// for await number in Counter(howHigh: 10).dropFirst(3) {
/// print("\(number) ", terminator: " ")
/// print(number, terminator: " ")
/// }
/// // prints "4 5 6 7 8 9 10"
/// // Prints "4 5 6 7 8 9 10"
///
/// If the number of elements to drop exceeds the number of elements in the
/// sequence, the result is an empty sequence.
Expand Down Expand Up @@ -144,4 +144,4 @@ extension AsyncDropFirstSequence: Sendable
@available(SwiftStdlib 5.1, *)
extension AsyncDropFirstSequence.Iterator: Sendable
where Base.AsyncIterator: Sendable,
Base.Element: Sendable { }
Base.Element: Sendable { }
4 changes: 2 additions & 2 deletions stdlib/public/Concurrency/AsyncDropWhileSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ extension AsyncSequence {
/// let stream = Counter(howHigh: 10)
/// .drop { $0 % 3 != 0 }
/// for await number in stream {
/// print("\(number) ", terminator: " ")
/// print(number, terminator: " ")
/// }
/// // prints "3 4 5 6 7 8 9 10"
/// // Prints "3 4 5 6 7 8 9 10"
///
/// After the predicate returns `false`, the sequence never executes it again,
/// and from then on the sequence passes through elements from its underlying
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Concurrency/AsyncFilterSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ extension AsyncSequence {
/// let stream = Counter(howHigh: 10)
/// .filter { $0 % 2 == 0 }
/// for await number in stream {
/// print("\(number) ", terminator: " ")
/// print(number, terminator: " ")
/// }
/// // Prints: 2 4 6 8 10
/// // Prints "2 4 6 8 10"
///
/// - Parameter isIncluded: A closure that takes an element of the
/// asynchronous sequence as its argument and returns a Boolean value
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Concurrency/AsyncFlatMapSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ extension AsyncSequence {
/// let stream = Counter(howHigh: 5)
/// .flatMap { Counter(howHigh: $0) }
/// for await number in stream {
/// print("\(number)", terminator: " ")
/// print(number, terminator: " ")
/// }
/// // Prints: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
/// // Prints "1 1 2 1 2 3 1 2 3 4 1 2 3 4 5"
///
/// - Parameter transform: A mapping closure. `transform` accepts an element
/// of this sequence as its parameter and returns an `AsyncSequence`.
Expand Down
7 changes: 4 additions & 3 deletions stdlib/public/Concurrency/AsyncIteratorProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import Swift
/// asynchronous, it shows the shape of a custom sequence and iterator, and how
/// to use it as if it were asynchronous:
///
/// struct Counter : AsyncSequence {
/// struct Counter: AsyncSequence {
/// typealias Element = Int
/// let howHigh: Int
///
/// struct AsyncIterator : AsyncIteratorProtocol {
/// struct AsyncIterator: AsyncIteratorProtocol {
/// let howHigh: Int
/// var current = 1
///
/// mutating func next() async -> Int? {
/// // A genuinely asynchronous implementation uses the `Task`
/// // API to check for cancellation here and return early.
Expand All @@ -59,7 +60,7 @@ import Swift
/// for await i in Counter(howHigh: 10) {
/// print(i, terminator: " ")
/// }
/// // Prints: 1 2 3 4 5 6 7 8 9 10
/// // Prints "1 2 3 4 5 6 7 8 9 10"
///
/// ### End of Iteration
///
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Concurrency/AsyncMapSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ extension AsyncSequence {
/// let stream = Counter(howHigh: 5)
/// .map { romanNumeralDict[$0] ?? "(unknown)" }
/// for await numeral in stream {
/// print("\(numeral) ", terminator: " ")
/// print(numeral, terminator: " ")
/// }
/// // Prints: I II III (unknown) V
/// // Prints "I II III (unknown) V"
///
/// - Parameter transform: A mapping closure. `transform` accepts an element
/// of this sequence as its parameter and returns a transformed value of the
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Concurrency/AsyncPrefixSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ extension AsyncSequence {
/// sequence to pass through the first six values, then end.
///
/// for await number in Counter(howHigh: 10).prefix(6) {
/// print("\(number) ")
/// print(number, terminator: " ")
/// }
/// // prints "1 2 3 4 5 6"
/// // Prints "1 2 3 4 5 6"
///
/// If the count passed to `prefix(_:)` exceeds the number of elements in the
/// base sequence, the result contains all of the elements in the sequence.
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Concurrency/AsyncPrefixWhileSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ extension AsyncSequence {
/// let stream = Counter(howHigh: 10)
/// .prefix { $0 % 2 != 0 || $0 % 3 != 0 }
/// for try await number in stream {
/// print("\(number) ", terminator: " ")
/// print(number, terminator: " ")
/// }
/// // prints "1 2 3 4 5"
/// // Prints "1 2 3 4 5"
///
/// - Parameter predicate: A closure that takes an element as a parameter and
/// returns a Boolean value indicating whether the element should be
Expand Down
22 changes: 11 additions & 11 deletions stdlib/public/Concurrency/AsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Swift
/// for await i in Counter(howHigh: 10) {
/// print(i, terminator: " ")
/// }
/// // Prints: 1 2 3 4 5 6 7 8 9 10
/// // Prints "1 2 3 4 5 6 7 8 9 10"
///
/// An `AsyncSequence` doesn't generate or contain the values; it just defines
/// how you access them. Along with defining the type of values as an associated
Expand Down Expand Up @@ -69,7 +69,7 @@ import Swift
/// for await s in stream {
/// print(s, terminator: " ")
/// }
/// // Prints: Odd Even Odd Even Odd Even Odd Even Odd Even
/// // Prints "Odd Even Odd Even Odd Even Odd Even Odd Even"
///
@available(SwiftStdlib 5.1, *)
@rethrows
Expand Down Expand Up @@ -108,7 +108,7 @@ extension AsyncSequence {
/// $0 + $1
/// }
/// print(sum)
/// // Prints: 10
/// // Prints "10"
///
///
/// - Parameters:
Expand Down Expand Up @@ -204,7 +204,7 @@ extension AsyncSequence {
/// let containsDivisibleByThree = await Counter(howHigh: 10)
/// .contains { $0 % 3 == 0 }
/// print(containsDivisibleByThree)
/// // Prints: true
/// // Prints "true"
///
/// The predicate executes each time the asynchronous sequence produces an
/// element, until either the predicate finds a match or the sequence ends.
Expand All @@ -231,7 +231,7 @@ extension AsyncSequence {
/// let allLessThanTen = await Counter(howHigh: 10)
/// .allSatisfy { $0 < 10 }
/// print(allLessThanTen)
/// // Prints: false
/// // Prints "false"
///
/// The predicate executes each time the asynchronous sequence produces an
/// element, until either the predicate returns `false` or the sequence ends.
Expand Down Expand Up @@ -263,7 +263,7 @@ extension AsyncSequence where Element: Equatable {
/// let containsFive = await Counter(howHigh: 10)
/// .contains(5)
/// print(containsFive)
/// // Prints: true
/// // Prints "true"
///
/// - Parameter search: The element to find in the asynchronous sequence.
/// - Returns: `true` if the method found the element in the asynchronous
Expand Down Expand Up @@ -306,7 +306,7 @@ extension AsyncSequence {
/// let divisibleBy2And3 = await Counter(howHigh: 10)
/// .first { $0 % 2 == 0 && $0 % 3 == 0 }
/// print(divisibleBy2And3 ?? "none")
/// // Prints: 6
/// // Prints "6"
///
/// The predicate executes each time the asynchronous sequence produces an
/// element, until either the predicate finds a match or the sequence ends.
Expand Down Expand Up @@ -357,7 +357,7 @@ extension AsyncSequence {
/// let min = await RankCounter()
/// .min { $0.rawValue < $1.rawValue }
/// print(min ?? "none")
/// // Prints: ace
/// // Prints "ace"
///
/// - Parameter areInIncreasingOrder: A predicate that returns `true` if its
/// first argument should be ordered before its second argument; otherwise,
Expand Down Expand Up @@ -412,7 +412,7 @@ extension AsyncSequence {
/// let max = await RankCounter()
/// .max { $0.rawValue < $1.rawValue }
/// print(max ?? "none")
/// // Prints: king
/// // Prints "king"
///
/// - Parameter areInIncreasingOrder: A predicate that returns `true` if its
/// first argument should be ordered before its second argument; otherwise,
Expand Down Expand Up @@ -449,7 +449,7 @@ extension AsyncSequence where Element: Comparable {
/// let min = await Counter(howHigh: 10)
/// .min()
/// print(min ?? "none")
/// // Prints: 1
/// // Prints "1"
///
/// - Returns: The sequence’s minimum element. If the sequence has no
/// elements, returns `nil`.
Expand All @@ -469,7 +469,7 @@ extension AsyncSequence where Element: Comparable {
/// let max = await Counter(howHigh: 10)
/// .max()
/// print(max ?? "none")
/// // Prints: 10
/// // Prints "10"
///
/// - Returns: The sequence’s maximum element. If the sequence has no
/// elements, returns `nil`.
Expand Down
28 changes: 13 additions & 15 deletions stdlib/public/Concurrency/AsyncStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ import Swift
/// produces it:
///
/// for await quake in QuakeMonitor.quakes {
/// print ("Quake: \(quake.date)")
/// print("Quake: \(quake.date)")
/// }
/// print ("Stream finished.")
/// print("Stream finished.")
///
@available(SwiftStdlib 5.1, *)
public struct AsyncStream<Element> {
Expand Down Expand Up @@ -272,18 +272,18 @@ public struct AsyncStream<Element> {
///
/// let stream = AsyncStream<Int>(Int.self,
/// bufferingPolicy: .bufferingNewest(5)) { continuation in
/// Task.detached {
/// for _ in 0..<100 {
/// await Task.sleep(1 * 1_000_000_000)
/// continuation.yield(Int.random(in: 1...10))
/// }
/// continuation.finish()
/// Task.detached {
/// for _ in 0..<100 {
/// await Task.sleep(1 * 1_000_000_000)
/// continuation.yield(Int.random(in: 1...10))
/// }
/// continuation.finish()
/// }
/// }
///
/// // Call point:
/// for await random in stream {
/// print ("\(random)")
/// print(random)
/// }
///
public init(
Expand Down Expand Up @@ -319,15 +319,13 @@ public struct AsyncStream<Element> {
/// the `unfolding` parameter label.
///
/// let stream = AsyncStream<Int> {
/// await Task.sleep(1 * 1_000_000_000)
/// return Int.random(in: 1...10)
/// }
/// onCancel: { @Sendable () in print ("Canceled.") }
/// )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job catching this stray ) that shouldn't be there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 😄
Note that I also edited the word "Canceled." → "Cancelled." (double l)

Copy link
Member

@amartini51 amartini51 May 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Canceled" was correct — please change this back.

Documentation follows Apple Style Guide, which calls for spelling it "canceled" and "canceling". The API naming guidelines for Cocoa (which Swift often follows) follow historical precedent of other API names, and spells it "cancelled" and "cancelling".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amartini51 Thanks for following up!
The Apple Style Guide - C definitely says that we should use one l for "canceled".

On lines 121-122 in the very same file, however, there is case cancelled in use.

/// The stream finished as a result of cancellation.
case cancelled

Actually, "cancelled" is being used much more than "canceled" in overall Swift repo. 🧐
Would it be better to just revert this word for now? How do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert this change please, so we're not making the problem worse. Someone (possibly me or @invalidname) can take another pass over the file to correct the other "cancelled" spellings in a different PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding lines 121-121, case cancelled is an example of the Cocoa naming guidelines I mentioned. You can see this style difference in API reference for Task and and related types — symbols with "cancelled" in its name have "canceled" in their docs.

Both style guides use the spelling "cancellation".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, now I see. Documentation follows "canceled", while the API naming follows "cancelled".
Sorry for additional revert commit, and thank you so much for being part of this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading my original post, I could have been clearer that there were two style guides involved here, one for code and one for docs. Sorry about that. Thanks for the fixes in this PR!

/// await Task.sleep(1 * 1_000_000_000)
/// return Int.random(in: 1...10)
/// } onCancel: { @Sendable () in print("Canceled.") }
///
/// // Call point:
/// for await random in stream {
/// print ("\(random)")
/// print(random)
/// }
///
///
Expand Down
1 change: 0 additions & 1 deletion stdlib/public/Concurrency/AsyncStreamBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,3 @@ final class _AsyncStreamCriticalStorage<Contents>: @unchecked Sendable {
return storage
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension AsyncSequence {
/// transformed asynchronous sequence. When the value is `5`, the closure
/// throws `MyError`, terminating the sequence.
///
/// let romanNumeralDict: [Int : String] =
/// let romanNumeralDict: [Int: String] =
/// [1: "I", 2: "II", 3: "III", 5: "V"]
///
/// do {
Expand All @@ -42,12 +42,12 @@ extension AsyncSequence {
/// return romanNumeralDict[value]
/// }
/// for try await numeral in stream {
/// print("\(numeral) ", terminator: " ")
/// print(numeral, terminator: " ")
/// }
/// } catch {
/// print("Error: \(error)")
/// }
/// // Prints: I II III Error: MyError()
/// // Prints "I II III Error: MyError()"
///
/// - Parameter transform: An error-throwing mapping closure. `transform`
/// accepts an element of this sequence as its parameter and returns a
Expand Down Expand Up @@ -162,4 +162,3 @@ extension AsyncThrowingCompactMapSequence: @unchecked Sendable
extension AsyncThrowingCompactMapSequence.Iterator: @unchecked Sendable
where Base.AsyncIterator: Sendable,
Base.Element: Sendable { }

Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ extension AsyncSequence {
/// throws without ever printing anything.
///
/// do {
/// let stream = Counter(howHigh: 10)
/// let stream = Counter(howHigh: 10)
/// .drop {
/// if $0 % 2 == 0 {
/// throw EvenError()
/// }
/// return $0 < 5
/// }
/// for try await number in stream {
/// print("\(number) ")
/// print(number)
/// }
/// } catch {
/// print ("\(error)")
/// print(error)
/// }
/// // Prints: EvenError()
/// // Prints "EvenError()"
///
/// After the predicate returns `false`, the sequence never executes it again,
/// and from then on the sequence passes through elements from its underlying
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/Concurrency/AsyncThrowingFilterSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ extension AsyncSequence {
/// but also throws an error for values divisible by 5:
///
/// do {
/// let stream = Counter(howHigh: 10)
/// let stream = Counter(howHigh: 10)
/// .filter {
/// if $0 % 5 == 0 {
/// throw MyError()
/// }
/// return $0 % 2 == 0
/// }
/// for try await number in stream {
/// print("\(number) ", terminator: " ")
/// print(number, terminator: " ")
/// }
/// } catch {
/// print("Error: \(error)")
/// }
/// // Prints: 2 4 Error: MyError()
/// // Prints "2 4 Error: MyError()"
///
/// - Parameter isIncluded: An error-throwing closure that takes an element
/// of the asynchronous sequence as its argument and returns a Boolean value
Expand Down
Loading