|
| 1 | +// |
| 2 | +// DataConvertible.swift |
| 3 | +// TLVCoding |
| 4 | +// |
| 5 | +// Created by Alsey Coleman Miller on 5/12/19. |
| 6 | +// Copyright © 2019 PureSwift. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +/// Can be converted into data. |
| 12 | +internal protocol DataConvertible { |
| 13 | + |
| 14 | + /// Append data representation into buffer. |
| 15 | + static func += <T: DataContainer> (data: inout T, value: Self) |
| 16 | + |
| 17 | + /// Length of value when encoded into data. |
| 18 | + var dataLength: Int { get } |
| 19 | +} |
| 20 | + |
| 21 | +extension Data { |
| 22 | + |
| 23 | + /// Initialize data with contents of value. |
| 24 | + @inline(__always) |
| 25 | + init <T: DataConvertible> (_ value: T) { |
| 26 | + self.init(capacity: value.dataLength) |
| 27 | + self += value |
| 28 | + } |
| 29 | + |
| 30 | + init <T: Sequence> (_ sequence: T) where T.Element: DataConvertible { |
| 31 | + |
| 32 | + let dataLength = sequence.reduce(0, { $0 + $1.dataLength }) |
| 33 | + self.init(capacity: dataLength) |
| 34 | + sequence.forEach { self += $0 } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// MARK: - UnsafeDataConvertible |
| 39 | + |
| 40 | +/// Internal Data casting protocol |
| 41 | +internal protocol UnsafeDataConvertible: DataConvertible { } |
| 42 | + |
| 43 | +extension UnsafeDataConvertible { |
| 44 | + |
| 45 | + var dataLength: Int { |
| 46 | + return MemoryLayout<Self>.size |
| 47 | + } |
| 48 | + |
| 49 | + /// Append data representation into buffer. |
| 50 | + static func += <T: DataContainer> (data: inout T, value: Self) { |
| 51 | + #if swift(>=4.2) |
| 52 | + withUnsafePointer(to: value) { |
| 53 | + $0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<Self>.size) { |
| 54 | + data.append($0, count: MemoryLayout<Self>.size) |
| 55 | + } |
| 56 | + } |
| 57 | + #else |
| 58 | + var value = value |
| 59 | + withUnsafePointer(to: &value) { |
| 60 | + $0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<Self>.size) { |
| 61 | + data.append($0, count: MemoryLayout<Self>.size) |
| 62 | + } |
| 63 | + } |
| 64 | + #endif |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +extension UInt16: UnsafeDataConvertible { } |
| 69 | +extension UInt32: UnsafeDataConvertible { } |
| 70 | +extension UInt64: UnsafeDataConvertible { } |
| 71 | + |
| 72 | +// MARK: - DataContainer |
| 73 | + |
| 74 | +/// Data container type. |
| 75 | +internal protocol DataContainer: RandomAccessCollection where Self.Index == Int { |
| 76 | + |
| 77 | + subscript(index: Int) -> UInt8 { get } |
| 78 | + |
| 79 | + subscript(range: Range<Int>) -> Slice<Self> { get } |
| 80 | + |
| 81 | + mutating func append(_ newElement: UInt8) |
| 82 | + |
| 83 | + mutating func append(_ pointer: UnsafePointer<UInt8>, count: Int) |
| 84 | + |
| 85 | + mutating func append <C: Collection> (contentsOf bytes: C) where C.Element == UInt8 |
| 86 | + |
| 87 | + #if swift(>=4.2) |
| 88 | + static func += (lhs: inout Self, rhs: UInt8) |
| 89 | + static func += <C: Collection> (lhs: inout Self, rhs: C) where C.Element == UInt8 |
| 90 | + #endif |
| 91 | +} |
| 92 | + |
| 93 | +extension DataContainer { |
| 94 | + |
| 95 | + #if swift(>=4.2) |
| 96 | + #else |
| 97 | + static func += (lhs: inout Self, rhs: UInt8) { |
| 98 | + lhs.append(rhs) |
| 99 | + } |
| 100 | + |
| 101 | + static func += <C: Collection> (lhs: inout Self, rhs: C) where C.Element == UInt8 { |
| 102 | + lhs.append(contentsOf: rhs) |
| 103 | + } |
| 104 | + #endif |
| 105 | + |
| 106 | + mutating func append <T: DataConvertible> (_ value: T) { |
| 107 | + self += value |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +extension Data: DataContainer { |
| 112 | + |
| 113 | + #if swift(>=4.2) |
| 114 | + static func += (lhs: inout Data, rhs: UInt8) { |
| 115 | + lhs.append(rhs) |
| 116 | + } |
| 117 | + #endif |
| 118 | +} |
0 commit comments