From f859dc8b2638f908c9449237c730f09ba4addfc3 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 26 Mar 2018 11:32:52 -0400 Subject: [PATCH] Expose VarInt's encoded length --- src/network/encodable.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/network/encodable.rs b/src/network/encodable.rs index 7127cd9..71fb201 100644 --- a/src/network/encodable.rs +++ b/src/network/encodable.rs @@ -83,6 +83,21 @@ impl_int_encodable!(i16, read_i16, emit_i16); impl_int_encodable!(i32, read_i32, emit_i32); impl_int_encodable!(i64, read_i64, emit_i64); +impl VarInt { + /// Gets the length of this VarInt when encoded. + /// Returns 1 for 0...0xFC, 3 for 0xFD...(2^16-1), 5 for 0x10000...(2^32-1), + /// and 9 otherwise. + #[inline] + pub fn encoded_length(&self) -> u64 { + match self.0 { + 0...0xFC => { 1 } + 0xFD...0xFFFF => { 3 } + 0x10000...0xFFFFFFFF => { 5 } + _ => { 9 } + } + } +} + impl ConsensusEncodable for VarInt { #[inline] fn consensus_encode(&self, s: &mut S) -> Result<(), S::Error> {