Skip to content

Add variants of flatMap which can return EventLoopFuture<NonSendable> - #3634

Merged
Lukasa merged 2 commits into
apple:mainfrom
hamzahrmalik:flatMapIsolated
Jul 20, 2026
Merged

Lukasa merged 2 commits into
apple:mainfrom
hamzahrmalik:flatMapIsolated

Conversation

@hamzahrmalik

Copy link
Copy Markdown
Contributor

Motivation:

Currently, EventLoopFuture.flatMap requires the inner eventloop to have a Sendable type. This makes sense, because the value might move between eventloops, if the inner and outer future are not on the same EventLoop. However, this makes it impossible for users to use non Sendable values even when they know they're on the right eventloop

Modifications:

Add a new flatMapIsolated function which can be used in this case. It requires users to return an EventLoopFuture.Isolated to prove that they are on the same eventloop

Example:

class T { // Not Sendable
    init() {

    }
}

func test(eventLoop: any EventLoop) -> EventLoopFuture<T> {
    let a = eventLoop.submit { T() }
    let b = eventLoop.submit { T() }
    return a.flatMap { _ in b }
}

The above example gives a warning on the final line, because flatMap requires the inner future (b) to have a Sendable type. That is because it doesn't know that a and b are on the same EventLoop.

func test(eventLoop: any EventLoop) -> EventLoopFuture<T> {
    let a = eventLoop.submit { T() }
    let b = eventLoop.submit { T() }
    return a.flatMapIsolated { _ in b.assumeIsolated() }
}

Here, it compiles without warnings. We can write assumeIsolated inside the flatMapIsolated closure because we know that the eventloops are the same. And therefore the new flatMapIsolated function does not have a Sendable requirement on its value

@FranzBusch FranzBusch left a comment

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.

LGTM. @Lukasa do you want to take a look as well?

@Lukasa Lukasa added the πŸ†• semver/minor Adds new public API. label Jul 20, 2026

@Lukasa Lukasa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice change, thanks!

@Lukasa
Lukasa enabled auto-merge (squash) July 20, 2026 09:52
@Lukasa
Lukasa merged commit 199ccb9 into apple:main Jul 20, 2026
52 of 57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

πŸ†• semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants