@@ -13,6 +13,7 @@ mod mapping;
13
13
mod sample;
14
14
mod string;
15
15
mod value_type;
16
+ mod varint;
16
17
17
18
#[ cfg( feature = "prost_impls" ) ]
18
19
pub mod prost_impls;
@@ -24,6 +25,7 @@ pub use mapping::*;
24
25
pub use sample:: * ;
25
26
pub use string:: * ;
26
27
pub use value_type:: * ;
28
+ pub use varint:: * ;
27
29
28
30
use std:: io:: { self , Write } ;
29
31
@@ -52,12 +54,6 @@ pub trait Value {
52
54
}
53
55
}
54
56
55
- /// You can use varint to store any of the listed data types:
56
- /// int32 | int64 | uint32 | uint64 | bool | enum | sint32 | sint64
57
- #[ repr( transparent) ]
58
- #[ derive( Copy , Clone ) ]
59
- pub struct Varint ( pub u64 ) ;
60
-
61
57
/// A tag and value pair.
62
58
///
63
59
/// The wire type isn't stored; it's provided by the Value implementation,
@@ -122,9 +118,6 @@ const MIN_FIELD: u32 = 1;
122
118
/// The largest possible protobuf field number.
123
119
const MAX_FIELD : u32 = ( 1 << 29 ) - 1 ;
124
120
125
- /// An encoded 64-bit unsigned number takes between 1 and 10 bytes, inclusive.
126
- pub const MAX_VARINT_LEN : u64 = 10 ;
127
-
128
121
/// Represents the wire type for in-wire protobuf. There are more types than
129
122
/// are represented here; these are just the supported ones.
130
123
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
@@ -133,72 +126,6 @@ pub enum WireType {
133
126
Varint = 0 ,
134
127
LengthDelimited = 2 ,
135
128
}
136
- impl Varint {
137
- /// Returns the number of bytes it takes to encode a varint. This is
138
- /// between 1 and 10 bytes, inclusive.
139
- pub const fn proto_len ( & self ) -> u64 {
140
- // https://github.com/google/protobuf/blob/3.3.x/src/google/protobuf/io/coded_stream.h#L1301-L1309
141
- ( ( ( ( self . 0 | 1 ) . leading_zeros ( ) ^ 63 ) * 9 + 73 ) / 64 ) as u64
142
- }
143
- }
144
-
145
- impl Value for Varint {
146
- const WIRE_TYPE : WireType = WireType :: Varint ;
147
-
148
- fn proto_len ( & self ) -> u64 {
149
- self . proto_len ( )
150
- }
151
-
152
- /// Encodes a varint according to protobuf semantics.
153
- ///
154
- /// Note that it will write between 1 and 10 bytes, inclusive. You should
155
- /// probably write to a buffered Write object--if you write directly to
156
- /// something like a TCP stream, it's going to send one byte at a time,
157
- /// which is excessively inefficient. In libdatadog, we typically write to
158
- /// some sort of compressor which has its own input buffer.
159
- ///
160
- /// See https://protobuf.dev/programming-guides/encoding/#varints
161
- #[ inline]
162
- fn encode < W : Write > ( & self , writer : & mut W ) -> io:: Result < ( ) > {
163
- let mut value = self . 0 ;
164
- loop {
165
- let byte = if value < 0x80 {
166
- value as u8
167
- } else {
168
- ( ( value & 0x7F ) | 0x80 ) as u8
169
- } ;
170
- writer. write_all ( & [ byte] ) ?;
171
- if value < 0x80 {
172
- return Ok ( ( ) ) ;
173
- }
174
- value >>= 7 ;
175
- }
176
- }
177
- }
178
-
179
- impl From < u64 > for Varint {
180
- fn from ( value : u64 ) -> Self {
181
- Varint ( value)
182
- }
183
- }
184
-
185
- impl From < & u64 > for Varint {
186
- fn from ( value : & u64 ) -> Self {
187
- Varint ( * value)
188
- }
189
- }
190
-
191
- impl From < i64 > for Varint {
192
- fn from ( value : i64 ) -> Self {
193
- Varint ( value as u64 )
194
- }
195
- }
196
-
197
- impl From < & i64 > for Varint {
198
- fn from ( value : & i64 ) -> Self {
199
- Varint ( * value as u64 )
200
- }
201
- }
202
129
203
130
impl Tag {
204
131
#[ cfg_attr( debug_assertions, track_caller) ]
@@ -209,7 +136,7 @@ impl Tag {
209
136
}
210
137
211
138
#[ inline]
212
- pub const fn proto_len ( self ) -> u64 {
139
+ pub fn proto_len ( self ) -> u64 {
213
140
self . into_varint ( ) . proto_len ( )
214
141
}
215
142
@@ -257,14 +184,3 @@ where
257
184
Ok ( ( ) )
258
185
}
259
186
}
260
-
261
- #[ cfg( test) ]
262
- mod tests {
263
- use super :: * ;
264
-
265
- #[ test]
266
- fn max_varint_len ( ) {
267
- assert_eq ! ( MAX_VARINT_LEN , 10 ) ;
268
- assert_eq ! ( MAX_VARINT_LEN , Varint ( u64 :: MAX ) . proto_len( ) ) ;
269
- }
270
- }
0 commit comments