forked from apple/swift-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopFuture+AssumeIsolated.swift
More file actions
754 lines (711 loc) · 33.1 KB
/
EventLoopFuture+AssumeIsolated.swift
File metadata and controls
754 lines (711 loc) · 33.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// A struct wrapping an ``EventLoop`` that ensures all calls to any method on this struct
/// are coming from the event loop.
///
/// This type is explicitly not `Sendable`. It may only be constructed on an event loop,
/// using ``EventLoop/assumeIsolated()``, and may not subsequently be passed to other isolation
/// domains.
///
/// Using this type relaxes the need to have the closures for ``EventLoop/execute(_:)``,
/// ``EventLoop/submit(_:)``, and ``EventLoop/scheduleTask(in:_:)`` to be `@Sendable`.
public struct NIOIsolatedEventLoop {
@usableFromInline
let _wrapped: EventLoop
@inlinable
internal init(_ eventLoop: EventLoop) {
self._wrapped = eventLoop
}
/// Submit a given task to be executed by the `EventLoop`
@available(*, noasync)
@inlinable
public func execute(_ task: @escaping () -> Void) {
self._wrapped._executeIsolatedUnsafeUnchecked(task)
}
/// Submit a given task to be executed by the `EventLoop`. Once the execution is complete the returned `EventLoopFuture` is notified.
///
/// - Parameters:
/// - task: The closure that will be submitted to the `EventLoop` for execution.
/// - Returns: `EventLoopFuture` that is notified once the task was executed.
@available(*, noasync)
@inlinable
public func submit<T>(_ task: @escaping () throws -> T) -> EventLoopFuture<T> {
self._wrapped._submitIsolatedUnsafeUnchecked(task)
}
/// Schedule a `task` that is executed by this `EventLoop` at the given time.
///
/// - Parameters:
/// - deadline: The time at which the task should run.
/// - task: The synchronous task to run. As with everything that runs on the `EventLoop`, it must not block.
/// - Returns: A `Scheduled` object which may be used to cancel the task if it has not yet run, or to wait
/// on the completion of the task.
///
/// - Note: You can only cancel a task before it has started executing.
@discardableResult
@available(*, noasync)
@inlinable
public func scheduleTask<T>(
deadline: NIODeadline,
_ task: @escaping () throws -> T
) -> Scheduled<T> {
self._wrapped._scheduleTaskIsolatedUnsafeUnchecked(deadline: deadline, task)
}
/// Schedule a `task` that is executed by this `EventLoop` after the given amount of time.
///
/// - Parameters:
/// - delay: The time to wait before running the task.
/// - task: The synchronous task to run. As with everything that runs on the `EventLoop`, it must not block.
/// - Returns: A `Scheduled` object which may be used to cancel the task if it has not yet run, or to wait
/// on the completion of the task.
///
/// - Note: You can only cancel a task before it has started executing.
/// - Note: The `in` value is clamped to a maximum when running on a Darwin-kernel.
@discardableResult
@available(*, noasync)
@inlinable
public func scheduleTask<T>(
in delay: TimeAmount,
_ task: @escaping () throws -> T
) -> Scheduled<T> {
self._wrapped._scheduleTaskIsolatedUnsafeUnchecked(in: delay, task)
}
/// Schedule a `task` that is executed by this `EventLoop` at the given time.
///
/// - Note: The `T` must be `Sendable` since the isolation domains of the event loop future returned from `task` and
/// this event loop might differ.
///
/// - Parameters:
/// - deadline: The time at which we should run the asynchronous task.
/// - file: The file in which the task is scheduled.
/// - line: The line of the `file` in which the task is scheduled.
/// - task: The asynchronous task to run. As with everything that runs on the `EventLoop`, it must not block.
/// - Returns: A `Scheduled` object which may be used to cancel the task if it has not yet run, or to wait
/// on the full execution of the task, including its returned `EventLoopFuture`.
///
/// - Note: You can only cancel a task before it has started executing.
@discardableResult
@available(*, noasync)
@inlinable
public func flatScheduleTask<T: Sendable>(
deadline: NIODeadline,
file: StaticString = #file,
line: UInt = #line,
_ task: @escaping () throws -> EventLoopFuture<T>
) -> Scheduled<T> {
let promise: EventLoopPromise<T> = self._wrapped.makePromise(file: file, line: line)
let scheduled = self.scheduleTask(deadline: deadline, task)
scheduled.futureResult.whenComplete { result in
switch result {
case .success(let futureResult):
promise.completeWith(futureResult)
case .failure(let error):
promise.fail(error)
}
}
return .init(promise: promise, cancellationTask: { scheduled.cancel() })
}
/// Returns the wrapped event loop.
@inlinable
public func nonisolated() -> any EventLoop {
self._wrapped
}
}
extension EventLoop {
/// Assumes the calling context is isolated to the event loop.
@inlinable
@available(*, noasync)
public func assumeIsolated() -> NIOIsolatedEventLoop {
self.preconditionInEventLoop()
return NIOIsolatedEventLoop(self)
}
/// Assumes the calling context is isolated to the event loop.
///
/// This version of ``EventLoop/assumeIsolated()`` omits the runtime
/// isolation check in release builds and doesn't prevent you using it
/// from using it in async contexts.
@inlinable
public func assumeIsolatedUnsafeUnchecked() -> NIOIsolatedEventLoop {
self.assertInEventLoop()
return NIOIsolatedEventLoop(self)
}
}
@available(*, unavailable)
extension NIOIsolatedEventLoop: Sendable {}
extension EventLoopFuture {
/// A struct wrapping an ``EventLoopFuture`` that ensures all calls to any method on this struct
/// are coming from the event loop of the future.
///
/// This type is explicitly not `Sendable`. It may only be constructed on an event loop,
/// using ``EventLoopFuture/assumeIsolated()``, and may not subsequently be passed to other isolation
/// domains.
///
/// Using this type relaxes the need to have the closures for the various ``EventLoopFuture``
/// callback-attaching functions be `Sendable`.
public struct Isolated {
@usableFromInline
let _wrapped: EventLoopFuture<Value>
@inlinable
init(_wrapped: EventLoopFuture<Value>) {
self._wrapped = _wrapped
}
/// When the current `EventLoopFuture<Value>` is fulfilled, run the provided callback,
/// which will provide a new `EventLoopFuture`.
///
/// This allows you to dynamically dispatch new asynchronous tasks as phases in a
/// longer series of processing steps. Note that you can use the results of the
/// current `EventLoopFuture<Value>` when determining how to dispatch the next operation.
///
/// This works well when you have APIs that already know how to return `EventLoopFuture`s.
/// You can do something with the result of one and just return the next future:
///
/// ```
/// let d1 = networkRequest(args).future()
/// let d2 = d1.flatMap { t -> EventLoopFuture<NewValue> in
/// . . . something with t . . .
/// return netWorkRequest(args)
/// }
/// d2.whenSuccess { u in
/// NSLog("Result of second request: \(u)")
/// }
/// ```
///
/// Note that the returned ``EventLoopFuture`` still needs a `Sendable` wrapped value,
/// as it may have been created on a different event loop.
///
/// Note: In a sense, the `EventLoopFuture<NewValue>` is returned before it's created.
///
/// - Parameters:
/// - callback: Function that will receive the value of this `EventLoopFuture` and return
/// a new `EventLoopFuture`.
/// - Returns: A future that will receive the eventual value.
@inlinable
@available(*, noasync)
public func flatMap<NewValue: Sendable>(
_ callback: @escaping (Value) -> EventLoopFuture<NewValue>
) -> EventLoopFuture<NewValue>.Isolated {
let next = EventLoopPromise<NewValue>.makeUnleakablePromise(eventLoop: self._wrapped.eventLoop)
let base = self._wrapped
base._whenCompleteIsolated {
switch base._value! {
case .success(let t):
let futureU = callback(t)
if futureU.eventLoop.inEventLoop {
return futureU._addCallback {
next._setValue(value: futureU._value!)
}
} else {
futureU.cascade(to: next)
return CallbackList()
}
case .failure(let error):
return next._setValue(value: .failure(error))
}
}
return next.futureResult.assumeIsolatedUnsafeUnchecked()
}
/// When the current `EventLoopFuture<Value>` is fulfilled, run the provided callback, which
/// performs a synchronous computation and returns a new value of type `NewValue`. The provided
/// callback may optionally `throw`.
///
/// Operations performed in `flatMapThrowing` should not block, or they will block the entire
/// event loop. `flatMapThrowing` is intended for use when you have a data-driven function that
/// performs a simple data transformation that can potentially error.
///
/// If your callback function throws, the returned `EventLoopFuture` will error.
///
/// - Parameters:
/// - callback: Function that will receive the value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - Returns: A future that will receive the eventual value.
@inlinable
@available(*, noasync)
public func flatMapThrowing<NewValue>(
_ callback: @escaping (Value) throws -> NewValue
) -> EventLoopFuture<NewValue>.Isolated {
let next = EventLoopPromise<NewValue>.makeUnleakablePromise(eventLoop: self._wrapped.eventLoop)
let base = self._wrapped
base._whenCompleteIsolated {
switch base._value! {
case .success(let t):
do {
let r = try callback(t)
return next._setValue(value: .success(r))
} catch {
return next._setValue(value: .failure(error))
}
case .failure(let e):
return next._setValue(value: .failure(e))
}
}
return next.futureResult.assumeIsolatedUnsafeUnchecked()
}
/// When the current `EventLoopFuture<Value>` is in an error state, run the provided callback, which
/// may recover from the error and returns a new value of type `Value`. The provided callback may optionally `throw`,
/// in which case the `EventLoopFuture` will be in a failed state with the new thrown error.
///
/// Operations performed in `flatMapErrorThrowing` should not block, or they will block the entire
/// event loop. `flatMapErrorThrowing` is intended for use when you have the ability to synchronously
/// recover from errors.
///
/// If your callback function throws, the returned `EventLoopFuture` will error.
///
/// - Parameters:
/// - callback: Function that will receive the error value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - Returns: A future that will receive the eventual value or a rethrown error.
@inlinable
@available(*, noasync)
public func flatMapErrorThrowing(
_ callback: @escaping (Error) throws -> Value
) -> EventLoopFuture<Value>.Isolated {
let next = EventLoopPromise<Value>.makeUnleakablePromise(eventLoop: self._wrapped.eventLoop)
let base = self._wrapped
base._whenCompleteIsolated {
switch base._value! {
case .success(let t):
return next._setValue(value: .success(t))
case .failure(let e):
do {
let r = try callback(e)
return next._setValue(value: .success(r))
} catch {
return next._setValue(value: .failure(error))
}
}
}
return next.futureResult.assumeIsolatedUnsafeUnchecked()
}
/// When the current `EventLoopFuture<Value>` is fulfilled, run the provided callback, which
/// performs a synchronous computation and returns a new value of type `NewValue`.
///
/// Operations performed in `map` should not block, or they will block the entire event
/// loop. `map` is intended for use when you have a data-driven function that performs
/// a simple data transformation that cannot error.
///
/// If you have a data-driven function that can throw, you should use `flatMapThrowing`
/// instead.
///
/// ```
/// let future1 = eventually()
/// let future2 = future1.map { T -> U in
/// ... stuff ...
/// return u
/// }
/// let future3 = future2.map { U -> V in
/// ... stuff ...
/// return v
/// }
/// ```
///
/// - Parameters:
/// - callback: Function that will receive the value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - Returns: A future that will receive the eventual value.
@inlinable
@available(*, noasync)
public func map<NewValue>(
_ callback: @escaping (Value) -> (NewValue)
) -> EventLoopFuture<NewValue>.Isolated {
if NewValue.self == Value.self && NewValue.self == Void.self {
self.whenSuccess(callback as! (Value) -> Void)
return self as! EventLoopFuture<NewValue>.Isolated
} else {
let next = EventLoopPromise<NewValue>.makeUnleakablePromise(eventLoop: self._wrapped.eventLoop)
let base = self._wrapped
base._whenCompleteIsolated {
next._setValue(value: base._value!.map(callback))
}
return next.futureResult.assumeIsolatedUnsafeUnchecked()
}
}
/// When the current `EventLoopFuture<Value>` is in an error state, run the provided callback, which
/// may recover from the error by returning an `EventLoopFuture<NewValue>`. The callback is intended to potentially
/// recover from the error by returning a new `EventLoopFuture` that will eventually contain the recovered
/// result.
///
/// If the callback cannot recover it should return a failed `EventLoopFuture`.
///
/// - Note: The `Value` must be `Sendable` since the isolation domains of this future and the future returned from the callback
/// might differ i.e. they might be bound to different event loops.
///
/// - Parameters:
/// - callback: Function that will receive the error value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - Returns: A future that will receive the recovered value.
@inlinable
@available(*, noasync)
public func flatMapError(
_ callback: @escaping (Error) -> EventLoopFuture<Value>
) -> EventLoopFuture<Value>.Isolated where Value: Sendable {
let next = EventLoopPromise<Value>.makeUnleakablePromise(eventLoop: self._wrapped.eventLoop)
let base = self._wrapped
base._whenCompleteIsolated {
switch base._value! {
case .success(let t):
return next._setValue(value: .success(t))
case .failure(let e):
let t = callback(e)
if t.eventLoop.inEventLoop {
return t._addCallback {
next._setValue(value: t._value!)
}
} else {
t.cascade(to: next)
return CallbackList()
}
}
}
return next.futureResult.assumeIsolatedUnsafeUnchecked()
}
/// When the current `EventLoopFuture<Value>.Isolated` is in an error state, run the provided callback, which
/// may recover from the error by returning an `EventLoopFuture<NewValue>.Isolated`. The callback is intended to potentially
/// recover from the error by returning a new `EventLoopFuture.Isolated` that will eventually contain the recovered
/// result.
///
/// If the callback cannot recover it should return a failed `EventLoopFuture.Isolated`.
///
/// - Note: The `Value` need not be `Sendable` since the isolation domains of this future and the future returned from the callback
/// must be the same
///
/// - Parameters:
/// - callback: Function that will receive the error value of this `EventLoopFuture.Isolated` and return
/// a new value lifted into a new `EventLoopFuture.Isolated`.
/// - Returns: A future that will receive the recovered value.
@inlinable
@available(*, noasync)
public func flatMapError(
_ callback: @escaping (Error) -> EventLoopFuture<Value>.Isolated
) -> EventLoopFuture<Value>.Isolated {
let next = EventLoopPromise<Value>.makeUnleakablePromise(eventLoop: self._wrapped.eventLoop)
let base = self._wrapped
base._whenCompleteIsolated {
switch base._value! {
case .success(let t):
return next._setValue(value: .success(t))
case .failure(let e):
let t = callback(e)
t._wrapped.eventLoop.assertInEventLoop()
return t._wrapped._addCallback {
next._setValue(value: t._wrapped._value!)
}
}
}
return next.futureResult.assumeIsolatedUnsafeUnchecked()
}
/// When the current `EventLoopFuture<Value>` is fulfilled, run the provided callback, which
/// performs a synchronous computation and returns either a new value (of type `NewValue`) or
/// an error depending on the `Result` returned by the closure.
///
/// Operations performed in `flatMapResult` should not block, or they will block the entire
/// event loop. `flatMapResult` is intended for use when you have a data-driven function that
/// performs a simple data transformation that can potentially error.
///
///
/// - Parameters:
/// - body: Function that will receive the value of this `EventLoopFuture` and return
/// a new value or error lifted into a new `EventLoopFuture`.
/// - Returns: A future that will receive the eventual value.
@inlinable
@available(*, noasync)
public func flatMapResult<NewValue, SomeError: Error>(
_ body: @escaping (Value) -> Result<NewValue, SomeError>
) -> EventLoopFuture<NewValue>.Isolated {
let next = EventLoopPromise<NewValue>.makeUnleakablePromise(eventLoop: self._wrapped.eventLoop)
let base = self._wrapped
base._whenCompleteIsolated {
switch base._value! {
case .success(let value):
switch body(value) {
case .success(let newValue):
return next._setValue(value: .success(newValue))
case .failure(let error):
return next._setValue(value: .failure(error))
}
case .failure(let e):
return next._setValue(value: .failure(e))
}
}
return next.futureResult.assumeIsolatedUnsafeUnchecked()
}
/// When the current `EventLoopFuture<Value>` is in an error state, run the provided callback, which
/// can recover from the error and return a new value of type `Value`. The provided callback may not `throw`,
/// so this function should be used when the error is always recoverable.
///
/// Operations performed in `recover` should not block, or they will block the entire
/// event loop. `recover` is intended for use when you have the ability to synchronously
/// recover from errors.
///
/// - Parameters:
/// - callback: Function that will receive the error value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - Returns: A future that will receive the recovered value.
@inlinable
@available(*, noasync)
public func recover(
_ callback: @escaping (Error) -> Value
) -> EventLoopFuture<Value>.Isolated {
let next = EventLoopPromise<Value>.makeUnleakablePromise(eventLoop: self._wrapped.eventLoop)
let base = self._wrapped
base._whenCompleteIsolated {
switch base._value! {
case .success(let t):
return next._setValue(value: .success(t))
case .failure(let e):
return next._setValue(value: .success(callback(e)))
}
}
return next.futureResult.assumeIsolatedUnsafeUnchecked()
}
/// Adds an observer callback to this `EventLoopFuture` that is called when the
/// `EventLoopFuture` has a success result.
///
/// An observer callback cannot return a value, meaning that this function cannot be chained
/// from. If you are attempting to create a computation pipeline, consider `map` or `flatMap`.
/// If you find yourself passing the results from this `EventLoopFuture` to a new `EventLoopPromise`
/// in the body of this function, consider using `cascade` instead.
///
/// - Parameters:
/// - callback: The callback that is called with the successful result of the `EventLoopFuture`.
@inlinable
@available(*, noasync)
public func whenSuccess(_ callback: @escaping (Value) -> Void) {
let base = self._wrapped
base._whenCompleteIsolated {
if case .success(let t) = base._value! {
callback(t)
}
return CallbackList()
}
}
/// Adds an observer callback to this `EventLoopFuture` that is called when the
/// `EventLoopFuture` has a failure result.
///
/// An observer callback cannot return a value, meaning that this function cannot be chained
/// from. If you are attempting to create a computation pipeline, consider `recover` or `flatMapError`.
/// If you find yourself passing the results from this `EventLoopFuture` to a new `EventLoopPromise`
/// in the body of this function, consider using `cascade` instead.
///
/// - Parameters:
/// - callback: The callback that is called with the failed result of the `EventLoopFuture`.
@inlinable
@available(*, noasync)
public func whenFailure(_ callback: @escaping (Error) -> Void) {
let base = self._wrapped
base._whenCompleteIsolated {
if case .failure(let e) = base._value! {
callback(e)
}
return CallbackList()
}
}
/// Adds an observer callback to this `EventLoopFuture` that is called when the
/// `EventLoopFuture` has any result.
///
/// - Parameters:
/// - callback: The callback that is called when the `EventLoopFuture` is fulfilled.
@inlinable
@available(*, noasync)
public func whenComplete(
_ callback: @escaping (Result<Value, Error>) -> Void
) {
let base = self._wrapped
base._whenCompleteIsolated {
callback(base._value!)
return CallbackList()
}
}
/// Adds an observer callback to this `EventLoopFuture` that is called when the
/// `EventLoopFuture` has any result.
///
/// - Parameters:
/// - callback: the callback that is called when the `EventLoopFuture` is fulfilled.
/// - Returns: the current `EventLoopFuture`
@inlinable
@available(*, noasync)
public func always(
_ callback: @escaping (Result<Value, Error>) -> Void
) -> EventLoopFuture<Value>.Isolated {
self.whenComplete { result in callback(result) }
return self
}
/// Unwrap an `EventLoopFuture` where its type parameter is an `Optional`.
///
/// Unwraps a future returning a new `EventLoopFuture` with either: the value passed in the `orReplace`
/// parameter when the future resolved with value Optional.none, or the same value otherwise. For example:
/// ```
/// promise.futureResult.unwrap(orReplace: 42).wait()
/// ```
///
/// - Parameters:
/// - replacement: the value of the returned `EventLoopFuture` when then resolved future's value is `Optional.some()`.
/// - Returns: an new `EventLoopFuture` with new type parameter `NewValue` and the value passed in the `orReplace` parameter.
@inlinable
@available(*, noasync)
public func unwrap<NewValue>(
orReplace replacement: NewValue
) -> EventLoopFuture<NewValue>.Isolated where Value == NewValue? {
self.map { (value) -> NewValue in
guard let value = value else {
return replacement
}
return value
}
}
/// Unwrap an `EventLoopFuture` where its type parameter is an `Optional`.
///
/// Unwraps a future returning a new `EventLoopFuture` with either: the value returned by the closure passed in
/// the `orElse` parameter when the future resolved with value Optional.none, or the same value otherwise. For example:
/// ```
/// var x = 2
/// promise.futureResult.unwrap(orElse: { x * 2 }).wait()
/// ```
///
/// - Parameters:
/// - callback: a closure that returns the value of the returned `EventLoopFuture` when then resolved future's value
/// is `Optional.some()`.
/// - Returns: an new `EventLoopFuture` with new type parameter `NewValue` and with the value returned by the closure
/// passed in the `orElse` parameter.
@inlinable
@available(*, noasync)
public func unwrap<NewValue>(
orElse callback: @escaping () -> NewValue
) -> EventLoopFuture<NewValue>.Isolated where Value == NewValue? {
self.map { (value) -> NewValue in
guard let value = value else {
return callback()
}
return value
}
}
/// Returns the wrapped event loop future.
@inlinable
public func nonisolated() -> EventLoopFuture<Value> {
self._wrapped
}
}
/// Returns a variant of this ``EventLoopFuture`` with less strict
/// `Sendable` requirements. Can only be called from on the
/// ``EventLoop`` to which this ``EventLoopFuture`` is bound, will crash
/// if that invariant fails to be met.
@inlinable
@available(*, noasync)
public func assumeIsolated() -> Isolated {
self.eventLoop.preconditionInEventLoop()
return Isolated(_wrapped: self)
}
/// Returns a variant of this ``EventLoopFuture`` with less strict
/// `Sendable` requirements. Can only be called from on the
/// ``EventLoop`` to which this ``EventLoopFuture`` is bound, will crash
/// if that invariant fails to be met in debug builds.
///
/// This is an unsafe version of ``EventLoopFuture/assumeIsolated()`` which
/// omits the runtime check in release builds.
@inlinable
@available(*, noasync)
public func assumeIsolatedUnsafeUnchecked() -> Isolated {
self.eventLoop.assertInEventLoop()
return Isolated(_wrapped: self)
}
}
@available(*, unavailable)
extension EventLoopFuture.Isolated: Sendable {}
extension EventLoopPromise {
/// A struct wrapping an ``EventLoopPromise`` that ensures all calls to any method on this struct
/// are coming from the event loop of the promise.
///
/// This type is explicitly not `Sendable`. It may only be constructed on an event loop,
/// using ``EventLoopPromise/assumeIsolated()``, and may not subsequently be passed to other isolation
/// domains.
///
/// Using this type relaxes the need to have the promise completion functions accept `Sendable`
/// values, as this type can only be handled on the ``EventLoop``.
///
/// This type does not offer the full suite of completion functions that ``EventLoopPromise``
/// does, as many of those functions do not require `Sendable` values already. It only offers
/// versions for the functions that do require `Sendable` types. If you have an
/// ``EventLoopPromise/Isolated`` but need a regular ``EventLoopPromise``, use
/// ``EventLoopPromise/Isolated/nonisolated()`` to unwrap the value.
public struct Isolated {
@usableFromInline
let _wrapped: EventLoopPromise<Value>
@inlinable
init(_wrapped: EventLoopPromise<Value>) {
self._wrapped = _wrapped
}
/// Returns the `EventLoopFuture.Isolated` which will be notified once the execution of the scheduled task completes.
@inlinable
@available(*, noasync)
public var futureResult: EventLoopFuture<Value>.Isolated {
self._wrapped.futureResult.assumeIsolated()
}
/// Deliver a successful result to the associated `EventLoopFuture<Value>` object.
///
/// - Parameters:
/// - value: The successful result of the operation.
@inlinable
@available(*, noasync)
public func succeed(_ value: Value) {
self._wrapped._setValue(value: .success(value))._run()
}
/// Complete the promise with the passed in `Result<Value, Error>`.
///
/// This method is equivalent to invoking:
/// ```
/// switch result {
/// case .success(let value):
/// promise.succeed(value)
/// case .failure(let error):
/// promise.fail(error)
/// }
/// ```
///
/// - Parameters:
/// - result: The result which will be used to succeed or fail this promise.
@inlinable
@available(*, noasync)
public func completeWith(_ result: Result<Value, Error>) {
self._wrapped._setValue(value: result)._run()
}
/// Returns the wrapped event loop promise.
@inlinable
public func nonisolated() -> EventLoopPromise<Value> {
self._wrapped
}
}
/// Returns a variant of this ``EventLoopPromise`` with less strict
/// `Sendable` requirements. Can only be called from on the
/// ``EventLoop`` to which this ``EventLoopPromise`` is bound, will crash
/// if that invariant fails to be met.
@inlinable
@available(*, noasync)
public func assumeIsolated() -> Isolated {
self.futureResult.eventLoop.preconditionInEventLoop()
return Isolated(_wrapped: self)
}
/// Returns a variant of this ``EventLoopPromise`` with less strict
/// `Sendable` requirements. Can only be called from on the
/// ``EventLoop`` to which this ``EventLoopPromise`` is bound, will crash
/// if that invariant fails to be met.
///
/// This is an unsafe version of ``EventLoopPromise/assumeIsolated()`` which
/// omits the runtime check in release builds and doesn't prevent you using it
/// from using it in async contexts.
@inlinable
public func assumeIsolatedUnsafeUnchecked() -> Isolated {
self.futureResult.eventLoop.assertInEventLoop()
return Isolated(_wrapped: self)
}
}
@available(*, unavailable)
extension EventLoopPromise.Isolated: Sendable {}