diff --git a/Sources/OpenGraph/Runtime/TupleType.swift b/Sources/OpenGraph/Runtime/TupleType.swift index fddf1e4b..cfea0135 100644 --- a/Sources/OpenGraph/Runtime/TupleType.swift +++ b/Sources/OpenGraph/Runtime/TupleType.swift @@ -10,33 +10,43 @@ public import OpenGraph_SPI // MARK: TupleType extension TupleType { + @_transparent public init(_ types: [Any.Type]) { self.init(count: types.count, elements: types.map(Metadata.init)) } - + + @_transparent public init(_ type: Any.Type) { self.init(rawValue: unsafeBitCast(type, to: UnsafePointer<_Metadata>.self)) } + @_transparent public var isEmpty: Bool { count == 0 } + + @_transparent public var indices: Range { 0 ..< count } - + + @_transparent public var type: Any.Type { unsafeBitCast(rawValue, to: Any.Type.self) } - + + @_transparent public func type(at index: Int) -> Any.Type { elementType(at: index).type } - + + @_transparent public func offset(at index: Int, as type: T.Type) -> Int { elementOffset(at: index, type: Metadata(type)) } - + + @_transparent public func setElement(in tupleValue: UnsafeMutableRawPointer, at index: Int, from srcValue: UnsafePointer, options: CopyOptions) { __OGTupleSetElement(self, tupleValue, index, srcValue, Metadata(T.self), options) } - + + @_transparent public func getElement(in tupleValue: UnsafeMutableRawPointer, at index: Int, to dstValue: UnsafeMutablePointer, options: CopyOptions) { __OGTupleGetElement(self, tupleValue, index, dstValue, Metadata(T.self), options) } @@ -76,6 +86,7 @@ extension UnsafeTuple { // MARK: - UnsafeMutableTuple extension UnsafeMutableTuple { + @_transparent public init(with tupleType: TupleType) { self.init( type: tupleType, @@ -86,20 +97,24 @@ extension UnsafeMutableTuple { ) } + @_transparent public func initialize(at index: Int, to element: T) { withUnsafePointer(to: element) { elementPointer in type.setElement(in: value, at: index, from: elementPointer, options: .initCopy) } } + @_transparent public func deinitialize() { type.destroy(value) } + @_transparent public func deinitialize(at index: Int) { type.destroy(value, at: index) } + @_transparent public func deallocate(initialized: Bool) { if initialized { deinitialize() @@ -107,10 +122,16 @@ extension UnsafeMutableTuple { value.deallocate() } + @_transparent public var count: Int { type.count } + + @_transparent public var isEmpty: Bool { type.isEmpty } + + @_transparent public var indices: Range { type.indices } + @_transparent public func address(as _: T.Type = T.self) -> UnsafeMutablePointer { guard type.type == T.self else { preconditionFailure() @@ -118,16 +139,19 @@ extension UnsafeMutableTuple { return value.assumingMemoryBound(to: T.self) } + @_transparent public func address(of index: Int, as _: T.Type = T.self) -> UnsafeMutablePointer { value.advanced(by: type.elementOffset(at: index, type: Metadata(T.self))) .assumingMemoryBound(to: T.self) } + @_transparent public subscript() -> T { unsafeAddress { UnsafePointer(address(as: T.self)) } nonmutating unsafeMutableAddress { address(as: T.self) } } + @_transparent public subscript(_ index: Int) -> T { unsafeAddress { UnsafePointer(address(of: index, as: T.self)) } nonmutating unsafeMutableAddress { address(of: index, as: T.self) }