Code Monkey home page Code Monkey logo

binarycoder's People

Contributors

mikeash avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

binarycoder's Issues

Improper decoding of binary arrays.

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)

Compile errors with swift 4.1

@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:

Click to expand `BinaryCodableExtensions.swift`
/// 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?

FixedWidthInteger in Swift 4

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.