diff --git a/Sources/BluetoothGATT/GAPUUIDList.swift b/Sources/BluetoothGATT/GATTUUIDList.swift similarity index 51% rename from Sources/BluetoothGATT/GAPUUIDList.swift rename to Sources/BluetoothGATT/GATTUUIDList.swift index 59b0791d9..53eacc997 100644 --- a/Sources/BluetoothGATT/GAPUUIDList.swift +++ b/Sources/BluetoothGATT/GATTUUIDList.swift @@ -6,19 +6,24 @@ // Copyright © 2018 PureSwift. All rights reserved. // +#if canImport(Foundation) import Foundation +#endif import Bluetooth /// GAP UUID List -internal struct GAPUUIDList { +internal struct GATTUUIDList { - internal var uuids: [Element] + var uuids: [Element] - internal init(uuids: [Element]) { + init(uuids: [Element]) { self.uuids = uuids } +} + +extension GATTUUIDList: DataConvertible { - internal init?(data: Data) { + init?(data: Data) { let elementSize = MemoryLayout.size @@ -31,7 +36,7 @@ internal struct GAPUUIDList { guard index + elementSize <= data.count else { return nil } - let valueData = data.subdataNoCopy(in: data.startIndex + index ..< data.startIndex + index + elementSize) + let valueData = data.subdata(in: data.startIndex + index ..< data.startIndex + index + elementSize) let value = Element(littleEndian: Element(data: valueData)!) @@ -42,26 +47,21 @@ internal struct GAPUUIDList { self.uuids = uuids } -} - -extension GAPUUIDList: DataConvertible { - static func += (data: inout T, value: GAPUUIDList) { - - value.forEach { data += $0.littleEndian } + func append(to data: inout Data) { + forEach { data += $0.littleEndian } } var dataLength: Int { - return MemoryLayout.size * uuids.count } } // MARK: - Sequence -extension GAPUUIDList: Sequence { +extension GATTUUIDList: Sequence { - public func makeIterator() -> IndexingIterator> { + public func makeIterator() -> IndexingIterator> { return IndexingIterator(_elements: self) } @@ -69,7 +69,7 @@ extension GAPUUIDList: Sequence { // MARK: - Collection -extension GAPUUIDList: Collection { +extension GATTUUIDList: Collection { subscript(index: Int) -> Element { @@ -94,41 +94,13 @@ extension GAPUUIDList: Collection { // MARK: - Supporting Types -internal protocol GAPUUIDElement: UnsafeDataConvertible { - - init?(data: Data) - +internal protocol GAPUUIDElement: DataConvertible { + init(littleEndian: Self) var littleEndian: Self { get } - - static func += (data: inout LowEnergyAdvertisingData, value: Self) -} - -extension UInt16: GAPUUIDElement { - - init?(data: Data) { - - guard data.count == MemoryLayout.size - else { return nil } - - self.init(bytes: (data[0], - data[1])) - } -} - -extension UInt32: GAPUUIDElement { - - init?(data: Data) { - - guard data.count == MemoryLayout.size - else { return nil } - - self.init(bytes: (data[0], - data[1], - data[2], - data[3])) - } } -extension Bluetooth.UInt128: GAPUUIDElement { } +extension UInt16: GAPUUIDElement { } +extension UInt32: GAPUUIDElement { } +extension UInt128: GAPUUIDElement { }