Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions Sources/OpenGraph/Runtime/TupleType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> { 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<T>(at index: Int, as type: T.Type) -> Int {
elementOffset(at: index, type: Metadata(type))
}


@_transparent
public func setElement<T>(in tupleValue: UnsafeMutableRawPointer, at index: Int, from srcValue: UnsafePointer<T>, options: CopyOptions) {
__OGTupleSetElement(self, tupleValue, index, srcValue, Metadata(T.self), options)
}


@_transparent
public func getElement<T>(in tupleValue: UnsafeMutableRawPointer, at index: Int, to dstValue: UnsafeMutablePointer<T>, options: CopyOptions) {
__OGTupleGetElement(self, tupleValue, index, dstValue, Metadata(T.self), options)
}
Expand Down Expand Up @@ -76,6 +86,7 @@ extension UnsafeTuple {
// MARK: - UnsafeMutableTuple

extension UnsafeMutableTuple {
@_transparent
public init(with tupleType: TupleType) {
self.init(
type: tupleType,
Expand All @@ -86,48 +97,61 @@ extension UnsafeMutableTuple {
)
}

@_transparent
public func initialize<T>(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()
}
value.deallocate()
}

@_transparent
public var count: Int { type.count }

@_transparent
public var isEmpty: Bool { type.isEmpty }

@_transparent
public var indices: Range<Int> { type.indices }

@_transparent
public func address<T>(as _: T.Type = T.self) -> UnsafeMutablePointer<T> {
guard type.type == T.self else {
preconditionFailure()
}
return value.assumingMemoryBound(to: T.self)
}

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

@_transparent
public subscript<T>() -> T {
unsafeAddress { UnsafePointer(address(as: T.self)) }
nonmutating unsafeMutableAddress { address(as: T.self) }
}

@_transparent
public subscript<T>(_ index: Int) -> T {
unsafeAddress { UnsafePointer(address(of: index, as: T.self)) }
nonmutating unsafeMutableAddress { address(of: index, as: T.self) }
Expand Down
Loading