Skip to main content

from_slice_into_borrowed

Function from_slice_into_borrowed 

Source
pub fn from_slice_into_borrowed<'input, 'facet>(
    input: &'input [u8],
    partial: Partial<'facet, true>,
) -> Result<Partial<'facet, true>, DeserializeError>
where 'input: 'facet,
Expand description

Deserialize MsgPack bytes into an existing Partial, allowing zero-copy borrowing.

This variant requires the input to outlive the Partial’s lifetime ('input: 'facet), enabling zero-copy deserialization of byte slices as &[u8] or Cow<[u8]>.

This is useful for reflection-based deserialization where you don’t have a concrete type T at compile time, only its Shape metadata.

§Example

use facet::Facet;
use facet_msgpack::from_slice_into_borrowed;
use facet_reflect::Partial;

#[derive(Facet, Debug, PartialEq)]
struct Message<'a> {
    id: u32,
    data: &'a [u8],
}

// MsgPack encoding of {"id": 1, "data": <bin8 with 3 bytes>}
let bytes = &[0x82, 0xa2, b'i', b'd', 0x01, 0xa4, b'd', b'a', b't', b'a', 0xc4, 0x03, 0xAB, 0xCD, 0xEF];
let partial = Partial::alloc::<Message>().unwrap();
let partial = from_slice_into_borrowed(bytes, partial).unwrap();
let value = partial.build().unwrap();
let msg: Message = value.materialize().unwrap();
assert_eq!(msg.id, 1);
assert_eq!(msg.data, &[0xAB, 0xCD, 0xEF]);