Skip to content

Commit

Permalink
Merge pull request #116 from PureSwift/feature/swift42
Browse files Browse the repository at this point in the history
Updated for Swift 4.2
  • Loading branch information
colemancda authored Nov 19, 2018
2 parents 157b540 + 8c77ec6 commit 821ead6
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 20 deletions.
15 changes: 15 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// swift-tools-version:4.2
import PackageDescription

let package = Package(name: "Bluetooth",
products: [
.library(
name: "Bluetooth",
targets: ["Bluetooth"]
)
],
targets: [
.target(name: "Bluetooth", path: "./Sources"),
.testTarget(name: "BluetoothTests", dependencies: ["Bluetooth"])
],
swiftLanguageVersions: [.v4, .v4_2])
3 changes: 3 additions & 0 deletions Sources/ATT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ public enum ATTAttributePermission: UInt8, BitMaskOption {
case authorized = 0x40
case noAuthorization = 0x80

#if swift(>=4.2)
#else
public static let allCases: Set<ATTAttributePermission> = [.read,
.write,
.readEncrypt,
Expand All @@ -221,6 +223,7 @@ public enum ATTAttributePermission: UInt8, BitMaskOption {
.writeAuthentication,
.authorized,
.noAuthorization]
#endif
}

public extension ATTAttributePermission {
Expand Down
4 changes: 1 addition & 3 deletions Sources/ATTConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,7 @@ internal enum ATTResponse <Value: ATTProtocolDataUnit> {
}
}

// MARK: - Private Supporting Types

fileprivate final class ATTSendOperation {
private final class ATTSendOperation {

typealias Response = AnyATTResponse

Expand Down
13 changes: 12 additions & 1 deletion Sources/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ import Foundation

internal extension Data {

#if swift(>=4.2)
func subdataNoCopy(in range: Range<Int>) -> Data {

let pointer = withUnsafeBytes { UnsafeMutableRawPointer(mutating: $0).advanced(by: range.lowerBound) }
return Data(bytesNoCopy: pointer, count: range.count, deallocator: .none)
}
#else
func subdataNoCopy(in range: CountableRange<Int>) -> Data {

let pointer = withUnsafeBytes { UnsafeMutableRawPointer(mutating: $0).advanced(by: range.lowerBound) }

return Data(bytesNoCopy: pointer, count: range.count, deallocator: .none)
}

func subdata(in range: CountableRange<Int>) -> Data {
return Data(self[range])
}
#endif

func suffixNoCopy(from index: Int) -> Data {

return subdataNoCopy(in: index ..< count)
Expand Down
2 changes: 1 addition & 1 deletion Sources/GAPDataEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public struct GAPDataDecoder {
guard index <= data.count
else { throw Error.insufficientBytes(expected: index + 1, actual: data.count) }

value = copyBytes ? data.subdata(in: Range(dataRange)) : data.subdataNoCopy(in: dataRange)
value = copyBytes ? data.subdata(in: dataRange) : data.subdataNoCopy(in: dataRange)

} else {

Expand Down
2 changes: 0 additions & 2 deletions Sources/GATTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public final class GATTClient {

public let preferredMaximumTransmissionUnit: ATTMaximumTransmissionUnit

// Don't modify
@_versioned
internal private(set) var connection: ATTConnection

/// Whether the client is currently writing a long value.
Expand Down
2 changes: 0 additions & 2 deletions Sources/GATTDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ public struct GATTDatabase {

// MARK: - Internal Properties

@_versioned
internal private(set) var attributeGroups = [AttributeGroup]()

/// Do not access directly, use `newHandle()`
@_versioned
internal private(set) var lastHandle: UInt16 = 0x0000

// MARK: - Initialization
Expand Down
9 changes: 4 additions & 5 deletions Sources/GATTServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public final class GATTServer {
public var didWrite: ((_ uuid: BluetoothUUID, _ handle: UInt16, _ value: Data) -> Void)?

// Don't modify
@_versioned
internal let connection: ATTConnection

private var preparedWrites = [PreparedWrite]()
Expand Down Expand Up @@ -879,7 +878,7 @@ internal extension GATTDatabase {
/// Used for Service discovery. Should return tuples with the Service start handle, end handle and UUID.
func readByGroupType(handle: (start: UInt16, end: UInt16), type: BluetoothUUID) -> [(start: UInt16, end: UInt16, uuid: BluetoothUUID)] {

let handleRange = handle.end < UInt16.max ? Range(handle.start ... handle.end) : Range(handle.start ..< handle.end)
let handleRange = handle.end < UInt16.max ? Range(handle.start ... handle.end) : handle.start ..< handle.end

var data: [(start: UInt16, end: UInt16, uuid: BluetoothUUID)] = []
data.reserveCapacity(attributeGroups.count)
Expand All @@ -902,21 +901,21 @@ internal extension GATTDatabase {

func readByType(handle: (start: UInt16, end: UInt16), type: BluetoothUUID) -> [Attribute] {

let range = handle.end < UInt16.max ? Range(handle.start ... handle.end) : Range(handle.start ..< handle.end)
let range = handle.end < UInt16.max ? Range(handle.start ... handle.end) : handle.start ..< handle.end

return attributes.filter { range.contains($0.handle) && $0.uuid == type }
}

func findInformation(handle: (start: UInt16, end: UInt16)) -> [Attribute] {

let range = handle.end < UInt16.max ? Range(handle.start ... handle.end) : Range(handle.start ..< handle.end)
let range = handle.end < UInt16.max ? Range(handle.start ... handle.end) : handle.start ..< handle.end

return attributes.filter { range.contains($0.handle) }
}

func findByTypeValue(handle: (start: UInt16, end: UInt16), type: UInt16, value: Data) -> [(UInt16, UInt16)] {

let range = handle.end < UInt16.max ? Range(handle.start ... handle.end) : Range(handle.start ..< handle.end)
let range = handle.end < UInt16.max ? Range(handle.start ... handle.end) : handle.start ..< handle.end

var results = [(UInt16, UInt16)]()

Expand Down
18 changes: 14 additions & 4 deletions Tests/BluetoothTests/BluetoothTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,21 @@ final class BluetoothTests: XCTestCase {
// set conversion
let all = BitMaskOptionSet(ATT.AttributePermission.allCases)

let expected: Set<ATT.AttributePermission> = [.read,
.write,
.readEncrypt,
.writeEncrypt,
.readAuthentication,
.writeAuthentication,
.authorized,
.noAuthorization]

XCTAssertEqual(expected, Set(ATT.AttributePermission.allCases))
XCTAssert(all.contains(ATT.AttributePermission.allCases))
XCTAssert(all.count == ATT.AttributePermission.allCases.count)
XCTAssert(all.count == 8)
XCTAssert(Set(all) == ATT.AttributePermission.allCases)
XCTAssert(all == BitMaskOptionSet<ATT.AttributePermission>.all)
XCTAssertEqual(all.count, ATT.AttributePermission.allCases.count)
XCTAssertEqual(all.count, 8)
XCTAssertEqual(Set(all), Set(ATT.AttributePermission.allCases))
XCTAssertEqual(all, BitMaskOptionSet<ATT.AttributePermission>.all)
XCTAssert(all.contains(ATT.AttributePermission.encrypt))
XCTAssert(all.contains(ATT.AttributePermission.authentication))
XCTAssert(BitMaskOptionSet<ATT.AttributePermission>().contains(.read) == false)
Expand Down
4 changes: 2 additions & 2 deletions Xcode/Bluetooth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3666,7 +3666,7 @@
PRODUCT_NAME = $PROJECT_NAME;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -3726,7 +3726,7 @@
PRODUCT_NAME = $PROJECT_NAME;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down

0 comments on commit 821ead6

Please sign in to comment.