mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
41 lines
1.1 KiB
Swift
41 lines
1.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import Swift
|
|
|
|
public protocol Clock: Sendable {
|
|
associatedtype Instant: InstantProtocol
|
|
|
|
var now: Instant { get }
|
|
var minimumResolution: Instant.Duration { get }
|
|
|
|
func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws
|
|
}
|
|
|
|
|
|
extension Clock {
|
|
public func measure(_ work: () throws -> Void) rethrows -> Instant.Duration {
|
|
let start = now
|
|
try work()
|
|
let end = now
|
|
return start.duration(to: end)
|
|
}
|
|
|
|
public func measure(
|
|
_ work: () async throws -> Void
|
|
) async rethrows -> Instant.Duration {
|
|
let start = now
|
|
try await work()
|
|
let end = now
|
|
return start.duration(to: end)
|
|
}
|
|
}
|