libdd-trace-utils 12.0.0

Trace utilities including span processing, MessagePack encoding/decoding, payload handling, and HTTP transport with retry logic for Datadog APM
Documentation
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

/// Data that can be read from raw bytes.
pub trait FromBytes: Sized {
    type Bytes: ?Sized;

    /// The number of bytes needed to be read to extract a value from `T`. Default to
    /// `mem::size_of::<T>()`.
    const FROM_BYTES_SIZE: usize = std::mem::size_of::<Self>();

    fn from_bytes(bytes: &[u8]) -> Self;
}

macro_rules! impl_from_bytes {
    ($ty:ty, $len:expr) => {
        impl FromBytes for $ty {
            type Bytes = $ty;

            // Note that this always does a copy into a new variable. This is
            // because the values in the buffer are not aligned. We could save
            // ourselves a copy by ensuring alignment from the managed side.
            fn from_bytes(bytes: &[u8]) -> Self {
                let mut code_buf = [0u8; $len];
                code_buf.copy_from_slice(bytes);
                <$ty>::from_le_bytes(code_buf)
            }
        }
    };
}

impl_from_bytes!(u128, 16);
impl_from_bytes!(u64, 8);
impl_from_bytes!(f64, 8);
impl_from_bytes!(i64, 8);
impl_from_bytes!(i32, 4);
impl_from_bytes!(u32, 4);
impl_from_bytes!(u16, 2);