Skip to content

Commit b9b1926

Browse files
committed
add serialization fast paths for OcaMethodID and OcaPropertyID
1 parent f515db3 commit b9b1926

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

Sources/SwiftOCA/OCC/ControlDataTypes/BaseDataTypes.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public struct OcaPropertyID: Codable, Hashable, Equatable, Comparable, Sendable,
118118
let defLevel: OcaUint16
119119
let propertyIndex: OcaUint16
120120

121+
public init(defLevel: OcaUint16, propertyIndex: OcaUint16) {
122+
self.defLevel = defLevel
123+
self.propertyIndex = propertyIndex
124+
}
125+
121126
public init(_ string: OcaString) {
122127
let s = string.split(separator: ".", maxSplits: 1).map { OcaUint16($0)! }
123128
defLevel = s[0]
@@ -139,6 +144,33 @@ public struct OcaPropertyID: Codable, Hashable, Equatable, Comparable, Sendable,
139144
lhs.defLevel < rhs.defLevel
140145
}
141146
}
147+
148+
@_spi(SwiftOCAPrivate)
149+
public init?(data: Data) {
150+
precondition(MemoryLayout<Self>.size == 4)
151+
guard data.count >= 4 else { return nil }
152+
153+
let encodedSelf = data.withUnsafeBytes {
154+
$0.withMemoryRebound(to: Self.self) {
155+
$0.baseAddress!.pointee
156+
}
157+
}
158+
159+
self.init(
160+
defLevel: UInt16(bigEndian: encodedSelf.defLevel),
161+
propertyIndex: UInt16(bigEndian: encodedSelf.propertyIndex)
162+
)
163+
}
164+
165+
@_spi(SwiftOCAPrivate)
166+
public var data: Data {
167+
withUnsafeBytes(of: Self(
168+
defLevel: defLevel.bigEndian,
169+
propertyIndex: propertyIndex.bigEndian
170+
)) {
171+
Data($0)
172+
}
173+
}
142174
}
143175

144176
public enum OcaIODirection: OcaUint8, Codable, Sendable, CaseIterable {
@@ -363,6 +395,11 @@ public struct OcaMethodID: Codable, Hashable, Sendable, CustomStringConvertible,
363395
public let defLevel: OcaUint16
364396
public let methodIndex: OcaUint16
365397

398+
public init(defLevel: OcaUint16, methodIndex: OcaUint16) {
399+
self.defLevel = defLevel
400+
self.methodIndex = methodIndex
401+
}
402+
366403
public init(_ string: OcaString) {
367404
let s = string.split(separator: ".", maxSplits: 1).map { OcaUint16($0)! }
368405
defLevel = s[0]
@@ -388,6 +425,33 @@ public struct OcaMethodID: Codable, Hashable, Sendable, CustomStringConvertible,
388425
self.init(value)
389426
}
390427

428+
@_spi(SwiftOCAPrivate)
429+
public init?(data: Data) {
430+
precondition(MemoryLayout<Self>.size == 4)
431+
guard data.count >= 4 else { return nil }
432+
433+
let encodedSelf = data.withUnsafeBytes {
434+
$0.withMemoryRebound(to: Self.self) {
435+
$0.baseAddress!.pointee
436+
}
437+
}
438+
439+
self.init(
440+
defLevel: UInt16(bigEndian: encodedSelf.defLevel),
441+
methodIndex: UInt16(bigEndian: encodedSelf.methodIndex)
442+
)
443+
}
444+
445+
@_spi(SwiftOCAPrivate)
446+
public var data: Data {
447+
withUnsafeBytes(of: Self(
448+
defLevel: defLevel.bigEndian,
449+
methodIndex: methodIndex.bigEndian
450+
)) {
451+
Data($0)
452+
}
453+
}
454+
391455
public var description: String {
392456
"\(defLevel).\(methodIndex)"
393457
}

Sources/SwiftOCADevice/OCA/ControllerDefaultSubscribing.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import AsyncAlgorithms
1818
import AsyncExtensions
1919
import Foundation
2020
import Logging
21+
@_spi(SwiftOCAPrivate)
2122
import SwiftOCA
2223

2324
public protocol OcaControllerDefaultSubscribing: OcaController {
@@ -127,7 +128,7 @@ public extension OcaControllerDefaultSubscribing {
127128
}
128129

129130
let property: OcaPropertyID? = if event.eventID == OcaPropertyChangedEventID {
130-
try Ocp1Decoder().decode(OcaPropertyID.self, from: parameters)
131+
OcaPropertyID(data: parameters)
131132
} else {
132133
nil
133134
}

0 commit comments

Comments
 (0)