diff --git a/Sources/Atomics/AtomicOptionalRawRepresentable.swift b/Sources/Atomics/AtomicOptionalRawRepresentable.swift new file mode 100644 index 0000000..ce2a54b --- /dev/null +++ b/Sources/Atomics/AtomicOptionalRawRepresentable.swift @@ -0,0 +1,136 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Atomics open source project +// +// Copyright (c) 2021-2023 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 +// +//===----------------------------------------------------------------------===// + +extension RawRepresentable where RawValue: AtomicOptionalWrappable { + public typealias AtomicOptionalRepresentation = RawValue.AtomicOptionalRepresentation +} + +/// A default atomic storage representation for a `RawRepresentable` type +/// whose `RawValue` conforms to `AtomicOptionalWrappable`. +@frozen +public struct AtomicOptionalRawRepresentableStorage: AtomicStorage + where Wrapped: RawRepresentable, + Wrapped.RawValue: AtomicOptionalWrappable { + + public typealias Value = Optional + + @usableFromInline + typealias Storage = Wrapped.RawValue.AtomicOptionalRepresentation + + @usableFromInline + var _storage: Storage + + @_transparent @_alwaysEmitIntoClient + public init(_ value: __owned Optional) { + _storage = Storage(value?.rawValue) + } + + @_transparent @_alwaysEmitIntoClient + __consuming public func dispose() -> Optional { + _storage.dispose().flatMap(Wrapped.init(rawValue:)) + } + + @usableFromInline + @_transparent @_alwaysEmitIntoClient + static func _extract( + _ ptr: UnsafeMutablePointer + ) -> UnsafeMutablePointer { + // `Self` is layout-compatible with its only stored property. + return UnsafeMutableRawPointer(ptr).assumingMemoryBound(to: Storage.self) + } + + @_semantics("atomics.requires_constant_orderings") + @_transparent @_alwaysEmitIntoClient + public static func atomicLoad( + at pointer: UnsafeMutablePointer, + ordering: AtomicLoadOrdering + ) -> Optional { + let ro = Storage.atomicLoad( + at: _extract(pointer), ordering: ordering) + return ro.flatMap(Wrapped.init(rawValue:)) + } + + @_semantics("atomics.requires_constant_orderings") + @_transparent @_alwaysEmitIntoClient + public static func atomicStore( + _ desired: __owned Optional, + at pointer: UnsafeMutablePointer, + ordering: AtomicStoreOrdering + ) { + Storage.atomicStore( + desired?.rawValue, at: _extract(pointer), ordering: ordering) + } + + @_semantics("atomics.requires_constant_orderings") + @_transparent @_alwaysEmitIntoClient + public static func atomicExchange( + _ desired: __owned Optional, + at pointer: UnsafeMutablePointer, + ordering: AtomicUpdateOrdering + ) -> Optional { + let ro = Storage.atomicExchange( + desired?.rawValue, at: _extract(pointer), ordering: ordering) + return ro.flatMap(Wrapped.init(rawValue:)) + } + + @_semantics("atomics.requires_constant_orderings") + @_transparent @_alwaysEmitIntoClient + public static func atomicCompareExchange( + expected: Optional, + desired: __owned Optional, + at pointer: UnsafeMutablePointer, + ordering: AtomicUpdateOrdering + ) -> (exchanged: Bool, original: Optional) { + let ro = Storage.atomicCompareExchange( + expected: expected?.rawValue, + desired: desired?.rawValue, + at: _extract(pointer), + ordering: ordering) + return (ro.exchanged, ro.original.flatMap(Wrapped.init(rawValue:))) + } + + @_semantics("atomics.requires_constant_orderings") + @_transparent @_alwaysEmitIntoClient + public static func atomicCompareExchange( + expected: Optional, + desired: __owned Optional, + at pointer: UnsafeMutablePointer, + successOrdering: AtomicUpdateOrdering, + failureOrdering: AtomicLoadOrdering + ) -> (exchanged: Bool, original: Optional) { + let ro = Storage.atomicCompareExchange( + expected: expected?.rawValue, + desired: desired?.rawValue, + at: _extract(pointer), + successOrdering: successOrdering, + failureOrdering: failureOrdering) + return (ro.exchanged, ro.original.flatMap(Wrapped.init(rawValue:))) + } + + @_semantics("atomics.requires_constant_orderings") + @_transparent @_alwaysEmitIntoClient + public static func atomicWeakCompareExchange( + expected: Optional, + desired: __owned Optional, + at pointer: UnsafeMutablePointer, + successOrdering: AtomicUpdateOrdering, + failureOrdering: AtomicLoadOrdering + ) -> (exchanged: Bool, original: Optional) { + let ro = Storage.atomicWeakCompareExchange( + expected: expected?.rawValue, + desired: desired?.rawValue, + at: _extract(pointer), + successOrdering: successOrdering, + failureOrdering: failureOrdering) + return (ro.exchanged, ro.original.flatMap(Wrapped.init(rawValue:))) + } +}