diff --git a/submodules/Postbox/Sources/Message.swift b/submodules/Postbox/Sources/Message.swift index 76c0d2348a..23d4a2875a 100644 --- a/submodules/Postbox/Sources/Message.swift +++ b/submodules/Postbox/Sources/Message.swift @@ -52,14 +52,12 @@ public struct MessageId: Hashable, Comparable, CustomStringConvertible, PostboxC } public func encodeToBuffer(_ buffer: WriteBuffer) { - var peerIdNamespace = self.peerId.namespace - var peerIdId = self.peerId.id + var peerIdValue = self.peerId.toInt64() var namespace = self.namespace var id = self.id - buffer.write(&peerIdNamespace, offset: 0, length: 4); - buffer.write(&peerIdId, offset: 0, length: 4); - buffer.write(&namespace, offset: 0, length: 4); - buffer.write(&id, offset: 0, length: 4); + buffer.write(&peerIdValue, offset: 0, length: 8) + buffer.write(&namespace, offset: 0, length: 4) + buffer.write(&id, offset: 0, length: 4) } public static func encodeArrayToBuffer(_ array: [MessageId], buffer: WriteBuffer) { diff --git a/submodules/Postbox/Sources/Peer.swift b/submodules/Postbox/Sources/Peer.swift index 70a1862e51..9ade559b28 100644 --- a/submodules/Postbox/Sources/Peer.swift +++ b/submodules/Postbox/Sources/Peer.swift @@ -152,6 +152,7 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable { public func toInt64() -> Int64 { let idLowBits = self.id.rawValue & 0xffffffff + let result: Int64 if self.namespace == .max && self.id.rawValue == 0 { var data: UInt64 = 0 @@ -160,7 +161,7 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable { data |= idLowBits - return Int64(bitPattern: data) + result = Int64(bitPattern: data) } else { var data: UInt64 = 0 data |= UInt64(self.namespace.rawValue) << 32 @@ -170,8 +171,12 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable { data |= idLowBits - return Int64(bitPattern: data) + result = Int64(bitPattern: data) } + + assert(PeerId(result) == self) + + return result } public static func encodeArrayToBuffer(_ array: [PeerId], buffer: WriteBuffer) {