Skip to content

Commit

Permalink
#154 Improve Embedded Swift support
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 8, 2024
1 parent d3adea7 commit 98fec71
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
7 changes: 1 addition & 6 deletions Sources/Bluetooth/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal extension String {
#if canImport(Foundation)
self.init(bytes: data, encoding: .utf8)
#else
#error("Darwin should have Foundation framework")
fatalError()
#endif
}
}
Expand All @@ -45,8 +45,3 @@ internal extension String {
}
#endif
}

#if hasFeature(Embedded)
@_silgen_name("snprintf")
internal func _snprintf_uint8_t(_ pointer: UnsafeMutablePointer<CChar>, _ length: Int, _ format: UnsafePointer<CChar>, _ arg: UInt8) -> Int32
#endif
11 changes: 9 additions & 2 deletions Sources/Bluetooth/Extensions/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@ import Musl
import WASILibc
#elseif canImport(Bionic)
import Bionic
#else
#error("Unsupported Platform")
#endif

// Declares the required C functions
#if hasFeature(Embedded)
@_silgen_name("memcmp")
internal func _memcmp(
_ p1: UnsafeRawPointer?,
_ p2: UnsafeRawPointer?,
_ size: Int
) -> Int32
#else
internal func _memcmp(
_ p1: UnsafeRawPointer?,
_ p2: UnsafeRawPointer?,
_ size: Int
) -> Int32 {
memcmp(p1, p2, size)
}
#endif

#if hasFeature(Embedded)
@_silgen_name("snprintf")
Expand Down
12 changes: 6 additions & 6 deletions Sources/Bluetooth/Extensions/UUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ extension _UUID {
@inline(__always)
internal func withUUIDBytes<R>(_ work: (UnsafeBufferPointer<UInt8>) throws -> R) rethrows -> R {
return try withExtendedLifetime(self) {
try withUnsafeBytes(of: uuid) { rawBuffer in
try Swift.withUnsafeBytes(of: uuid) { rawBuffer in
return try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in
return try work(buffer)
}
Expand All @@ -143,8 +143,8 @@ extension _UUID {
extension _UUID: Equatable {

public static func ==(lhs: _UUID, rhs: _UUID) -> Bool {
withUnsafeBytes(of: lhs) { lhsPtr in
withUnsafeBytes(of: rhs) { rhsPtr in
Swift.withUnsafeBytes(of: lhs) { lhsPtr in
Swift.withUnsafeBytes(of: rhs) { rhsPtr in
let lhsTuple = lhsPtr.loadUnaligned(as: (UInt64, UInt64).self)
let rhsTuple = rhsPtr.loadUnaligned(as: (UInt64, UInt64).self)
return (lhsTuple.0 ^ rhsTuple.0) | (lhsTuple.1 ^ rhsTuple.1) == 0
Expand All @@ -156,7 +156,7 @@ extension _UUID: Equatable {
extension _UUID: Hashable {

public func hash(into hasher: inout Hasher) {
withUnsafeBytes(of: uuid) { buffer in
Swift.withUnsafeBytes(of: uuid) { buffer in
hasher.combine(bytes: buffer)
}
}
Expand Down Expand Up @@ -211,8 +211,8 @@ extension _UUID : Comparable {
var rightUUID = rhs.uuid
var result: Int = 0
var diff: Int = 0
withUnsafeBytes(of: &leftUUID) { leftPtr in
withUnsafeBytes(of: &rightUUID) { rightPtr in
Swift.withUnsafeBytes(of: &leftUUID) { leftPtr in
Swift.withUnsafeBytes(of: &rightUUID) { rightPtr in
for offset in (0 ..< MemoryLayout<ByteValue>.size).reversed() {
diff = Int(leftPtr.load(fromByteOffset: offset, as: UInt8.self)) -
Int(rightPtr.load(fromByteOffset: offset, as: UInt8.self))
Expand Down

0 comments on commit 98fec71

Please sign in to comment.