Skip to main content

libdd_trace_utils/msgpack_encoder/
mod.rs

1// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4pub mod v04;
5pub mod v1;
6
7use rmp::encode::ValueWriteError;
8use std::convert::Infallible;
9
10/// Flatten `ValueWriteError<Infallible>` (uninhabited because both variants wrap
11/// `Infallible`) into the bare `Infallible` so callers can use
12/// [`libdd_common::ResultInfallibleExt`].
13#[inline(always)]
14pub(crate) fn flatten_value_write_infallible(err: ValueWriteError<Infallible>) -> Infallible {
15    match err {
16        ValueWriteError::InvalidMarkerWrite(never) | ValueWriteError::InvalidDataWrite(never) => {
17            never
18        }
19    }
20}
21
22/// A writer that counts bytes without storing them, used to compute encoded payload size.
23pub(crate) struct CountLength(u32);
24
25impl std::io::Write for CountLength {
26    #[inline]
27    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
28        self.write_all(buf)?;
29        Ok(buf.len())
30    }
31
32    #[inline]
33    fn flush(&mut self) -> std::io::Result<()> {
34        Ok(())
35    }
36
37    #[inline]
38    fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
39        self.0 += buf.len() as u32;
40        Ok(())
41    }
42}