···6161 // I hate this conditional cast, but Swift forces us to do this because Codable can't implement a specialized
6262 // function for any type, only the standard library types. This is the same method Foundation uses to detect
6363 // special encoding cases. It's still lame.
6464-6565- if let date = value as? Date {
6464+ switch value {
6565+ case let value as Date:
6666 switch options.dateEncodingStrategy {
6767 case .string:
6868- parent.register(StringDateOptimizer(value: date))
6868+ parent.register(StringDateOptimizer(value: value))
6969 case .float:
7070- parent.register(EpochFloatDateOptimizer(value: date))
7070+ parent.register(EpochFloatDateOptimizer(value: value))
7171 case .double:
7272- parent.register(EpochDoubleDateOptimizer(value: date))
7272+ parent.register(EpochDoubleDateOptimizer(value: value))
7373 }
7474- } else if let uuid = value as? UUID {
7575- parent.register(UUIDOptimizer(value: uuid))
7676- } else if let data = value as? Data {
7777- parent.register(ByteStringOptimizer(value: data))
7878- } else {
7474+ case let value as UUID:
7575+ parent.register(UUIDOptimizer(value: value))
7676+ case let value as Data:
7777+ parent.register(ByteStringOptimizer(value: value))
7878+// #if canImport(Float16)
7979+// case let value as Float16:
8080+// parent.register(Float16Optimizer(value: value))
8181+// #endif
8282+ default:
7983 try value.encode(to: self)
8084 }
8185 }
···11+//
22+// FloatOptimizer.swift
33+// SwiftCBOR
44+//
55+// Created by Khan Winter on 9/01/25.
66+//
77+88+// Would love to enable this, also see SingleValueCBOREncodingContainer for the other half of this
99+// but I'm not entirely sure how to make it work in that container and still compile on every machine
1010+// and platform this library supports...
1111+1212+// @available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
1313+// struct Float16Optimizer: EncodingOptimizer {
1414+// let value: Float16
1515+//
1616+// var type: MajorType { .simple }
1717+// var argument: UInt8 { 25 }
1818+// var contentSize: Int { 2 }
1919+//
2020+// init(value: Float16) {
2121+// self.value = value
2222+// }
2323+//
2424+// func writePayload(to data: inout Slice<UnsafeMutableRawBufferPointer>) {
2525+// assert(data.count >= 2)
2626+// var bytes = value.bitPattern.bigEndian
2727+// withUnsafeBytes(of: &bytes) { ptr in
2828+// UnsafeMutableRawBufferPointer(rebasing: data).copyBytes(from: ptr)
2929+// }
3030+// data.removeFirst(2)
3131+// }
3232+// }
+18
Tests/CBORTests/EncodableTests.swift
···228228 let data = try encoder.encode(1999)
229229 #expect(data == "1907CF".asHexData())
230230 }
231231+232232+ @Test(
233233+ .disabled("Disabled until we can figure out how to make Float16 compile on all platforms."),
234234+ arguments: [
235235+ ("F90000", 0),
236236+ ("F93C00", 1.0),
237237+ ("F9BE00", -1.5),
238238+ ("F97C00", .infinity),
239239+ ("F93E32", 1.548828125),
240240+ ("F9F021", -8456)
241241+ ]
242242+ )
243243+ func halfFloat(expected: String, value: Float16) throws {
244244+ let expectedData = expected.asHexData()
245245+ let encoder = CBOREncoder()
246246+ let data = try encoder.encode(value)
247247+ #expect(data == expectedData)
248248+ }
231249}