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