Streamable.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Streamable.swift
  2. // This file is part of MiKee.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // MiKee is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // MiKee is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with MiKee. If not, see <https://www.gnu.org/licenses/>.
  18. import Foundation
  19. /// A type that can write itself to a binary output.
  20. public protocol Writable {
  21. /// Writes this value into the given output.
  22. ///
  23. /// This function throws an error if any values are invalid for the given format.
  24. ///
  25. /// - Parameter output: The output stream to write data to.
  26. func write(to output: Output) throws
  27. }
  28. /// A type that can read itself from an binary input.
  29. public protocol Readable {
  30. /// Creates a new instance by reading from the input stream.
  31. ///
  32. /// This initializer throws an error if reading from the input fails, or
  33. /// if the data read is corrupted or otherwise invalid.
  34. ///
  35. /// - Parameter input: The input stream to read data from.
  36. init(from input: Input) throws
  37. }
  38. /// A type that can convert itself into and out of a binary stream.
  39. ///
  40. /// `Binary` is a type alias for the `Writable` and `Readable` protocols.
  41. /// When you use `Binary` as a type or a generic constraint, it matches
  42. /// any type that conforms to both protocols.
  43. public typealias Streamable = Readable & Writable
  44. extension Readable where Self: BytesRepresentable {
  45. public init(from input: Input) throws {
  46. let bytes = try input.read(lenght: MemoryLayout<Self>.size)
  47. self = try Self(bytes)
  48. }
  49. }
  50. extension Writable where Self: BytesRepresentable {
  51. public func write(to output: Output) throws {
  52. try output.write(bytes)
  53. }
  54. }
  55. // MARK: - Streamable Boolean
  56. extension Bool: Streamable {}
  57. // MARK: - Streamable Integer
  58. extension Int: Streamable {}
  59. extension Int8: Streamable {}
  60. extension Int16: Streamable {}
  61. extension Int32: Streamable {}
  62. extension Int64: Streamable {}
  63. extension UInt: Streamable {}
  64. extension UInt8: Streamable {}
  65. extension UInt16: Streamable {}
  66. extension UInt32: Streamable {}
  67. extension UInt64: Streamable {}
  68. // MARK: - Streamable Floating Point
  69. extension Double: Streamable {}
  70. extension Float: Streamable {}
  71. // MARK: - Streamable RawRepresentable
  72. extension Readable where Self: RawRepresentable, RawValue: Readable {
  73. public init(from input: Input) throws {
  74. let rawValue = try RawValue(from: input)
  75. guard let value = Self(rawValue: rawValue) else { throw BinaryError.invalidValue }
  76. self = value
  77. }
  78. }
  79. extension Writable where Self: RawRepresentable, RawValue: Writable {
  80. public func write(to output: Output) throws {
  81. try output.write(rawValue)
  82. }
  83. }
  84. // MARK: - Readable OptionSet
  85. extension Readable where Self: OptionSet, RawValue: Readable {
  86. public init(from input: Input) throws {
  87. let rawValue = try RawValue(from: input)
  88. self = Self(rawValue: rawValue)
  89. }
  90. }
  91. // MARK: - Writable Array
  92. extension Array: Writable where Element: Writable {
  93. public func write(to output: Output) throws {
  94. try forEach(output.write)
  95. }
  96. }
  97. // MARK: - Streamable String
  98. extension String: Streamable {
  99. public init?(bytes: Bytes, encoding: String.Encoding = .utf8) {
  100. let data = Data(bytes.rawValue)
  101. self.init(data: data, encoding: encoding)
  102. }
  103. public func bytes(using encoding: String.Encoding = .utf8) -> Bytes? {
  104. data(using: encoding)?.bytes
  105. }
  106. }
  107. // MARK: - Streamable Data
  108. extension Data: Streamable {
  109. public init(from input: Input) throws {
  110. let bytes = try input.read() as Bytes
  111. self = Data(bytes.rawValue)
  112. }
  113. public func write(to output: Output) throws {
  114. let bytes = Bytes(data: self)
  115. try output.write(bytes)
  116. }
  117. }
  118. // MARK: - Streamable UUID
  119. extension UUID: Streamable {}
  120. // MARK: - Streamable Bytes
  121. extension Bytes: Streamable {
  122. public init(from input: Input) throws {
  123. self = try input.read(lenght: input.remaining.count)
  124. }
  125. public func write(to output: Output) throws {
  126. try output.write(self)
  127. }
  128. }