Skip to content

Commit 7e4a6f3

Browse files
authored
Update Metadata+OpenSwiftUI (#196)
* Update OpenGraph folder structure * Add Metadata+OpenSwiftUI
1 parent 2771461 commit 7e4a6f3

File tree

8 files changed

+118
-3
lines changed

8 files changed

+118
-3
lines changed

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Metadata+OpenSwiftUI.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for iOS 18.0
6+
// Status: Complete
7+
8+
public import OpenGraphShims
9+
10+
extension Metadata {
11+
package var isValueType: Bool {
12+
switch kind {
13+
case .struct, .enum, .optional, .tuple: true
14+
default: false
15+
}
16+
}
17+
18+
public func genericType(at index: Int) -> any Any.Type {
19+
UnsafeRawPointer(rawValue)
20+
.advanced(by: index &* 8)
21+
.advanced(by: 16)
22+
.assumingMemoryBound(to: Any.Type.self)
23+
.pointee
24+
}
25+
26+
#if OPENSWIFTUI_SUPPORT_2024_API
27+
@inline(__always)
28+
package func projectEnum(
29+
at ptr: UnsafeRawPointer,
30+
tag: Int,
31+
_ body: (UnsafeRawPointer) -> Void
32+
) {
33+
projectEnumData(UnsafeMutableRawPointer(mutating: ptr))
34+
body(ptr)
35+
injectEnumTag(tag: UInt32(tag), UnsafeMutableRawPointer(mutating: ptr))
36+
}
37+
#endif
38+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// Metadata+OpenSwiftUITests.swift
3+
// OpenSwiftUICoreTests
4+
5+
import OpenGraphShims
6+
import OpenSwiftUICore
7+
import Testing
8+
9+
@Suite(.enabled(if: swiftToolchainSupported))
10+
struct Metadata_OpenSwiftUITests {
11+
class T1 {}
12+
13+
struct T2 {}
14+
15+
enum T3 {}
16+
17+
protocol P {}
18+
19+
@Test
20+
func isValueType() {
21+
#expect(Metadata(T1.self).isValueType == false)
22+
#expect(Metadata(T2.self).isValueType == true)
23+
#expect(Metadata(T3.self).isValueType == true)
24+
#expect(Metadata(P.self).isValueType == false)
25+
26+
#expect(Metadata(T1?.self).isValueType == true)
27+
#expect(Metadata((T1, T1).self).isValueType == true)
28+
}
29+
30+
@Test
31+
func genericType() {
32+
struct T1<A, B, C> {}
33+
struct T2<A, B> where A: P, B: P {}
34+
35+
struct P1: P {}
36+
struct P2: P {}
37+
let t1Type = Metadata(T1<Int, Double, Float>.self)
38+
#expect(t1Type.genericType(at: 0) == Int.self)
39+
#expect(t1Type.genericType(at: 1) == Double.self)
40+
#expect(t1Type.genericType(at: 2) == Float.self)
41+
42+
let t2Type = Metadata(T2<P1, P2>.self)
43+
#expect(t2Type.genericType(at: 0) == P1.self)
44+
#expect(t2Type.genericType(at: 1) == P2.self)
45+
}
46+
47+
#if OPENSWIFTUI_SUPPORT_2024_API
48+
@Test
49+
func projectEnum() {
50+
enum T: Equatable {
51+
case empty
52+
case int64(Int64)
53+
case int32(Int32)
54+
}
55+
56+
var t = T.empty
57+
let metadata = Metadata(T.self)
58+
59+
metadata.projectEnum(at: &t, tag: 0) { p in
60+
UnsafeMutableRawPointer(mutating: p)
61+
.assumingMemoryBound(to: Int64.self)
62+
.pointee = 1
63+
}
64+
#expect(t == .int64(1))
65+
66+
metadata.projectEnum(at: &t, tag: 1) { p in
67+
UnsafeMutableRawPointer(mutating: p)
68+
.assumingMemoryBound(to: Int32.self)
69+
.pointee = 1
70+
}
71+
#expect(t == .int32(1))
72+
73+
metadata.projectEnum(at: &t, tag: 2) { _ in }
74+
#expect(t == .empty)
75+
}
76+
#endif
77+
}

0 commit comments

Comments
 (0)