Skip to content

Commit

Permalink
Updates for Swift 4.1.
Browse files Browse the repository at this point in the history
Add direct conditionals into the code to move to the newer apis and
avoid warnings.
  • Loading branch information
thomasvl committed Jan 25, 2018
1 parent 17b6b0b commit 442a3ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Sources/SwiftProtobuf/BinaryDelimited.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ internal func decodeVarint(_ stream: InputStream) throws -> UInt64 {

// Buffer to reuse within nextByte.
var readBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
defer { readBuffer.deallocate(capacity: 1) }
#if swift(>=4.1)
defer { readBuffer.deallocate() }
#else
defer { readBuffer.deallocate(capacity: 1) }
#endif

func nextByte() throws -> UInt8 {
let bytesRead = stream.read(readBuffer, maxLength: 1)
Expand Down
22 changes: 19 additions & 3 deletions Sources/SwiftProtobuf/DoubleFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,24 @@ private func wrapped_vsnprintf(destination: UnsafeMutableRawBufferPointer,
/// Support parsing and formatting float/double values to/from UTF-8
internal class DoubleFormatter {
private var doubleFormatString: UnsafeMutableRawBufferPointer
private var work = UnsafeMutableRawBufferPointer.allocate(count: 128)
#if swift(>=4.1)
private var work =
UnsafeMutableRawBufferPointer.allocate(byteCount: 128,
alignment: MemoryLayout<UInt8>.alignment)
#else
private var work = UnsafeMutableRawBufferPointer.allocate(count: 128)
#endif

init() {
let format: StaticString = "%.*g"
let formatBytes = UnsafeBufferPointer(start: format.utf8Start, count: format.utf8CodeUnitCount)
doubleFormatString = UnsafeMutableRawBufferPointer.allocate(count: formatBytes.count + 1)
#if swift(>=4.1)
doubleFormatString =
UnsafeMutableRawBufferPointer.allocate(byteCount: formatBytes.count + 1,
alignment: MemoryLayout<UInt8>.alignment)
#else
doubleFormatString = UnsafeMutableRawBufferPointer.allocate(count: formatBytes.count + 1)
#endif
doubleFormatString.copyBytes(from: formatBytes)
doubleFormatString[formatBytes.count] = 0
}
Expand All @@ -64,7 +76,11 @@ internal class DoubleFormatter {
}
// Copy it to the work buffer and null-terminate it
let source = UnsafeRawBufferPointer(start: bytes, count: count)
work.copyBytes(from:source)
#if swift(>=4.1)
work.copyMemory(from:source)
#else
work.copyBytes(from:source)
#endif
work[count] = 0

// Use C library strtod() to parse it
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftProtobuf/NameMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ fileprivate class InternPool {

deinit {
for buff in interned {
let p = UnsafeMutableRawPointer(mutating: buff.baseAddress)!
p.deallocate(bytes: buff.count, alignedTo: 1)
#if swift(>=4.1)
buff.deallocate()
#else
let p = UnsafeMutableRawPointer(mutating: buff.baseAddress)!
p.deallocate(bytes: buff.count, alignedTo: 1)
#endif
}
}
}
Expand Down

0 comments on commit 442a3ae

Please sign in to comment.