Skip to main content

from_slice_borrowed

Function from_slice_borrowed 

Source
pub fn from_slice_borrowed<'input, 'facet, T>(
    input: &'input [u8],
) -> Result<T, DeserializeError<PostcardError>>
where T: Facet<'facet>, 'input: 'facet,
Expand description

Deserialize a value from postcard bytes, allowing zero-copy borrowing.

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

Use this when you need maximum performance and can guarantee the input buffer outlives the deserialized value. For most use cases, prefer from_slice which doesn’t have lifetime requirements.

§Example

use facet::Facet;
use facet_postcard::from_slice_borrowed;

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

// Postcard encoding: [id=1, data_len=3, 0xAB, 0xCD, 0xEF]
let bytes = &[0x01, 0x03, 0xAB, 0xCD, 0xEF];
let msg: Message = from_slice_borrowed(bytes).unwrap();
assert_eq!(msg.id, 1);
assert_eq!(msg.data, &[0xAB, 0xCD, 0xEF]);