Fix another warning and @unknown default issue

This commit is contained in:
Gus Cairo 2024-06-21 13:01:23 +01:00
parent fdc5520ddd
commit 79dbc9dc4a
4 changed files with 30 additions and 5 deletions

View File

@ -46,7 +46,7 @@ import Musl
/// of lock is safe to use with `libpthread`-based threading models, such as the
/// one used by NIO. On Windows, the lock is based on the substantially similar
/// `SRWLOCK` type.
internal final class Lock: Sendable {
internal final class Lock {
#if os(Windows)
fileprivate let mutex: UnsafeMutablePointer<SRWLOCK> =
UnsafeMutablePointer.allocate(capacity: 1)
@ -131,6 +131,8 @@ extension Lock {
}
}
extension Lock: @unchecked Sendable {}
/// A reader/writer threading lock based on `libpthread` instead of `libdispatch`.
///
/// This object provides a lock on top of a single `pthread_rwlock_t`. This kind
@ -273,3 +275,5 @@ extension ReadWriteLock {
try self.withWriterLock(body)
}
}
extension ReadWriteLock: @unchecked Sendable {}

View File

@ -600,7 +600,8 @@ public enum MetricsSystem {
return try self._factory.withWriterLock(body)
}
private final class FactoryBox {
// This can be `@unchecked Sendable` because we're manually gating access to mutable state with a lock.
private final class FactoryBox: @unchecked Sendable {
private let lock = ReadWriteLock()
fileprivate var _underlying: MetricsFactory
private var initialized = false

View File

@ -60,6 +60,16 @@ extension Timer {
/// - duration: The duration to record.
@inlinable
public func record(_ duration: DispatchTimeInterval) {
// This wrapping in a optional is a workaround because DispatchTimeInterval
// is a non-frozen public enum and Dispatch is built with library evolution
// mode turned on.
// This means we should have an `@unknown default` case, but this breaks
// on non-Darwin platforms.
// Switching over an optional means that the `.none` case will map to
// `default` (which means we'll always have a valid case to go into
// the default case), but in reality this case will never exist as this
// optional will never be nil.
let duration = Optional(duration)
switch duration {
case .nanoseconds(let value):
self.recordNanoseconds(value)
@ -71,7 +81,7 @@ extension Timer {
self.recordSeconds(value)
case .never:
self.record(0)
@unknown default:
default:
self.record(0)
}
}

View File

@ -185,7 +185,17 @@ class MetricsExtensionsTests: XCTestCase {
// https://bugs.swift.org/browse/SR-6310
extension DispatchTimeInterval {
func nano() -> Int {
switch self {
// This wrapping in a optional is a workaround because DispatchTimeInterval
// is a non-frozen public enum and Dispatch is built with library evolution
// mode turned on.
// This means we should have an `@unknown default` case, but this breaks
// on non-Darwin platforms.
// Switching over an optional means that the `.none` case will map to
// `default` (which means we'll always have a valid case to go into
// the default case), but in reality this case will never exist as this
// optional will never be nil.
let interval = Optional(self)
switch interval {
case .nanoseconds(let value):
return value
case .microseconds(let value):
@ -196,7 +206,7 @@ extension DispatchTimeInterval {
return value * 1_000_000_000
case .never:
return 0
@unknown default:
default:
return 0
}
}