Skip to content

Commit 16c5f5b

Browse files
authored
Add @_transparent for TupleType (#121)
1 parent 938fe2e commit 16c5f5b

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

Sources/OpenGraph/Runtime/TupleType.swift

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,43 @@ public import OpenGraph_SPI
1010
// MARK: TupleType
1111

1212
extension TupleType {
13+
@_transparent
1314
public init(_ types: [Any.Type]) {
1415
self.init(count: types.count, elements: types.map(Metadata.init))
1516
}
16-
17+
18+
@_transparent
1719
public init(_ type: Any.Type) {
1820
self.init(rawValue: unsafeBitCast(type, to: UnsafePointer<_Metadata>.self))
1921
}
2022

23+
@_transparent
2124
public var isEmpty: Bool { count == 0 }
25+
26+
@_transparent
2227
public var indices: Range<Int> { 0 ..< count }
23-
28+
29+
@_transparent
2430
public var type: Any.Type {
2531
unsafeBitCast(rawValue, to: Any.Type.self)
2632
}
27-
33+
34+
@_transparent
2835
public func type(at index: Int) -> Any.Type {
2936
elementType(at: index).type
3037
}
31-
38+
39+
@_transparent
3240
public func offset<T>(at index: Int, as type: T.Type) -> Int {
3341
elementOffset(at: index, type: Metadata(type))
3442
}
35-
43+
44+
@_transparent
3645
public func setElement<T>(in tupleValue: UnsafeMutableRawPointer, at index: Int, from srcValue: UnsafePointer<T>, options: CopyOptions) {
3746
__OGTupleSetElement(self, tupleValue, index, srcValue, Metadata(T.self), options)
3847
}
39-
48+
49+
@_transparent
4050
public func getElement<T>(in tupleValue: UnsafeMutableRawPointer, at index: Int, to dstValue: UnsafeMutablePointer<T>, options: CopyOptions) {
4151
__OGTupleGetElement(self, tupleValue, index, dstValue, Metadata(T.self), options)
4252
}
@@ -76,6 +86,7 @@ extension UnsafeTuple {
7686
// MARK: - UnsafeMutableTuple
7787

7888
extension UnsafeMutableTuple {
89+
@_transparent
7990
public init(with tupleType: TupleType) {
8091
self.init(
8192
type: tupleType,
@@ -86,48 +97,61 @@ extension UnsafeMutableTuple {
8697
)
8798
}
8899

100+
@_transparent
89101
public func initialize<T>(at index: Int, to element: T) {
90102
withUnsafePointer(to: element) { elementPointer in
91103
type.setElement(in: value, at: index, from: elementPointer, options: .initCopy)
92104
}
93105
}
94106

107+
@_transparent
95108
public func deinitialize() {
96109
type.destroy(value)
97110
}
98111

112+
@_transparent
99113
public func deinitialize(at index: Int) {
100114
type.destroy(value, at: index)
101115
}
102116

117+
@_transparent
103118
public func deallocate(initialized: Bool) {
104119
if initialized {
105120
deinitialize()
106121
}
107122
value.deallocate()
108123
}
109124

125+
@_transparent
110126
public var count: Int { type.count }
127+
128+
@_transparent
111129
public var isEmpty: Bool { type.isEmpty }
130+
131+
@_transparent
112132
public var indices: Range<Int> { type.indices }
113133

134+
@_transparent
114135
public func address<T>(as _: T.Type = T.self) -> UnsafeMutablePointer<T> {
115136
guard type.type == T.self else {
116137
preconditionFailure()
117138
}
118139
return value.assumingMemoryBound(to: T.self)
119140
}
120141

142+
@_transparent
121143
public func address<T>(of index: Int, as _: T.Type = T.self) -> UnsafeMutablePointer<T> {
122144
value.advanced(by: type.elementOffset(at: index, type: Metadata(T.self)))
123145
.assumingMemoryBound(to: T.self)
124146
}
125147

148+
@_transparent
126149
public subscript<T>() -> T {
127150
unsafeAddress { UnsafePointer(address(as: T.self)) }
128151
nonmutating unsafeMutableAddress { address(as: T.self) }
129152
}
130153

154+
@_transparent
131155
public subscript<T>(_ index: Int) -> T {
132156
unsafeAddress { UnsafePointer(address(of: index, as: T.self)) }
133157
nonmutating unsafeMutableAddress { address(of: index, as: T.self) }

0 commit comments

Comments
 (0)