from_slice

Function from_slice 

Source
pub fn from_slice(
    data: &[u8],
) -> Result<(Vec<Vec<SpanSlice<'_>>>, usize), DecodeError>
Expand description

Decodes a slice of bytes into a Vec<Vec<SpanSlice>> object. The resulting spans have the same lifetime as the initial buffer.

§Arguments

  • data - A slice of bytes containing the encoded data. Bytes are expected to be encoded msgpack data containing a list of a list of v04 spans.

§Returns

  • Ok(Vec<TracerPayloadV04>, usize) - A vector of decoded Vec<SpanSlice> objects if successful. and the number of bytes in the slice used by the decoder.
  • Err(DecodeError) - An error if the decoding process fails.

§Errors

This function will return an error if:

  • The array length for trace count or span count cannot be read.
  • Any span cannot be decoded.

§Examples

use libdd_tinybytes;
use libdd_trace_protobuf::pb::Span;
use libdd_trace_utils::msgpack_decoder::v04::from_slice;
use rmp_serde::to_vec_named;

let span = Span {
    name: "test-span".to_owned(),
    ..Default::default()
};
let encoded_data = to_vec_named(&vec![vec![span]]).unwrap();
let encoded_data_as_tinybytes = libdd_tinybytes::Bytes::from(encoded_data);
let (decoded_traces, _payload_size) =
    from_slice(&encoded_data_as_tinybytes).expect("Decoding failed");

assert_eq!(1, decoded_traces.len());
assert_eq!(1, decoded_traces[0].len());
let decoded_span = &decoded_traces[0][0];
assert_eq!("test-span", decoded_span.name);