protobuf/varint/
generic.rs

1use crate::rt::compute_raw_varint64_size;
2
3/// Helper trait implemented by integer types which could be encoded as varint.
4pub(crate) trait ProtobufVarint {
5    /// Size of self when encoded as varint.
6    fn len_varint(&self) -> u64;
7}
8
9impl ProtobufVarint for u64 {
10    fn len_varint(&self) -> u64 {
11        compute_raw_varint64_size(*self)
12    }
13}
14
15impl ProtobufVarint for u32 {
16    fn len_varint(&self) -> u64 {
17        (*self as u64).len_varint()
18    }
19}
20
21impl ProtobufVarint for i64 {
22    fn len_varint(&self) -> u64 {
23        // same as length of u64
24        (*self as u64).len_varint()
25    }
26}
27
28impl ProtobufVarint for i32 {
29    fn len_varint(&self) -> u64 {
30        // sign-extend and then compute
31        (*self as i64).len_varint()
32    }
33}
34
35impl ProtobufVarint for bool {
36    fn len_varint(&self) -> u64 {
37        1
38    }
39}