Files
swift-nio/Sources
hamzahrmalikandCory Benfield 199ccb983c Add variants of flatMap which can return EventLoopFuture<NonSendable> (#3634)
### 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:

```swift
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.

```swift
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

Co-authored-by: Cory Benfield <lukasa@apple.com>
2026-07-20 11:15:50 +00:00
..
2024-09-12 13:18:25 +01:00
2026-07-07 15:46:28 +01:00
2026-07-07 15:46:28 +01:00
2025-10-06 10:58:33 +01:00
2025-05-07 12:24:44 +00:00
2025-05-07 12:24:44 +00:00