Last active
November 25, 2022 15:30
-
-
Save honghaoz/4ec349e97ecf269e9e2dc82bf14ba7fe to your computer and use it in GitHub Desktop.
Aggregated continuation, uses checked continuation version in DEBUG build and use unsafe version in RELEASE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Aggregated continuation, uses `CheckedContinuation` for DEBUG build, uses `UnsafeContinuation` for RELEASE build. | |
public struct Continuation<T, E>: Sendable where E: Error { | |
#if DEBUG | |
public let continuation: CheckedContinuation<T, E> | |
public init(continuation: CheckedContinuation<T, E>) { | |
self.continuation = continuation | |
} | |
#else | |
public let continuation: UnsafeContinuation<T, E> | |
public init(continuation: UnsafeContinuation<T, E>) { | |
self.continuation = continuation | |
} | |
#endif | |
@inlinable | |
@inline(__always) | |
public func resume(returning value: T) { | |
continuation.resume(returning: value) | |
} | |
@inlinable | |
@inline(__always) | |
public func resume(returning value: T) where E == Never { | |
continuation.resume(returning: value) | |
} | |
@inlinable | |
@inline(__always) | |
public func resume(throwing error: E) { | |
continuation.resume(throwing: error) | |
} | |
@inlinable | |
@inline(__always) | |
public func resume(with result: Result<T, some Error>) where E == Error { | |
continuation.resume(with: result) | |
} | |
@inlinable | |
@inline(__always) | |
public func resume(with result: Result<T, E>) { | |
continuation.resume(with: result) | |
} | |
@inlinable | |
@inline(__always) | |
public func resume() where T == () { | |
continuation.resume() | |
} | |
} | |
@inlinable | |
@inline(__always) | |
public func withContinuation<T>(function: String = #function, _ body: (Continuation<T, Never>) -> Void) async -> T { | |
#if DEBUG | |
await withCheckedContinuation(function: function) { (checkedContinuation: CheckedContinuation<T, Never>) in | |
body(Continuation(continuation: checkedContinuation)) | |
} | |
#else | |
await withUnsafeContinuation { (unsafeContinuation: UnsafeContinuation<T, Never>) in | |
body(Continuation(continuation: unsafeContinuation)) | |
} | |
#endif | |
} | |
@inlinable | |
@inline(__always) | |
public func withThrowingContinuation<T>(function: String = #function, _ body: (Continuation<T, Error>) -> Void) async throws -> T { | |
#if DEBUG | |
try await withCheckedThrowingContinuation(function: function) { (checkedContinuation: CheckedContinuation<T, Error>) in | |
body(Continuation(continuation: checkedContinuation)) | |
} | |
#else | |
try await withUnsafeThrowingContinuation { (unsafeContinuation: UnsafeContinuation<T, Error>) in | |
body(Continuation(continuation: unsafeContinuation)) | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment