From 73a1a95a8a76dab6d606285eca0acea4fd6928f5 Mon Sep 17 00:00:00 2001 From: kudit Date: Fri, 14 Aug 2026 11:46:19 -0400 Subject: [PATCH] Make color logging concurrency-safe without MainActor --- Sources/Compatibility.swift | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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, *)