mirror of
https://github.com/HaishinKit/HaishinKit.swift.git
synced 2026-05-07 20:12:28 +00:00
25 lines
756 B
Swift
25 lines
756 B
Swift
import Foundation
|
|
|
|
package extension ExpressibleByIntegerLiteral {
|
|
var data: Data {
|
|
return withUnsafePointer(to: self) { value in
|
|
return Data(bytes: UnsafeRawPointer(value), count: MemoryLayout<Self>.size)
|
|
}
|
|
}
|
|
|
|
init(data: Data) {
|
|
let diff: Int = MemoryLayout<Self>.size - data.count
|
|
if 0 < diff {
|
|
var buffer = Data(repeating: 0, count: diff)
|
|
buffer.append(data)
|
|
self = buffer.withUnsafeBytes { $0.baseAddress!.assumingMemoryBound(to: Self.self).pointee }
|
|
return
|
|
}
|
|
self = data.withUnsafeBytes { $0.baseAddress!.assumingMemoryBound(to: Self.self).pointee }
|
|
}
|
|
|
|
init(data: Slice<Data>) {
|
|
self.init(data: Data(data))
|
|
}
|
|
}
|