77
88public import OpenGraph_SPI
99
10+ // MARK: TupleType
11+
1012extension TupleType {
1113 public init ( _ types: [ Any . Type ] ) {
1214 self . init ( count: types. count, elements: types. map ( Metadata . init) )
1315 }
1416
1517 public init ( _ type: Any . Type ) {
16- self . init ( rawValue: unsafeBitCast ( type, to: UnsafePointer< OGSwiftMetadata > . self ) )
18+ self . init ( rawValue: unsafeBitCast ( type, to: UnsafePointer< _Metadata > . self ) )
1719 }
1820
21+ public var isEmpty : Bool { count == 0 }
22+ public var indices : Range < Int > { 0 ..< count }
23+
1924 public var type : Any . Type {
2025 unsafeBitCast ( rawValue, to: Any . Type. self)
2126 }
2227
23- public var isEmpty : Bool {
24- count == 0
25- }
26-
27- public var indices : Range < Int > {
28- 0 ..< count
29- }
30-
3128 public func type( at index: Int ) -> Any . Type {
3229 elementType ( at: index) . type
3330 }
@@ -47,3 +44,86 @@ extension TupleType {
4744
4845@_silgen_name ( " OGTupleWithBuffer " )
4946public func withUnsafeTuple( of type: TupleType , count: Int , _ body: ( UnsafeMutableTuple ) -> ( ) )
47+
48+ // MARK: - UnsafeTuple
49+
50+ extension UnsafeTuple {
51+ public var count : Int { type. count }
52+ public var isEmpty : Bool { type. isEmpty }
53+ public var indices : Range < Int > { type. indices }
54+
55+ public func address< T> ( as _: T . Type = T . self) -> UnsafePointer < T > {
56+ value. assumingMemoryBound ( to: T . self)
57+ }
58+
59+ public func address< T> ( of index: Int , as _: T . Type = T . self) -> UnsafePointer < T > {
60+ value. advanced ( by: type. elementOffset ( at: index, type: Metadata ( T . self) ) )
61+ . assumingMemoryBound ( to: T . self)
62+ }
63+
64+ public subscript< T > ( ) - > T {
65+ unsafeAddress { address ( as: T . self) }
66+ }
67+
68+ public subscript< T > ( _ index: Int) - > T {
69+ unsafeAddress { address ( of: index, as: T . self) }
70+ }
71+ }
72+
73+ // MARK: - UnsafeMutableTuple
74+
75+ @_silgen_name ( " swift_slowAlloc " )
76+ private func slowAlloc( _ size: Int , _ alignMask: Int ) -> UnsafeMutableRawPointer
77+
78+ @_silgen_name ( " swift_slowDealloc " )
79+ private func slowDealloc( _ ptr: UnsafeMutableRawPointer , _ size: Int , _ alignMask: Int )
80+
81+ extension UnsafeMutableTuple {
82+ public init( with tupleType: TupleType) {
83+ self . init ( type: tupleType, value: slowAlloc ( tupleType. size, - 1 ) )
84+ }
85+
86+ public func initialize< T> ( at index: Int , to element: T ) {
87+ withUnsafePointer ( to: element) { elementPointer in
88+ type. setElement ( in: value, at: index, from: elementPointer, options: . initCopy)
89+ }
90+ }
91+
92+ public func deinitialize( ) {
93+ type. destory ( value)
94+ }
95+
96+ public func deinitialize( at index: Int ) {
97+ type. destory ( value, at: index)
98+ }
99+
100+ public func deallocate( initialized: Bool ) {
101+ if initialized {
102+ deinitialize ( )
103+ }
104+ slowDealloc ( value, - 1 , - 1 )
105+ }
106+
107+ public var count : Int { type. count }
108+ public var isEmpty : Bool { type. isEmpty }
109+ public var indices : Range < Int > { type. indices }
110+
111+ public func address< T> ( as _: T . Type = T . self) -> UnsafeMutablePointer < T > {
112+ value. assumingMemoryBound ( to: T . self)
113+ }
114+
115+ public func address< T> ( of index: Int , as _: T . Type = T . self) -> UnsafeMutablePointer < T > {
116+ value. advanced ( by: type. elementOffset ( at: index, type: Metadata ( T . self) ) )
117+ . assumingMemoryBound ( to: T . self)
118+ }
119+
120+ public subscript< T > ( ) - > T {
121+ unsafeAddress { UnsafePointer ( address ( as: T . self) ) }
122+ unsafeMutableAddress { address ( as: T . self) }
123+ }
124+
125+ public subscript< T > ( _ index: Int) - > T {
126+ unsafeAddress { UnsafePointer ( address ( of: index, as: T . self) ) }
127+ unsafeMutableAddress { address ( of: index, as: T . self) }
128+ }
129+ }
0 commit comments