mikeash / binarycoder Goto Github PK
View Code? Open in Web Editor NEWAn example implementation of Swift.Codable using a simple binary format
License: MIT License
An example implementation of Swift.Codable using a simple binary format
License: MIT License
In the Array.init(fromBinary:) extension, the array elements should likewise be initialized with init(fromBinary:), not init(from:) as in the code. As it is, array elements are decoded by the implicit decoder but not by explicit binary decoders.
This:
let decoded = try binaryElement.init(from: decoder)
should be:
let decoded = try binaryElement.init(fromBinary: decoder)
@mikeash getting the following compile errors building this project.
Swift Compiler Error Group
BinaryCodableExtensions.swift:6:1: Type 'Element' does not conform to protocol 'Decodable'
BinaryCodableExtensions.swift:6:1: 'BinaryDecodable' requires that 'Element' conform to 'Decodable'
BinaryCodableExtensions.swift:6:1: Requirement specified as 'Element' : 'Decodable'
BinaryCodableExtensions.swift:6:1: Requirement from conditional conformance of 'Array<Element>' to 'Decodable'
BinaryCodableExtensions.swift:6:1: Type 'Element' does not conform to protocol 'Encodable'
BinaryCodableExtensions.swift:6:1: 'BinaryEncodable' requires that 'Element' conform to 'Encodable'
BinaryCodableExtensions.swift:6:1: Requirement specified as 'Element' : 'Encodable'
BinaryCodableExtensions.swift:6:1: Requirement from conditional conformance of 'Array<Element>' to 'Encodable'
I'm not too familiar with Swift's new conditional conformance but attempted to fix these in BinaryCodableExtensions.swift
. See:
/// Implementations of BinaryCodable for built-in types.
import Foundation
// AB: addition
extension Dictionary: BinaryCodable where Key: BinaryCodable, Value: BinaryCodable {
public func binaryEncode(to encoder: BinaryEncoder) throws {
try encoder.encode(self.count)
for pair in self {
try pair.key.encode(to: encoder)
try pair.value.encode(to: encoder)
}
}
public init(fromBinary decoder: BinaryDecoder) throws {
let binaryKeyElement = Key.self
let binaryValueElement = Value.self
let count = try decoder.decode(Int.self)
self.init()
self.reserveCapacity(count)
for _ in 0 ..< count {
let decodedKey = try binaryKeyElement.init(from: decoder)
let decodedValue = try binaryValueElement.init(from: decoder)
self[decodedKey] = decodedValue
}
}
}
extension Array: BinaryCodable where Element: BinaryCodable {
public func binaryEncode(to encoder: BinaryEncoder) throws {
try encoder.encode(self.count)
for element in self {
try element.encode(to: encoder)
}
}
public init(fromBinary decoder: BinaryDecoder) throws {
let binaryElement = Element.self
let count = try decoder.decode(Int.self)
self.init()
self.reserveCapacity(count)
for _ in 0 ..< count {
let decoded = try binaryElement.init(from: decoder)
self.append(decoded)
}
}
}
extension String: BinaryCodable {
public func binaryEncode(to encoder: BinaryEncoder) throws {
try (Array(self.utf8) as! BinaryCodable).binaryEncode(to: encoder)
}
public init(fromBinary decoder: BinaryDecoder) throws {
let utf8: [UInt8] = try (Array as [BinaryCodable.Type]).init(fromBinary: decoder) as! [UInt8]
if let str = String(bytes: utf8, encoding: .utf8) {
self = str
} else {
throw BinaryDecoder.Error.invalidUTF8(utf8)
}
}
}
But running into:
Cannot convert value of type 'Array<_>.Type' to type '[BinaryCodable.Type]' (aka 'Array<(BinaryDecodable & BinaryEncodable).Type>') in coercion
for this line
let utf8: [UInt8] = try (Array as [BinaryCodable.Type]).init(fromBinary: decoder) as! [UInt8]
Any suggestions?
Could you teach me? let Range type supports Codable or BinaryCodable.
Thanks
Have you had time to look at Swift 4 migration issues?
BinaryEncoder.swift:70:23: Protocol 'FixedWidthInteger' can only be used as a generic constraint because it has Self or associated type requirements
BinaryDecoder.swift:109:26: Protocol 'FixedWidthInteger' can only be used as a generic constraint because it has Self or associated type requirements
Does it support encode to Data and decode from Data?
Does it supoort NSRect, NSSize, NSPoint?
BinaryEncoder.encode() -> Data
BinaryDecoder.decode(BinaryCodable, data: Data)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.