Skip to content

Commit e4ba6a5

Browse files
committed
use extract method in decoder
1 parent aa77a21 commit e4ba6a5

File tree

2 files changed

+3
-20
lines changed

2 files changed

+3
-20
lines changed

Sources/PostgreSQL/Message+Parse/PostgreSQLMessageDecoder.swift

+1-18
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,7 @@ fileprivate final class _PostgreSQLMessageDecoder: Decoder, SingleValueDecodingC
105105

106106
/// Decodes a fixed width integer.
107107
func decode<B>(fixedWidthInteger type: B.Type) throws -> B where B: FixedWidthInteger {
108-
guard data.count >= MemoryLayout<B>.size else {
109-
fatalError("Unexpected end of data while decoding \(B.self).")
110-
}
111-
112-
113-
let int: B = data.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) -> B in
114-
return pointer.withMemoryRebound(to: B.self, capacity: 1) { (pointer: UnsafePointer<B>) -> B in
115-
return pointer.pointee.bigEndian
116-
}
117-
}
118-
119-
if data.count == MemoryLayout<B>.size {
120-
/// FIXME: safely advance elsewhere as well
121-
data = Data()
122-
} else {
123-
data = data.advanced(by: MemoryLayout<B>.size)
124-
}
125-
return int
108+
return data.extract(B.self).bigEndian
126109
}
127110

128111
/// See SingleValueDecodingContainer.decode

Sources/PostgreSQL/Utilities.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension Data {
4949

5050
/// Casts data to a supplied type.
5151
internal mutating func extract<T>(_ type: T.Type = T.self) -> T {
52-
assert(MemoryLayout<T>.size <= count, "Insufficient data to decode: \(T.self)")
52+
assert(MemoryLayout<T>.size <= count, "Insufficient data to exctract: \(T.self)")
5353
defer { skip(sizeOf: T.self) }
5454
return withUnsafeBytes { (pointer: UnsafePointer<T>) -> T in
5555
return pointer.pointee
@@ -66,5 +66,5 @@ extension Data {
6666
}
6767
}
6868

69-
69+
7070
}

0 commit comments

Comments
 (0)