diff --git a/Sources/TesseractClient/TesseractDelegate.swift b/Sources/TesseractClient/TesseractDelegate.swift index a5dbcfc..df0066a 100644 --- a/Sources/TesseractClient/TesseractDelegate.swift +++ b/Sources/TesseractClient/TesseractDelegate.swift @@ -30,7 +30,6 @@ extension CKeyValue_CString__ClientStatus: CKeyValue, CPtr { public typealias CKey = CString public typealias CVal = ClientStatus public typealias SVal = KeyValue - public typealias Val = SVal public func copied() -> KeyValue { KeyValue(key: key.copied(), value: val.copied()) @@ -48,7 +47,6 @@ extension CKeyValue_CString__ClientStatus: CKeyValue, CPtr { extension ClientTransportsStatusRef: CPtrDictionaryPtrRef { public typealias SElement = KeyValue - public typealias RefVal = [KeyValue] public typealias CElement = CKeyValue_CString__ClientStatus } diff --git a/Sources/TesseractShared/Protocols/SubstrateService.swift b/Sources/TesseractShared/Protocols/SubstrateService.swift index 6010a14..8984279 100644 --- a/Sources/TesseractShared/Protocols/SubstrateService.swift +++ b/Sources/TesseractShared/Protocols/SubstrateService.swift @@ -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) -> Void) -> CResult? { _withOnCompleteContext(cb) { ctx, value, error in diff --git a/Sources/TesseractTransportsClient/CoreTransport.swift b/Sources/TesseractTransportsClient/CoreTransport.swift index 06bf83b..ee902fc 100644 --- a/Sources/TesseractTransportsClient/CoreTransport.swift +++ b/Sources/TesseractTransportsClient/CoreTransport.swift @@ -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) -> Void) -> CResult? { _withOnCompleteContext(cb) { ctx, value, error in diff --git a/Sources/TesseractTransportsShared/TesseractError+Ext.swift b/Sources/TesseractTransportsShared/TesseractError+Ext.swift index f52de56..5529fdf 100644 --- a/Sources/TesseractTransportsShared/TesseractError+Ext.swift +++ b/Sources/TesseractTransportsShared/TesseractError+Ext.swift @@ -14,7 +14,7 @@ import TesseractUtils public typealias TResult = Result public extension CFuturePtr { - init(_ cb: @escaping @Sendable () async -> Result) { + init(_ cb: @escaping @Sendable () async -> Result) { self.init { await cb().mapError { $0.tesseract.cError } } } } diff --git a/Sources/TesseractUtils/Array.swift b/Sources/TesseractUtils/Array.swift index 0f35b1e..705f33b 100644 --- a/Sources/TesseractUtils/Array.swift +++ b/Sources/TesseractUtils/Array.swift @@ -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 @@ -17,44 +17,37 @@ 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!, len: UInt) - - var ptr: UnsafePointer! { get set } - var len: UInt { get } -} +public protocol CArrayPtr: CArrayPtrRef, CPtr {} -extension CArrayPtrRef { - public var bufferPtr: UnsafeBufferPointer { - UnsafeBufferPointer(start: ptr, count: Int(len)) - } -} - -extension CArrayPtr { - public init(buffer: UnsafeBufferPointer) { +public extension CArrayPtrRef { + init(buffer: UnsafeBufferPointer) { self.init(ptr: buffer.baseAddress, len: UInt(buffer.count)) } - public init(buffer: UnsafeMutableBufferPointer) { + init(buffer: UnsafeMutableBufferPointer) { self.init(ptr: buffer.baseAddress, len: UInt(buffer.count)) } - public var bufferPtr: UnsafeBufferPointer { + var bufferPtr: UnsafeBufferPointer { UnsafeBufferPointer(start: ptr, count: Int(len)) } } +public extension CArrayPtr { + mutating func mutBufferPtr() -> UnsafeMutableBufferPointer { + UnsafeMutableBufferPointer(start: UnsafeMutablePointer(mutating: ptr), + count: Int(len)) + } +} + extension UnsafeBufferPointer { - public func arrayPtr() -> A where A.CElement == Element { + public func arrayPtr() -> A where A.CElement == Element { A(buffer: self) } } extension UnsafeMutableBufferPointer where Element == UInt8 { - public func arrayPtr() -> A where A.CElement == Element { + public func arrayPtr() -> A where A.CElement == Element { A(buffer: self) } } @@ -62,15 +55,13 @@ extension UnsafeMutableBufferPointer where Element == UInt8 { 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() } @@ -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() } @@ -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() result.reserveCapacity(memory.count) for (indx, var elem) in memory.enumerated() { diff --git a/Sources/TesseractUtils/Data.swift b/Sources/TesseractUtils/Data.swift index 1a56b94..6e865c8 100644 --- a/Sources/TesseractUtils/Data.swift +++ b/Sources/TesseractUtils/Data.swift @@ -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() } @@ -37,12 +37,22 @@ extension CData: CPtr { } } -extension CData { - public init(buffer: UnsafeBufferPointer) { +public extension CDataRef { + init(buffer: UnsafeBufferPointer) { self.init(ptr: buffer.baseAddress, len: UInt(buffer.count)) } - public init(buffer: UnsafeMutableBufferPointer) { + init(buffer: UnsafeMutableBufferPointer) { + self.init(ptr: buffer.baseAddress, len: UInt(buffer.count)) + } +} + +public extension CData { + init(buffer: UnsafeBufferPointer) { + self.init(ptr: buffer.baseAddress, len: UInt(buffer.count)) + } + + init(buffer: UnsafeMutableBufferPointer) { self.init(ptr: buffer.baseAddress, len: UInt(buffer.count)) } } @@ -96,4 +106,3 @@ extension UnsafeMutableBufferPointer where Element == UInt8 { CData(buffer: self) } } - diff --git a/Sources/TesseractUtils/Dictionary.swift b/Sources/TesseractUtils/Dictionary.swift index c1c953a..70c7977 100644 --- a/Sources/TesseractUtils/Dictionary.swift +++ b/Sources/TesseractUtils/Dictionary.swift @@ -67,15 +67,7 @@ public extension CDictionaryPtrRef { } } -public protocol CDictionaryPtr: CArrayPtr - where CElement: CKeyValue, SElement == CElement.SVal -{ - func copiedDictionary() -> D - where D.TKey == SElement.Key, D.TValue == SElement.Value - - func copiedDictionary(_ type: D.Type) -> D - where D.TKey == SElement.Key, D.TValue == SElement.Value - +public protocol CDictionaryPtr: CDictionaryPtrRef, CArrayPtr { mutating func ownedDictionary() -> D where D.TKey == SElement.Key, D.TValue == SElement.Value @@ -84,18 +76,6 @@ public protocol CDictionaryPtr: CArrayPtr } public extension CDictionaryPtr { - func copiedDictionary() -> D - where D.TKey == SElement.Key, D.TValue == SElement.Value - { - self.copiedDictionary(D.self) - } - - func copiedDictionary(_ type: D.Type) -> D - where D.TKey == SElement.Key, D.TValue == SElement.Value - { - type.init(kv: self.copied()) - } - mutating func ownedDictionary() -> D where D.TKey == SElement.Key, D.TValue == SElement.Value { diff --git a/Sources/TesseractUtils/Future.swift b/Sources/TesseractUtils/Future.swift index 0e4d610..0ab8e06 100644 --- a/Sources/TesseractUtils/Future.swift +++ b/Sources/TesseractUtils/Future.swift @@ -10,19 +10,19 @@ import CTesseractShared public protocol CFuturePtr: CType { associatedtype CVal: CType - associatedtype Val + associatedtype SVal var ptr: CAnyDropPtr { get set } // Consumes future. Will call free automatically - func onComplete(cb: @escaping (CResult) -> Void) -> CResult? + func onComplete(cb: @escaping (CResult) -> Void) -> CResult? // Frees unconsumed future. Call it only if you don't want to use the future mutating func free() -> CResult // Helpers for value converting - static func convert(cvalue: inout CVal) -> CResult - static func convert(value: inout Val) -> CResult + static func convert(cvalue: inout CVal) -> CResult + static func convert(value: inout SVal) -> CResult // Don't use this methods directly mutating func _onComplete(cb: @escaping (CResult) -> Void) -> CResult? @@ -30,7 +30,7 @@ public protocol CFuturePtr: CType { } extension CFuturePtr { - public init(_ cb: @escaping @Sendable () async throws -> Val) { + public init(_ cb: @escaping @Sendable () async throws -> SVal) { self = Self._wrapAsync { do { return .success(try await cb()) @@ -44,15 +44,15 @@ extension CFuturePtr { } } - public init(_ cb: @escaping @Sendable () async -> CResult) { + public init(_ cb: @escaping @Sendable () async -> CResult) { self = Self._wrapAsync(cb) } - public init(_ cb: @escaping @Sendable () async -> Result) { + public init(_ cb: @escaping @Sendable () async -> Result) { self = Self._wrapAsync { await cb().mapError { $0.cError } } } - public var value: Val { + public var value: SVal { get async throws { try await withUnsafeThrowingContinuation { cont in if let value = self.onComplete(cb: cont.resume) { @@ -62,7 +62,7 @@ extension CFuturePtr { } } - public var result: CResult { + public var result: CResult { get async { await withUnsafeContinuation { cont in if let value = self.onComplete(cb: cont.resume) { @@ -73,7 +73,7 @@ extension CFuturePtr { } // Should be mutating but has workaround for better API - public func onComplete(cb: @escaping (CResult) -> Void) -> CResult? { + public func onComplete(cb: @escaping (CResult) -> Void) -> CResult? { guard !self.ptr.isNull else { return .failure(.null(Self.self)) } var this = self withUnsafePointer(to: self) { ptr in @@ -96,8 +96,8 @@ extension CFuturePtr { } } -extension CFuturePtr where CVal: CPtr, Val == CVal.Val { - public static func convert(cvalue: inout CVal) -> CResult { +extension CFuturePtr where CVal: CPtr, SVal == CVal.SVal { + public static func convert(cvalue: inout CVal) -> CResult { return .success(cvalue.owned()) } } @@ -105,9 +105,9 @@ extension CFuturePtr where CVal: CPtr, Val == CVal.Val { extension CFuturePtr where CVal: OptionProtocol, CVal.OWrapped: CPtr, - Val == CVal.OWrapped.Val + SVal == CVal.OWrapped.SVal { - public static func convert(cvalue: inout CVal) -> CResult { + public static func convert(cvalue: inout CVal) -> CResult { switch cvalue.option { case .none: return .failure(.null(Self.self)) case .some(var val): @@ -118,72 +118,72 @@ extension CFuturePtr } } -extension CFuturePtr where Val: AsCPtrCopy, Val.CopyPtr == CVal { - public static func convert(value: inout Val) -> CResult { +extension CFuturePtr where SVal: AsCPtrCopy, SVal.CopyPtr == CVal { + public static func convert(value: inout SVal) -> CResult { .success(value.copiedPtr()) } } -extension CFuturePtr where Val: AsCPtrOwn, Val.OwnPtr == CVal { - static func convert(value: inout Val) -> CResult { +extension CFuturePtr where SVal: AsCPtrOwn, SVal.OwnPtr == CVal { + static func convert(value: inout SVal) -> CResult { .success(value.ownedPtr()) } } extension CFuturePtr - where Val: AsCPtrCopy, + where SVal: AsCPtrCopy, CVal: OptionProtocol, - Val.CopyPtr == CVal.OWrapped + SVal.CopyPtr == CVal.OWrapped { - public static func convert(value: inout Val) -> CResult { + public static func convert(value: inout SVal) -> CResult { .success(CVal(value.copiedPtr())) } } extension CFuturePtr - where Val: OptionProtocol, - Val.OWrapped: AsCPtrCopy, + where SVal: OptionProtocol, + SVal.OWrapped: AsCPtrCopy, CVal: OptionProtocol, - Val.OWrapped.CopyPtr == CVal.OWrapped + SVal.OWrapped.CopyPtr == CVal.OWrapped { - public static func convert(value: inout Val) -> CResult { + public static func convert(value: inout SVal) -> CResult { .success(CVal(value.option?.copiedPtr())) } } extension CFuturePtr - where Val: AsCPtrOwn, + where SVal: AsCPtrOwn, CVal: OptionProtocol, - Val.OwnPtr == CVal.OWrapped + SVal.OwnPtr == CVal.OWrapped { - static func convert(value: inout Val) -> CResult { + static func convert(value: inout SVal) -> CResult { .success(CVal(value.ownedPtr())) } } extension CFuturePtr - where Val: OptionProtocol, - Val.OWrapped: AsCPtrOwn, + where SVal: OptionProtocol, + SVal.OWrapped: AsCPtrOwn, CVal: OptionProtocol, - Val.OWrapped.OwnPtr == CVal.OWrapped + SVal.OWrapped.OwnPtr == CVal.OWrapped { - static func convert(value: inout Val) -> CResult { + static func convert(value: inout SVal) -> CResult { switch value.option { case .none: return .success(CVal(nil)) case .some(var val): let owned = val.ownedPtr() - value = Val(val) + value = SVal(val) return .success(CVal(owned)) } } } -extension CFuturePtr where Val: CValue, CVal == Val.CVal { - public static func convert(cvalue: inout CVal) -> CResult { - return .success(Val(cvalue: cvalue)) +extension CFuturePtr where SVal: CValue, CVal == SVal.CVal { + public static func convert(cvalue: inout CVal) -> CResult { + return .success(SVal(cvalue: cvalue)) } - public static func convert(value: inout Val) -> CResult { + public static func convert(value: inout SVal) -> CResult { return .success(value.asCValue) } } @@ -277,7 +277,7 @@ extension CFuturePtr { } } - public static func _wrapAsync(_ fn: @escaping @Sendable () async -> CResult) -> Self { + public static func _wrapAsync(_ fn: @escaping @Sendable () async -> CResult) -> Self { let context = CAsyncContext() var future = Self() diff --git a/Sources/TesseractUtils/FutureImpls.swift b/Sources/TesseractUtils/FutureImpls.swift index 120daca..9ca6514 100644 --- a/Sources/TesseractUtils/FutureImpls.swift +++ b/Sources/TesseractUtils/FutureImpls.swift @@ -10,7 +10,7 @@ import CTesseractShared extension CFutureNothing: CFuturePtr { public typealias CVal = Nothing - public typealias Val = Void + public typealias SVal = Void public mutating func _onComplete(cb: @escaping (CResult) -> Void) -> CResult? { _withOnCompleteContext(cb) { ctx, value, error in @@ -39,7 +39,7 @@ extension CFutureNothing: CFuturePtr { extension CFutureAnyRustPtr: CFuturePtr { public typealias CVal = CAnyRustPtr - public typealias Val = CAnyRustPtr + public typealias SVal = CAnyRustPtr public mutating func _onComplete(cb: @escaping (CResult) -> Void) -> CResult? { _withOnCompleteContext(cb) { ctx, value, error in @@ -68,7 +68,7 @@ extension CFutureAnyRustPtr: CFuturePtr { extension CFutureData: CFuturePtr { public typealias CVal = CData - public typealias Val = Data + public typealias SVal = Data public mutating func _onComplete(cb: @escaping (CResult) -> Void) -> CResult? { _withOnCompleteContext(cb) { ctx, value, error in @@ -89,7 +89,7 @@ extension CFutureData: CFuturePtr { extension CFutureString: CFuturePtr { public typealias CVal = CString - public typealias Val = String + public typealias SVal = String public mutating func _onComplete(cb: @escaping (CResult) -> Void) -> CResult? { _withOnCompleteContext(cb) { ctx, value, error in @@ -110,7 +110,7 @@ extension CFutureString: CFuturePtr { extension CFutureBool: CFuturePtr { public typealias CVal = Bool - public typealias Val = Bool + public typealias SVal = Bool public mutating func _onComplete(cb: @escaping (CResult) -> Void) -> CResult? { _withOnCompleteContext(cb) { ctx, value, error in diff --git a/Sources/TesseractUtils/Option.swift b/Sources/TesseractUtils/Option.swift index 34bfcb5..685e3a7 100644 --- a/Sources/TesseractUtils/Option.swift +++ b/Sources/TesseractUtils/Option.swift @@ -12,21 +12,15 @@ extension Optional: CType { } extension Optional: CPtrRef where Wrapped: CPtrRef { - public typealias RefVal = Optional + public typealias SVal = Optional - public func copied() -> RefVal { + public func copied() -> SVal { map { $0.copied() } } } extension Optional: CPtr where Wrapped: CPtr { - public typealias Val = Optional - - public func copied() -> Val { - map { $0.copied() } - } - - public mutating func owned() -> Val { + public mutating func owned() -> SVal { switch self { case .none: return nil case .some(var val): @@ -35,7 +29,7 @@ extension Optional: CPtr where Wrapped: CPtr { return owned } } - + public mutating func free() { if var val = self { val.free() @@ -129,7 +123,7 @@ extension COption where SOpVal: CValue, COpVal == SOpVal.CVal { } } -extension COption where COpVal: CPtr, SOpVal == COpVal.Val { +extension COption where COpVal: CPtr, SOpVal == COpVal.SVal { public func copied() -> SOpVal? { option.map { $0.copied() } } diff --git a/Sources/TesseractUtils/Primitives.swift b/Sources/TesseractUtils/Primitives.swift index 01a0422..bc6fa5f 100644 --- a/Sources/TesseractUtils/Primitives.swift +++ b/Sources/TesseractUtils/Primitives.swift @@ -25,9 +25,9 @@ extension UInt64: CType, CValue {} extension Bool: CType, CValue {} extension UnsafePointer: CPtrRef where Pointee: CPtr { - public typealias RefVal = Pointee.Val + public typealias SVal = Pointee.SVal - public func copied() -> Pointee.Val { + public func copied() -> Pointee.SVal { pointee.copied() } } diff --git a/Sources/TesseractUtils/Protocols.swift b/Sources/TesseractUtils/Protocols.swift index dfc064f..0bcbd8e 100644 --- a/Sources/TesseractUtils/Protocols.swift +++ b/Sources/TesseractUtils/Protocols.swift @@ -13,24 +13,21 @@ public protocol CType { init() } -// Structure with pointers inside +// Ref structure with pointers inside public protocol CPtrRef { - associatedtype RefVal + associatedtype SVal - func copied() -> RefVal + func copied() -> SVal } // Structure with pointers inside -public protocol CPtr { - associatedtype Val - - func copied() -> Val - mutating func owned() -> Val +public protocol CPtr: CPtrRef { + mutating func owned() -> SVal mutating func free() } // Swift value which can be simply converted from C (static struct) -// Don't use it for pointers. For pointers CPtr and CPtrMove should be used. +// Don't use it for pointers. For pointers CPtr should be used. public protocol CValue { associatedtype CVal: CType diff --git a/Sources/TesseractUtils/Result.swift b/Sources/TesseractUtils/Result.swift index 751f284..32868f6 100644 --- a/Sources/TesseractUtils/Result.swift +++ b/Sources/TesseractUtils/Result.swift @@ -11,7 +11,7 @@ import CTesseractShared public typealias CResult = Result // Result is unfinished -public protocol CResultPtr: CType, CPtr where Val == CResult { +public protocol CResultPtr: CType, CPtr where SVal == CResult { associatedtype CResTag: Equatable associatedtype CResVal: CType associatedtype SResVal @@ -38,7 +38,7 @@ public extension Result { public extension Result { static func wrap( ccall: @escaping (UnsafeMutablePointer, UnsafeMutablePointer) -> Bool - ) -> Result where F.Val: Error { + ) -> Result where F.SVal: Error { var val = S() var error = F() if !ccall(&val, &error) { return .failure(error.owned()) } @@ -66,7 +66,7 @@ public extension Result { static func wrap( ccall: @escaping (UnsafeMutablePointer, UnsafeMutablePointer) -> COptionResponseResult - ) -> Result where F.Val: Error { + ) -> Result where F.SVal: Error { var val = S() var error = F() switch ccall(&val, &error) { @@ -91,7 +91,7 @@ public extension Result { public extension Result { static func wrap ( ccall: @escaping (UnsafeMutablePointer) -> Bool - ) -> Result where F.Val: Error { + ) -> Result where F.SVal: Error { var error = F() if !ccall(&error) { return .failure(error.owned()) } return .success(())