Function abi_stable::std_types::cow::deserialize_borrowed_str
source · pub fn deserialize_borrowed_str<'de, 'a, D>(
deserializer: D
) -> Result<RCowStr<'a>, D::Error>where
D: Deserializer<'de>,
'de: 'a,
Expand description
Deserializes an RCowStr<'a>
that borrows the string from the deserializer
whenever possible.
Example
Defining a type containing an RCowStr<'a>
which borrows from the deserializer.
use abi_stable::std_types::cow::{deserialize_borrowed_str, RCow, RCowStr};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct TheSlice<'a> {
#[serde(borrow, deserialize_with = "deserialize_borrowed_str")]
slice: RCowStr<'a>,
}
let the_slice = TheSlice {
slice: RCow::from("That's a lot of fish."),
};
let string = serde_json::to_string(&the_slice).unwrap();
let deserialized_slice = serde_json::from_str::<TheSlice<'_>>(&string).unwrap();
assert_eq!(the_slice, deserialized_slice);
assert!(deserialized_slice.slice.is_borrowed());