diff --git a/Sources/Compatibility.swift b/Sources/Compatibility.swift index e1255af..692edfe 100644 --- a/Sources/Compatibility.swift +++ b/Sources/Compatibility.swift @@ -5,11 +5,28 @@ // Created by Ben Ku on 7/5/24. // +#if compiler(>=6.0) && canImport(Synchronization) +import Synchronization +private let colorLoggingState = Mutex(false) +#endif public extension DebugLevel { - /// Set this to `true` to log failed color parsing notices when returning `nil` - @MainActor + /// Set this to `true` to log failed color parsing notices when returning `nil`. + /// + /// Swift 6 uses `Synchronization.Mutex` so synchronous color parsing can read this + /// configuration from any actor without making the parsing APIs MainActor-isolated. +#if compiler(>=6.0) && canImport(Synchronization) + static var colorLogging: Bool { + get { colorLoggingState.withLock { $0 } } + set { colorLoggingState.withLock { $0 = newValue } } + } +#elseif compiler(>=6.0) + // Compatibility fallback for Swift 6 toolchains that do not ship Synchronization. + // Current supported Swift 6 toolchains use the mutex-backed implementation above. + nonisolated(unsafe) static var colorLogging = false +#else static var colorLogging = false +#endif // TODO: Change this to generic type matching protocol instead of boxed any type? @available(iOS 13, tvOS 13, watchOS 6, *)