|
| 1 | +// |
| 2 | +// TupleType.swift |
| 3 | +// OpenGraph |
| 4 | +// |
| 5 | +// Audited for iOS 18.0 |
| 6 | +// Status: WIP |
| 7 | + |
| 8 | +public import OpenGraph_SPI |
| 9 | + |
| 10 | +// MARK: TupleType |
| 11 | + |
| 12 | +extension TupleType { |
| 13 | + public init(_ types: [Any.Type]) { |
| 14 | + self.init(count: types.count, elements: types.map(Metadata.init)) |
| 15 | + } |
| 16 | + |
| 17 | + public init(_ type: Any.Type) { |
| 18 | + self.init(rawValue: unsafeBitCast(type, to: UnsafePointer<_Metadata>.self)) |
| 19 | + } |
| 20 | + |
| 21 | + public var isEmpty: Bool { count == 0 } |
| 22 | + public var indices: Range<Int> { 0 ..< count } |
| 23 | + |
| 24 | + public var type: Any.Type { |
| 25 | + unsafeBitCast(rawValue, to: Any.Type.self) |
| 26 | + } |
| 27 | + |
| 28 | + public func type(at index: Int) -> Any.Type { |
| 29 | + elementType(at: index).type |
| 30 | + } |
| 31 | + |
| 32 | + public func offset<T>(at index: Int, as type: T.Type) -> Int { |
| 33 | + elementOffset(at: index, type: Metadata(type)) |
| 34 | + } |
| 35 | + |
| 36 | + public func setElement<T>(in tupleValue: UnsafeMutableRawPointer, at index: Int, from srcValue: UnsafePointer<T>, options: CopyOptions) { |
| 37 | + __OGTupleSetElement(self, tupleValue, index, srcValue, Metadata(T.self), options) |
| 38 | + } |
| 39 | + |
| 40 | + public func getElement<T>(in tupleValue: UnsafeMutableRawPointer, at index: Int, to dstValue: UnsafeMutablePointer<T>, options: CopyOptions) { |
| 41 | + __OGTupleGetElement(self, tupleValue, index, dstValue, Metadata(T.self), options) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +@_silgen_name("OGTupleWithBuffer") |
| 46 | +public 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 | + guard type.type == T.self else { |
| 57 | + preconditionFailure() |
| 58 | + } |
| 59 | + return value.assumingMemoryBound(to: T.self) |
| 60 | + } |
| 61 | + |
| 62 | + public func address<T>(of index: Int, as _: T.Type = T.self) -> UnsafePointer<T> { |
| 63 | + value.advanced(by: type.elementOffset(at: index, type: Metadata(T.self))) |
| 64 | + .assumingMemoryBound(to: T.self) |
| 65 | + } |
| 66 | + |
| 67 | + public subscript<T>() -> T { |
| 68 | + unsafeAddress { address(as: T.self) } |
| 69 | + } |
| 70 | + |
| 71 | + public subscript<T>(_ index: Int) -> T { |
| 72 | + unsafeAddress { address(of: index, as: T.self) } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// MARK: - UnsafeMutableTuple |
| 77 | + |
| 78 | +@_silgen_name("swift_slowAlloc") |
| 79 | +private func slowAlloc(_ size: Int, _ alignMask: Int) -> UnsafeMutableRawPointer |
| 80 | + |
| 81 | +@_silgen_name("swift_slowDealloc") |
| 82 | +private func slowDealloc(_ ptr: UnsafeMutableRawPointer, _ size: Int, _ alignMask: Int) |
| 83 | + |
| 84 | +extension UnsafeMutableTuple { |
| 85 | + public init(with tupleType: TupleType) { |
| 86 | + self.init(type: tupleType, value: slowAlloc(tupleType.size, -1)) |
| 87 | + } |
| 88 | + |
| 89 | + public func initialize<T>(at index: Int, to element: T) { |
| 90 | + withUnsafePointer(to: element) { elementPointer in |
| 91 | + type.setElement(in: value, at: index, from: elementPointer, options: .initCopy) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public func deinitialize() { |
| 96 | + type.destroy(value) |
| 97 | + } |
| 98 | + |
| 99 | + public func deinitialize(at index: Int) { |
| 100 | + type.destroy(value, at: index) |
| 101 | + } |
| 102 | + |
| 103 | + public func deallocate(initialized: Bool) { |
| 104 | + if initialized { |
| 105 | + deinitialize() |
| 106 | + } |
| 107 | + slowDealloc(value, -1, -1) |
| 108 | + } |
| 109 | + |
| 110 | + public var count: Int { type.count } |
| 111 | + public var isEmpty: Bool { type.isEmpty } |
| 112 | + public var indices: Range<Int> { type.indices } |
| 113 | + |
| 114 | + public func address<T>(as _: T.Type = T.self) -> UnsafeMutablePointer<T> { |
| 115 | + guard type.type == T.self else { |
| 116 | + preconditionFailure() |
| 117 | + } |
| 118 | + return value.assumingMemoryBound(to: T.self) |
| 119 | + } |
| 120 | + |
| 121 | + public func address<T>(of index: Int, as _: T.Type = T.self) -> UnsafeMutablePointer<T> { |
| 122 | + value.advanced(by: type.elementOffset(at: index, type: Metadata(T.self))) |
| 123 | + .assumingMemoryBound(to: T.self) |
| 124 | + } |
| 125 | + |
| 126 | + public subscript<T>() -> T { |
| 127 | + unsafeAddress { UnsafePointer(address(as: T.self)) } |
| 128 | + nonmutating unsafeMutableAddress { address(as: T.self) } |
| 129 | + } |
| 130 | + |
| 131 | + public subscript<T>(_ index: Int) -> T { |
| 132 | + unsafeAddress { UnsafePointer(address(of: index, as: T.self)) } |
| 133 | + nonmutating unsafeMutableAddress { address(of: index, as: T.self) } |
| 134 | + } |
| 135 | +} |
0 commit comments