Skip to content

Commit bdc048e

Browse files
authored
Merge pull request swiftlang#36511 from DougGregor/main-actor-run
2 parents cfa07b6 + 3aa4fbe commit bdc048e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

stdlib/public/Concurrency/Actor.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ fileprivate func _registerMainActor(actor: AnyObject)
4343
_registerMainActor(actor: self)
4444
}
4545
}
46+
47+
extension MainActor {
48+
/// Execute the given body closure on the main actor.
49+
public static func run<T>(
50+
resultType: T.Type = T.self,
51+
body: @MainActor @Sendable () throws -> T
52+
) async rethrows -> T {
53+
@MainActor func runOnMain(body: @MainActor @Sendable () throws -> T) async rethrows -> T {
54+
return try body()
55+
}
56+
57+
return try await runOnMain(body: body)
58+
}
59+
}

0 commit comments

Comments
 (0)