Skip to content

Commit 9633fd5

Browse files
committed
Rework observation for transaction actors and start to include macros
1 parent a6c0108 commit 9633fd5

23 files changed

+663
-968
lines changed

include/swift/Threading/Impl/Darwin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ inline tls_key_t tls_get_key(tls_key k) {
238238
return __PTK_FRAMEWORK_SWIFT_KEY4;
239239
case tls_key::concurrency_fallback:
240240
return __PTK_FRAMEWORK_SWIFT_KEY5;
241+
case tls_key::observation_transaction:
242+
return __PTK_FRAMEWORK_SWIFT_KEY6;
241243
}
242244
}
243245

include/swift/Threading/TLSKeys.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ enum class tls_key {
2121
compatibility50,
2222
concurrency_task,
2323
concurrency_executor_tracking_info,
24-
concurrency_fallback
24+
concurrency_fallback,
25+
observation_transaction
2526
};
2627

2728
} // namespace swift

stdlib/public/Observation/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#===--- CMakeLists.txt - Reflection support library ---------------------===#
1+
#===--- CMakeLists.txt - Observation support library ---------------------===#
22
#
33
# This source file is part of the Swift.org open source project
44
#
@@ -11,3 +11,4 @@
1111
#===----------------------------------------------------------------------===#
1212

1313
add_subdirectory(Sources/Observation)
14+
add_subdirectory(Sources/ObservationMacros)

stdlib/public/Observation/Sources/Observation/CMakeLists.txt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,27 @@
1212

1313
set(SWIFT_OBSERVATION_SWIFT_FLAGS)
1414

15+
list(APPEND SWIFT_OBSERVATION_SWIFT_FLAGS
16+
"-enable-experimental-feature Macros")
17+
1518
add_swift_target_library(swiftObservation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
16-
ImmediateTransactionModel.swift
19+
KeyPaths.swift
1720
Locking.cpp
1821
Locking.swift
19-
MemberKeyPaths.swift
22+
Macros.swift
2023
Observable.swift
21-
ObservationRegistrar.swift
22-
ObservationToken.swift
24+
ObservationRegistar.swift
2325
ObservationTracking.swift
24-
ObservationTransaction.swift
25-
ObservationTransactionModel.swift
2626
ObservedChanges.swift
27-
Observer.swift
27+
ObservedTransactions.swift
2828
ThreadLocal.cpp
2929
ThreadLocal.swift
3030

3131
SWIFT_COMPILE_FLAGS
3232
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
3333
${SWIFT_OBSERVATION_SWIFT_FLAGS}
3434
SWIFT_MODULE_DEPENDS _Concurrency
35-
INSTALL_IN_COMPONENT stdlib)
35+
INSTALL_IN_COMPONENT stdlib
36+
37+
MACCATALYST_BUILD_FLAVOR "zippered"
38+
)

stdlib/public/Observation/Sources/Observation/ImmediateTransactionModel.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
@available(SwiftStdlib 5.9, *)
13+
public struct KeyPaths<Root>: SetAlgebra, Hashable, @unchecked Sendable {
14+
public typealias Element = PartialKeyPath<Root>
15+
public typealias ArrayLiteralElement = PartialKeyPath<Root>
16+
17+
internal var raw: Set<PartialKeyPath<Root>>
18+
19+
internal init(raw keyPaths: Set<AnyKeyPath>) {
20+
self.raw = Set(keyPaths.compactMap { $0 as? PartialKeyPath<Root> })
21+
}
22+
23+
public init(_ sequence: __owned some Sequence<PartialKeyPath<Root>>) {
24+
self.raw = Set(sequence)
25+
}
26+
27+
public init() {
28+
self.raw = Set()
29+
}
30+
31+
public init(arrayLiteral elements: PartialKeyPath<Root>...) {
32+
self.init(elements)
33+
}
34+
35+
public func union(_ other: __owned KeyPaths<Root>) -> KeyPaths<Root> {
36+
KeyPaths(raw: raw.union(other.raw))
37+
}
38+
39+
public func intersection(_ other: KeyPaths<Root>) -> KeyPaths<Root> {
40+
KeyPaths(raw: raw.intersection(other.raw))
41+
}
42+
43+
public func symmetricDifference(_ other: __owned KeyPaths<Root>) -> KeyPaths<Root> {
44+
KeyPaths(raw: raw.symmetricDifference(other.raw))
45+
}
46+
47+
public mutating func formUnion(_ other: __owned KeyPaths<Root>) {
48+
raw.formUnion(other.raw)
49+
}
50+
51+
public mutating func formIntersection(_ other: KeyPaths<Root>) {
52+
raw.formIntersection(other.raw)
53+
}
54+
55+
public mutating func formSymmetricDifference(_ other: __owned KeyPaths<Root>) {
56+
raw.formSymmetricDifference(other.raw)
57+
}
58+
59+
public func contains(_ member: PartialKeyPath<Root>) -> Bool {
60+
raw.contains(member)
61+
}
62+
63+
public var isEmpty: Bool {
64+
raw.isEmpty
65+
}
66+
67+
@discardableResult
68+
public mutating func insert(_ newMember: __owned PartialKeyPath<Root>) -> (inserted: Bool, memberAfterInsert: PartialKeyPath<Root>) {
69+
let (inserted, memberAfterInsert) = raw.insert(newMember)
70+
return (inserted, memberAfterInsert)
71+
}
72+
73+
@discardableResult
74+
public mutating func remove(_ member: PartialKeyPath<Root>) -> PartialKeyPath<Root>? {
75+
raw.remove(member)
76+
}
77+
78+
@discardableResult
79+
public mutating func update(with newMember: __owned PartialKeyPath<Root>) -> PartialKeyPath<Root>? {
80+
raw.update(with:newMember)
81+
}
82+
83+
public func hash(into hasher: inout Hasher) {
84+
hasher.combine(raw)
85+
}
86+
87+
public static func == (_ lhs: KeyPaths<Root>, _ rhs: KeyPaths<Root>) -> Bool {
88+
return lhs.raw == rhs.raw
89+
}
90+
}
91+
92+
@available(SwiftStdlib 5.9, *)
93+
extension KeyPaths where Root: Observable {
94+
public init(dependent: KeyPaths<Root>) {
95+
self.init()
96+
for raw in dependent.raw {
97+
self.formUnion(Root.dependencies(of: raw))
98+
}
99+
}
100+
}

stdlib/public/Observation/Sources/Observation/Locking.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct ManagedCriticalState<State> {
5252

5353
@available(SwiftStdlib 5.9, *)
5454
protocol Deinitializable {
55-
func deinitialize()
55+
mutating func deinitialize()
5656
}
5757

5858
@available(SwiftStdlib 5.9, *)
@@ -78,15 +78,8 @@ extension ManagedCriticalState where State: Deinitializable {
7878
extension ManagedCriticalState: @unchecked Sendable where State: Sendable { }
7979

8080
@available(SwiftStdlib 5.9, *)
81-
extension ManagedCriticalState: Hashable {
82-
static func == (
83-
lhs: ManagedCriticalState<State>,
84-
rhs: ManagedCriticalState<State>
85-
) -> Bool {
86-
lhs.buffer === rhs.buffer
87-
}
88-
89-
func hash(into hasher: inout Hasher) {
90-
hasher.combine(ObjectIdentifier(buffer))
81+
extension ManagedCriticalState: Identifiable {
82+
var id: ObjectIdentifier {
83+
ObjectIdentifier(buffer)
9184
}
9285
}

stdlib/public/Observation/Sources/Observation/Observer.swift renamed to stdlib/public/Observation/Sources/Observation/Macros.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
@available(SwiftStdlib 5.9, *)
13+
@attached(member)
14+
@attached(memberAttribute)
15+
public macro Observable() = #externalMacro(module: "ObservationMacros", type: "ObservableMacro")
1216

1317
@available(SwiftStdlib 5.9, *)
14-
public protocol Observer<Subject> {
15-
associatedtype Subject: Observable
16-
17-
func changes(_ subject: Subject, to members: MemberKeyPaths<Subject>)
18-
}
18+
@attached(accessor)
19+
public macro ObservableProperty() = #externalMacro(module: "ObservationMacros", type: "ObservablePropertyMacro")

0 commit comments

Comments
 (0)