1
1
//! Encoding of [`LogMsg`]es as a binary stream, e.g. to store in an `.rrd` file, or send over network.
2
2
3
+ use std:: borrow:: Borrow ;
4
+
3
5
use re_build_info:: CrateVersion ;
4
6
use re_chunk:: { ChunkError , ChunkResult } ;
5
7
use re_log_types:: LogMsg ;
@@ -74,6 +76,33 @@ pub struct Encoder<W: std::io::Write> {
74
76
is_finished : bool ,
75
77
}
76
78
79
+ impl Encoder < Vec < u8 > > {
80
+ pub fn local ( ) -> Result < Self , EncodeError > {
81
+ Self :: new (
82
+ CrateVersion :: LOCAL ,
83
+ EncodingOptions :: PROTOBUF_COMPRESSED ,
84
+ Vec :: new ( ) ,
85
+ )
86
+ }
87
+
88
+ /// All-in-one helper to encode a stream of [`LogMsg`]s into an actual RRD stream.
89
+ ///
90
+ /// This always uses the local version and its default encoding options.
91
+ ///
92
+ /// Returns the encoded data in a newly allocated vector.
93
+ pub fn encode (
94
+ messages : impl IntoIterator < Item = ChunkResult < impl Borrow < LogMsg > > > ,
95
+ ) -> Result < Vec < u8 > , EncodeError > {
96
+ re_tracing:: profile_function!( ) ;
97
+ let mut encoder = Self :: local ( ) ?;
98
+ for message in messages {
99
+ encoder. append ( message?. borrow ( ) ) ?;
100
+ }
101
+ encoder. finish ( ) ?;
102
+ encoder. into_inner ( )
103
+ }
104
+ }
105
+
77
106
impl < W : std:: io:: Write > Encoder < W > {
78
107
pub fn new (
79
108
version : CrateVersion ,
@@ -191,6 +220,26 @@ impl<W: std::io::Write> Encoder<W> {
191
220
}
192
221
}
193
222
223
+ impl < W : std:: io:: Write > Encoder < W > {
224
+ /// All-in-one helper to encode a stream of [`LogMsg`]s into an actual RRD stream.
225
+ ///
226
+ /// Returns the size in bytes of the encoded data.
227
+ pub fn encode_into (
228
+ version : CrateVersion ,
229
+ options : EncodingOptions ,
230
+ messages : impl IntoIterator < Item = ChunkResult < impl Borrow < LogMsg > > > ,
231
+ write : & mut W ,
232
+ ) -> Result < u64 , EncodeError > {
233
+ re_tracing:: profile_function!( ) ;
234
+ let mut encoder = Encoder :: new ( version, options, write) ?;
235
+ let mut size_bytes = 0 ;
236
+ for message in messages {
237
+ size_bytes += encoder. append ( message?. borrow ( ) ) ?;
238
+ }
239
+ Ok ( size_bytes)
240
+ }
241
+ }
242
+
194
243
// TODO(cmc): It seems a bit suspicious to me that we send an EOS marker on drop, but don't flush.
195
244
// But I don't want to change any flushing behavior at the moment, so I'll keep it that way for now.
196
245
impl < W : std:: io:: Write > std:: ops:: Drop for Encoder < W > {
@@ -205,91 +254,3 @@ impl<W: std::io::Write> std::ops::Drop for Encoder<W> {
205
254
}
206
255
}
207
256
}
208
-
209
- /// Returns the size in bytes of the encoded data.
210
- pub fn encode (
211
- version : CrateVersion ,
212
- options : EncodingOptions ,
213
- messages : impl Iterator < Item = ChunkResult < LogMsg > > ,
214
- write : & mut impl std:: io:: Write ,
215
- ) -> Result < u64 , EncodeError > {
216
- re_tracing:: profile_function!( ) ;
217
- let mut encoder = Encoder :: new ( version, options, write) ?;
218
- let mut size_bytes = 0 ;
219
- for message in messages {
220
- size_bytes += encoder. append ( & message?) ?;
221
- }
222
- Ok ( size_bytes)
223
- }
224
-
225
- /// Returns the size in bytes of the encoded data.
226
- pub fn encode_ref < ' a > (
227
- version : CrateVersion ,
228
- options : EncodingOptions ,
229
- messages : impl Iterator < Item = ChunkResult < & ' a LogMsg > > ,
230
- write : & mut impl std:: io:: Write ,
231
- ) -> Result < u64 , EncodeError > {
232
- re_tracing:: profile_function!( ) ;
233
- let mut encoder = Encoder :: new ( version, options, write) ?;
234
- let mut size_bytes = 0 ;
235
- for message in messages {
236
- size_bytes += encoder. append ( message?) ?;
237
- }
238
- Ok ( size_bytes)
239
- }
240
-
241
- pub fn encode_as_bytes (
242
- version : CrateVersion ,
243
- options : EncodingOptions ,
244
- messages : impl Iterator < Item = ChunkResult < LogMsg > > ,
245
- ) -> Result < Vec < u8 > , EncodeError > {
246
- re_tracing:: profile_function!( ) ;
247
- let mut encoder = Encoder :: new ( version, options, vec ! [ ] ) ?;
248
- for message in messages {
249
- encoder. append ( & message?) ?;
250
- }
251
- encoder. finish ( ) ?;
252
- encoder. into_inner ( )
253
- }
254
-
255
- #[ inline]
256
- pub fn local_encoder ( ) -> Result < Encoder < Vec < u8 > > , EncodeError > {
257
- Encoder :: new (
258
- CrateVersion :: LOCAL ,
259
- EncodingOptions :: PROTOBUF_COMPRESSED ,
260
- Vec :: new ( ) ,
261
- )
262
- }
263
-
264
- #[ inline]
265
- pub fn local_raw_encoder ( ) -> Result < Encoder < Vec < u8 > > , EncodeError > {
266
- Encoder :: new (
267
- CrateVersion :: LOCAL ,
268
- EncodingOptions :: PROTOBUF_COMPRESSED ,
269
- Vec :: new ( ) ,
270
- )
271
- }
272
-
273
- #[ inline]
274
- pub fn encode_as_bytes_local (
275
- messages : impl Iterator < Item = ChunkResult < LogMsg > > ,
276
- ) -> Result < Vec < u8 > , EncodeError > {
277
- let mut encoder = local_raw_encoder ( ) ?;
278
- for message in messages {
279
- encoder. append ( & message?) ?;
280
- }
281
- encoder. finish ( ) ?;
282
- encoder. into_inner ( )
283
- }
284
-
285
- #[ inline]
286
- pub fn encode_ref_as_bytes_local < ' a > (
287
- messages : impl Iterator < Item = ChunkResult < & ' a LogMsg > > ,
288
- ) -> Result < Vec < u8 > , EncodeError > {
289
- let mut encoder = local_raw_encoder ( ) ?;
290
- for message in messages {
291
- encoder. append ( message?) ?;
292
- }
293
- encoder. finish ( ) ?;
294
- encoder. into_inner ( )
295
- }
0 commit comments