Skip to content

Commit 37ad908

Browse files
First pass at some documentation for AsyncBufferSequence (#90)
* First pass at some documentation for AsyncBufferSequence * Apply suggestions from code review Co-authored-by: Chris Adamson <[email protected]> Co-authored-by: Chris Adamson <[email protected]>
1 parent b8b0491 commit 37ad908

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Sources/AsyncAlgorithms/AsyncBufferSequence.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,33 @@ actor AsyncBufferState<Input: Sendable, Output: Sendable> {
123123
}
124124
}
125125

126+
/// An asynchronous buffer storage actor protocol used for buffering
127+
/// elements to an `AsyncBufferSequence`.
126128
@rethrows
127129
public protocol AsyncBuffer: Actor {
128130
associatedtype Input: Sendable
129131
associatedtype Output: Sendable
130132

133+
/// Push an element to enqueue to the buffer
131134
func push(_ element: Input) async
135+
136+
/// Pop an element from the buffer.
137+
///
138+
/// Implementors of `pop()` may throw. In cases where types
139+
/// throw from this function, that throwing behavior contributes to
140+
/// the rethrowing characteristics of `AsyncBufferSequence`.
132141
func pop() async throws -> Output?
133142
}
134143

144+
/// A buffer that limits pushed items by a certain count.
135145
public actor AsyncLimitBuffer<Element: Sendable>: AsyncBuffer {
146+
/// A policy for buffering elements to an `AsyncLimitBuffer`
136147
public enum Policy: Sendable {
148+
/// A policy for no bounding limit of pushed elements.
137149
case unbounded
150+
/// A policy for limiting to a specific number of oldest values.
138151
case bufferingOldest(Int)
152+
/// A policy for limiting to a specific number of newest values.
139153
case bufferingNewest(Int)
140154
}
141155

@@ -154,6 +168,7 @@ public actor AsyncLimitBuffer<Element: Sendable>: AsyncBuffer {
154168
self.policy = policy
155169
}
156170

171+
/// Push an element to enqueue to the buffer.
157172
public func push(_ element: Element) async {
158173
switch policy {
159174
case .unbounded:
@@ -174,6 +189,7 @@ public actor AsyncLimitBuffer<Element: Sendable>: AsyncBuffer {
174189
}
175190
}
176191

192+
/// Pop an element from the buffer.
177193
public func pop() async -> Element? {
178194
guard buffer.count > 0 else {
179195
return nil
@@ -183,17 +199,31 @@ public actor AsyncLimitBuffer<Element: Sendable>: AsyncBuffer {
183199
}
184200

185201
extension AsyncSequence where Element: Sendable {
202+
/// Creates an asynchronous sequence that buffers elements using a buffer created from a supplied closure.
203+
///
204+
/// Use the `buffer(_:)` method to account for `AsyncSequence` types that may produce elements faster
205+
/// than they are iterated. The `createBuffer` closure returns a backing buffer for storing elements and dealing with
206+
/// behavioral charcteristics of the `buffer(_:)` algorithm.
207+
///
208+
/// - Parameter createBuffer: A closure that constructs a new `AsyncBuffer` actor to store buffered values.
209+
/// - Returns: An asynchronous sequence that buffers elements using the specified `AsyncBuffer`.
186210
public func buffer<Buffer: AsyncBuffer>(_ createBuffer: @Sendable @escaping () -> Buffer) -> AsyncBufferSequence<Self, Buffer> where Buffer.Input == Element {
187211
AsyncBufferSequence(self, createBuffer: createBuffer)
188212
}
189213

214+
/// Creates an asynchronous sequence that buffers elements using a specific policy to limit the number of
215+
/// elements that are buffered.
216+
///
217+
/// - Parameter policy: A limiting policy behavior on the buffering behavior of the `AsyncBufferSequence`
218+
/// - Returns: An asynchronous sequence that buffers elements up to a given limit.
190219
public func buffer(policy limit: AsyncLimitBuffer<Element>.Policy) -> AsyncBufferSequence<Self, AsyncLimitBuffer<Element>> {
191220
buffer {
192221
AsyncLimitBuffer(policy: limit)
193222
}
194223
}
195224
}
196225

226+
/// An `AsyncSequence` that buffers elements utilizing an `AsyncBuffer`.
197227
public struct AsyncBufferSequence<Base: AsyncSequence, Buffer: AsyncBuffer> where Base.Element == Buffer.Input, Base.AsyncIterator: Sendable {
198228
let base: Base
199229
let createBuffer: @Sendable () -> Buffer
@@ -210,6 +240,7 @@ extension AsyncBufferSequence.Iterator: Sendable where Base: Sendable, Base.Asyn
210240
extension AsyncBufferSequence: AsyncSequence {
211241
public typealias Element = Buffer.Output
212242

243+
/// The iterator for a `AsyncBufferSequence` instance.
213244
public struct Iterator: AsyncIteratorProtocol {
214245
struct Active {
215246
var task: Task<Void, Never>?

0 commit comments

Comments
 (0)