Skip to content

Adds a new AsyncReader.collect method#415

Merged
FranzBusch merged 1 commit into
apple:mainfrom
FranzBusch:fb-collect
May 12, 2026
Merged

Adds a new AsyncReader.collect method#415
FranzBusch merged 1 commit into
apple:mainfrom
FranzBusch:fb-collect

Conversation

@FranzBusch
Copy link
Copy Markdown
Member

Often developers want to collect elements from a reader up to a certain limit. This PR provides a few convenience methods for this.

Copy link
Copy Markdown
Member

@Catfish-Man Catfish-Man left a comment

Choose a reason for hiding this comment

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

Looks good

/// with caution on large datasets. The `limit` parameter serves as a safety mechanism
/// to prevent excessive memory usage.
public mutating func collect<Result, Failure: Error>(
upTo limit: Int,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I could see an argument that this might want a default parameter, but I don't think that's critical to decide now

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We had a lot of arguments in the HTTP APIs around this. Our main argument against a default parameter is that it is impossible to pick the default. The only default to pick is Int.max but that makes this vulnerable to remote denial of service attacks if the attack can tricker an essentially unbounded memory collection

Comment thread Sources/AsyncStreaming/AsyncReader/AsyncReader+collect.swift Outdated
Often developers want to collect elements from a reader up to a certain limit. This PR provides a few convenience methods for this.
@FranzBusch FranzBusch merged commit d14a83b into apple:main May 12, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants