Skip to content

Commit 037df1f

Browse files
authored
Add Namespace API (#214)
1 parent 3a68347 commit 037df1f

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// Namespace.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for iOS 18.0
6+
// Status: Complete
7+
// ID: 79F323039D8AB6E63210271E57AD5E86 (SwiftUICore)
8+
9+
/// A dynamic property type that allows access to a namespace defined
10+
/// by the persistent identity of the object containing the property
11+
/// (e.g. a view).
12+
@frozen
13+
@propertyWrapper
14+
public struct Namespace: DynamicProperty, Sendable {
15+
@usableFromInline
16+
var id: Int
17+
18+
package init(id: Int) {
19+
self.id = id
20+
}
21+
22+
@inlinable
23+
public init() { id = 0 }
24+
25+
private struct Box: DynamicPropertyBox {
26+
27+
typealias Property = Namespace
28+
29+
var id: Int
30+
31+
mutating func reset() {
32+
id = 0
33+
}
34+
35+
mutating func update(property: inout Property, phase: ViewPhase) -> Bool {
36+
let oldID = id
37+
if oldID == 0 {
38+
id = UniqueID().value
39+
}
40+
property.id = id
41+
return oldID == 0
42+
}
43+
}
44+
45+
public static func _makeProperty<V>(
46+
in buffer: inout _DynamicPropertyBuffer,
47+
container: _GraphValue<V>,
48+
fieldOffset: Int,
49+
inputs: inout _GraphInputs
50+
) {
51+
buffer.append(Box(id: 0), fieldOffset: fieldOffset)
52+
}
53+
54+
public var wrappedValue: Namespace.ID {
55+
guard id != .zero else {
56+
Log.runtimeIssues("Reading a Namespace property outside View.body. This will result in identifiers that never match any other identifier.")
57+
return Namespace.ID(id: UniqueID().value)
58+
}
59+
return Namespace.ID(id: id)
60+
}
61+
62+
/// A namespace defined by the persistent identity of an
63+
/// `@Namespace` dynamic property.
64+
@frozen
65+
public struct ID: Hashable {
66+
package private(set) var id: Int
67+
68+
package init(id: Int) {
69+
self.id = id
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)