Function abi_stable::std_types::cow::deserialize_borrowed_bytes[][src]

pub fn deserialize_borrowed_bytes<'de, 'a, D>(
    deserializer: D
) -> Result<RCow<'a, [u8]>, D::Error> where
    D: Deserializer<'de>,
    'de: 'a, 
Expand description

Deserializes an RCow<'a, [u8]> that borrows the slice from the deserializer whenever possible.

Example

Defining a type containing an RCow<'a, [u8]> which borrows from the deserializer.

use abi_stable::std_types::cow::{deserialize_borrowed_bytes, RCow};

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct TheSlice<'a> {
    #[serde(borrow, deserialize_with = "deserialize_borrowed_bytes")]
    slice: RCow<'a, [u8]>,
}

let the_slice = TheSlice {
    slice: RCow::from(vec![0, 1, 2, 3, 4, 5]),
};

let vec = bincode::serialize(&the_slice).unwrap();

let deserialized_slice = bincode::deserialize(&vec).unwrap();

assert_eq!(the_slice, deserialized_slice);

assert!(deserialized_slice.slice.is_borrowed());