CustomActor + assumeIsolated

It seems you can replicate assumeIsolated by discarding the closure isolation through unsafeBitCast. This is what the MainActor implementation does.

@globalActor
actor MyActor {
  private init() {}

  static let shared = MyActor()

  static func assumeIsolated<T: Sendable>(_ operation: @MyActor () throws -> T) rethrows -> T {
    shared.preconditionIsolated()

    return try withoutActuallyEscaping(operation) { escapingClosure in
      try unsafeBitCast(escapingClosure, to: (() throws -> T).self)()
    }
  }
}
4 Likes