forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.swift
45 lines (39 loc) · 1.1 KB
/
Utilities.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Bits
import Foundation
extension Data {
public var hexDebug: String {
return "0x" + map { String(format: "%02X", $0) }.joined(separator: " ")
}
}
extension UnsafeBufferPointer {
public var unsafeBaseAddress: UnsafePointer<Element> {
guard let baseAddress = self.baseAddress else {
fatalError("Unexpected nil baseAddress for \(self)")
}
return baseAddress
}
}
extension UnsafeRawBufferPointer {
public var unsafeBaseAddress: UnsafeRawPointer {
guard let baseAddress = self.baseAddress else {
fatalError("Unexpected nil baseAddress for \(self)")
}
return baseAddress
}
}
extension Data {
public mutating func unsafePopFirst() -> Byte {
guard let byte = popFirst() else {
fatalError("Unexpected end of data")
}
return byte
}
}
extension Data {
/// Casts data to a supplied type.
internal func unsafeCast<T>(to type: T.Type = T.self) -> T {
return withUnsafeBytes { (pointer: UnsafePointer<T>) -> T in
return pointer.pointee
}
}
}