Skip to content

Commit

Permalink
utils refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Nov 22, 2023
1 parent 00fd2e2 commit 2acf8f0
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 161 deletions.
2 changes: 0 additions & 2 deletions Sources/TesseractClient/TesseractDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ extension CKeyValue_CString__ClientStatus: CKeyValue, CPtr {
public typealias CKey = CString
public typealias CVal = ClientStatus
public typealias SVal = KeyValue<String, Status>
public typealias Val = SVal

public func copied() -> KeyValue<String, Status> {
KeyValue(key: key.copied(), value: val.copied())
Expand All @@ -48,7 +47,6 @@ extension CKeyValue_CString__ClientStatus: CKeyValue, CPtr {

extension ClientTransportsStatusRef: CPtrDictionaryPtrRef {
public typealias SElement = KeyValue<String, Status>
public typealias RefVal = [KeyValue<String, Status>]
public typealias CElement = CKeyValue_CString__ClientStatus
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/TesseractShared/Protocols/SubstrateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ extension SubstrateAccountType: CValue {

extension CFuture_SubstrateGetAccountResponse: CFuturePtr {
public typealias CVal = CTesseract.SubstrateGetAccountResponse
public typealias Val = SubstrateGetAccountResponse
public typealias SVal = SubstrateGetAccountResponse

public mutating func _onComplete(cb: @escaping (CResult<CVal>) -> Void) -> CResult<CVal>? {
_withOnCompleteContext(cb) { ctx, value, error in
Expand Down
2 changes: 1 addition & 1 deletion Sources/TesseractTransportsClient/CoreTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension Status: AsCPtrCopy {

extension CFutureClientStatus: CFuturePtr {
public typealias CVal = ClientStatus
public typealias Val = Status
public typealias SVal = Status

mutating public func _onComplete(cb: @escaping (CResult<CVal>) -> Void) -> CResult<CVal>? {
_withOnCompleteContext(cb) { ctx, value, error in
Expand Down
2 changes: 1 addition & 1 deletion Sources/TesseractTransportsShared/TesseractError+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TesseractUtils
public typealias TResult<V> = Result<V, TesseractError>

public extension CFuturePtr {
init<E: TesseractErrorConvertible>(_ cb: @escaping @Sendable () async -> Result<Val, E>) {
init<E: TesseractErrorConvertible>(_ cb: @escaping @Sendable () async -> Result<SVal, E>) {
self.init { await cb().mapError { $0.tesseract.cError } }
}
}
Expand Down
84 changes: 27 additions & 57 deletions Sources/TesseractUtils/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public protocol CArrayPtrRef: CType, CPtrRef where RefVal == [SElement] {
public protocol CArrayPtrRef: CType, CPtrRef where SVal == [SElement] {
associatedtype CElement
associatedtype SElement

Expand All @@ -17,60 +17,51 @@ public protocol CArrayPtrRef: CType, CPtrRef where RefVal == [SElement] {
var len: UInt { get }
}

public protocol CArrayPtr: CType, CPtr where Val == [SElement] {
associatedtype CElement
associatedtype SElement

init(ptr: UnsafePointer<CElement>!, len: UInt)

var ptr: UnsafePointer<CElement>! { get set }
var len: UInt { get }
}
public protocol CArrayPtr: CArrayPtrRef, CPtr {}

extension CArrayPtrRef {
public var bufferPtr: UnsafeBufferPointer<CElement> {
UnsafeBufferPointer(start: ptr, count: Int(len))
}
}

extension CArrayPtr {
public init(buffer: UnsafeBufferPointer<CElement>) {
public extension CArrayPtrRef {
init(buffer: UnsafeBufferPointer<CElement>) {
self.init(ptr: buffer.baseAddress, len: UInt(buffer.count))
}

public init(buffer: UnsafeMutableBufferPointer<CElement>) {
init(buffer: UnsafeMutableBufferPointer<CElement>) {
self.init(ptr: buffer.baseAddress, len: UInt(buffer.count))
}

public var bufferPtr: UnsafeBufferPointer<CElement> {
var bufferPtr: UnsafeBufferPointer<CElement> {
UnsafeBufferPointer(start: ptr, count: Int(len))
}
}

public extension CArrayPtr {
mutating func mutBufferPtr() -> UnsafeMutableBufferPointer<CElement> {
UnsafeMutableBufferPointer(start: UnsafeMutablePointer(mutating: ptr),
count: Int(len))
}
}

extension UnsafeBufferPointer {
public func arrayPtr<A: CArrayPtr>() -> A where A.CElement == Element {
public func arrayPtr<A: CArrayPtrRef>() -> A where A.CElement == Element {
A(buffer: self)
}
}

extension UnsafeMutableBufferPointer where Element == UInt8 {
public func arrayPtr<A: CArrayPtr>() -> A where A.CElement == Element {
public func arrayPtr<A: CArrayPtrRef>() -> A where A.CElement == Element {
A(buffer: self)
}
}

public protocol CCopyArrayPtrRef: CArrayPtrRef where CElement == SElement {}

extension CCopyArrayPtrRef {
public func copied() -> RefVal { Array(bufferPtr) }
public func copied() -> SVal { Array(bufferPtr) }
}

public protocol CCopyArrayPtr: CArrayPtr where CElement == SElement {}
public protocol CCopyArrayPtr: CCopyArrayPtrRef, CArrayPtr {}

extension CCopyArrayPtr {
public func copied() -> Val { Array(bufferPtr) }

public mutating func owned() -> Val {
public mutating func owned() -> SVal {
defer { self.free() }
return self.copied()
}
Expand All @@ -81,21 +72,15 @@ public protocol CCopyConvertArrayPtrRef: CArrayPtrRef {
}

extension CCopyConvertArrayPtrRef {
public func copied() -> RefVal {
public func copied() -> SVal {
bufferPtr.map { Self.convert(element: $0) }
}
}

public protocol CCopyConvertArrayPtr: CArrayPtr {
static func convert(element: CElement) -> SElement
}
public protocol CCopyConvertArrayPtr: CCopyConvertArrayPtrRef, CArrayPtr {}

extension CCopyConvertArrayPtr {
public func copied() -> Val {
bufferPtr.map { Self.convert(element: $0) }
}

public mutating func owned() -> Val {
public mutating func owned() -> SVal {
defer { self.free() }
return self.copied()
}
Expand All @@ -110,38 +95,23 @@ extension CValueArrayPtrRef {
}
}

public protocol CValueArrayPtr: CCopyConvertArrayPtr
where CElement: CValue, SElement == CElement.CVal {}

extension CValueArrayPtr {
public static func convert(element: CElement) -> SElement {
element.asCValue
}
}
public protocol CValueArrayPtr: CValueArrayPtrRef, CArrayPtr {}

public protocol CPtrArrayPtrRef: CArrayPtrRef
where CElement: CPtr, SElement == CElement.Val {}
where CElement: CPtr, SElement == CElement.SVal {}

extension CPtrArrayPtrRef {
public func copied() -> RefVal {
public func copied() -> SVal {
bufferPtr.map { $0.copied() }
}
}

public protocol CPtrArrayPtr: CArrayPtr
where CElement: CPtr, SElement == CElement.Val {}
public protocol CPtrArrayPtr: CPtrArrayPtrRef, CArrayPtr {}

extension CPtrArrayPtr {
public func copied() -> Val {
bufferPtr.map { $0.copied() }
}

public mutating func owned() -> Val {
public mutating func owned() -> SVal {
defer { self.free() }
let memory = UnsafeMutableBufferPointer(
start: UnsafeMutablePointer(mutating: ptr),
count: Int(len)
)
let memory = mutBufferPtr()
var result = Array<SElement>()
result.reserveCapacity(memory.count)
for (indx, var elem) in memory.enumerated() {
Expand Down
27 changes: 18 additions & 9 deletions Sources/TesseractUtils/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ extension CDataRef: CType {}
extension CData: CType {}

extension CDataRef: CPtrRef {
public typealias Val = Data
public typealias SVal = Data

public func copied() -> Data {
public func copied() -> SVal {
Data(bytes: self.ptr, count: Int(self.len))
}
}

extension CData: CPtr {
public typealias Val = Data
public typealias SVal = Data

public func copied() -> Data {
public func copied() -> SVal {
Data(bytes: self.ptr, count: Int(self.len))
}

public mutating func owned() -> Data {
public mutating func owned() -> SVal {
defer { self.free() }
return self.copied()
}
Expand All @@ -37,12 +37,22 @@ extension CData: CPtr {
}
}

extension CData {
public init(buffer: UnsafeBufferPointer<UInt8>) {
public extension CDataRef {
init(buffer: UnsafeBufferPointer<UInt8>) {
self.init(ptr: buffer.baseAddress, len: UInt(buffer.count))
}

public init(buffer: UnsafeMutableBufferPointer<UInt8>) {
init(buffer: UnsafeMutableBufferPointer<UInt8>) {
self.init(ptr: buffer.baseAddress, len: UInt(buffer.count))
}
}

public extension CData {
init(buffer: UnsafeBufferPointer<UInt8>) {
self.init(ptr: buffer.baseAddress, len: UInt(buffer.count))
}

init(buffer: UnsafeMutableBufferPointer<UInt8>) {
self.init(ptr: buffer.baseAddress, len: UInt(buffer.count))
}
}
Expand Down Expand Up @@ -96,4 +106,3 @@ extension UnsafeMutableBufferPointer where Element == UInt8 {
CData(buffer: self)
}
}

22 changes: 1 addition & 21 deletions Sources/TesseractUtils/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,7 @@ public extension CDictionaryPtrRef {
}
}

public protocol CDictionaryPtr: CArrayPtr
where CElement: CKeyValue, SElement == CElement.SVal
{
func copiedDictionary<D: FromKeyValueArray>() -> D
where D.TKey == SElement.Key, D.TValue == SElement.Value

func copiedDictionary<D: FromKeyValueArray>(_ type: D.Type) -> D
where D.TKey == SElement.Key, D.TValue == SElement.Value

public protocol CDictionaryPtr: CDictionaryPtrRef, CArrayPtr {
mutating func ownedDictionary<D: FromKeyValueArray>() -> D
where D.TKey == SElement.Key, D.TValue == SElement.Value

Expand All @@ -84,18 +76,6 @@ public protocol CDictionaryPtr: CArrayPtr
}

public extension CDictionaryPtr {
func copiedDictionary<D: FromKeyValueArray>() -> D
where D.TKey == SElement.Key, D.TValue == SElement.Value
{
self.copiedDictionary(D.self)
}

func copiedDictionary<D: FromKeyValueArray>(_ type: D.Type) -> D
where D.TKey == SElement.Key, D.TValue == SElement.Value
{
type.init(kv: self.copied())
}

mutating func ownedDictionary<D: FromKeyValueArray>() -> D
where D.TKey == SElement.Key, D.TValue == SElement.Value
{
Expand Down
Loading

0 comments on commit 2acf8f0

Please sign in to comment.