Skip to content

Commit c42bb90

Browse files
authored
Allow converting from TimeValue to timespec (#85)
This extends the `TimeValue` type added in #83 by allowing conversion to `timespec`. It then adopts that in a couple places to ensure consistency.
1 parent ab0ab98 commit c42bb90

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Sources/Testing/Events/Clock.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ extension Test.Clock {
113113
@available(_clockAPI, *)
114114
static func sleep(for duration: Duration) async throws {
115115
#if SWT_NO_UNSTRUCTURED_TASKS
116-
var ts = timespec(duration)
116+
let timeValue = TimeValue(duration)
117+
var ts = timespec(timeValue)
117118
var tsRemaining = ts
118119
while 0 != nanosleep(&ts, &tsRemaining) {
119120
try Task.checkCancellation()
@@ -140,7 +141,7 @@ extension Test.Clock: _Concurrency.Clock {
140141
#if SWT_TARGET_OS_APPLE
141142
var res = timespec()
142143
_ = clock_getres(CLOCK_UPTIME_RAW, &res)
143-
return Duration(res)
144+
return Duration(TimeValue(res))
144145
#else
145146
SuspendingClock().minimumResolution
146147
#endif

Sources/Testing/Events/TimeValue.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,9 @@ extension SuspendingClock.Instant {
8484
self = unsafeBitCast(Duration(timeValue), to: SuspendingClock.Instant.self)
8585
}
8686
}
87+
88+
extension timespec {
89+
init(_ timeValue: TimeValue) {
90+
self.init(tv_sec: .init(timeValue.seconds), tv_nsec: .init(timeValue.attoseconds / 1_000_000_000))
91+
}
92+
}

0 commit comments

Comments
 (0)