Struct serde_with::BorrowCow
source · pub struct BorrowCow;
Expand description
Borrow Cow
data during deserialization when possible.
The types Cow<'a, [u8]>
, Cow<'a, [u8; N]>
, and Cow<'a, str>
can borrow from the input data during deserialization.
serde supports this, by annotating the fields with #[serde(borrow)]
. but does not support borrowing on nested types.
This gap is filled by this BorrowCow
adapter.
Using this adapter with Cow<'a, [u8]>
/Cow<'a, [u8; N]>
will serialize the value as a sequence of u8
values.
This might not allow to borrow the data during deserialization.
For a different format, which is also more efficient, use the Bytes
adapter, which is also implemented for Cow
.
When combined with the serde_as
attribute, the #[serde(borrow)]
annotation will be added automatically.
If the annotation is wrong or too broad, for example because of multiple lifetime parameters, a manual annotation is required.
§Examples
#[serde_as]
#[derive(Deserialize, Serialize)]
struct Data<'a, 'b, 'c> {
#[serde_as(as = "BorrowCow")]
str: Cow<'a, str>,
#[serde_as(as = "BorrowCow")]
slice: Cow<'b, [u8]>,
#[serde_as(as = "Option<[BorrowCow; 1]>")]
nested: Option<[Cow<'c, str>; 1]>,
}
let data = Data {
str: "foobar".into(),
slice: b"foobar"[..].into(),
nested: Some(["HelloWorld".into()]),
};
// Define our expected JSON form
let j = r#"{
"str": "foobar",
"slice": [
102,
111,
111,
98,
97,
114
],
"nested": [
"HelloWorld"
]
}"#;
// Ensure serialization and deserialization produce the expected results
assert_eq!(j, serde_json::to_string_pretty(&data).unwrap());
assert_eq!(data, serde_json::from_str(j).unwrap());
// Cow borrows from the input data
let deserialized: Data<'_, '_, '_> = serde_json::from_str(j).unwrap();
assert!(matches!(deserialized.str, Cow::Borrowed(_)));
assert!(matches!(deserialized.nested, Some([Cow::Borrowed(_)])));
// JSON does not allow borrowing bytes, so `slice` does not borrow
assert!(matches!(deserialized.slice, Cow::Owned(_)));
Trait Implementations§
source§impl<'de> DeserializeAs<'de, Cow<'de, [u8]>> for BorrowCow
impl<'de> DeserializeAs<'de, Cow<'de, [u8]>> for BorrowCow
source§fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8]>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8]>, D::Error>where
D: Deserializer<'de>,
source§impl<'de, const N: usize> DeserializeAs<'de, Cow<'de, [u8; N]>> for BorrowCow
impl<'de, const N: usize> DeserializeAs<'de, Cow<'de, [u8; N]>> for BorrowCow
source§fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8; N]>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8; N]>, D::Error>where
D: Deserializer<'de>,
source§impl<'de> DeserializeAs<'de, Cow<'de, str>> for BorrowCow
impl<'de> DeserializeAs<'de, Cow<'de, str>> for BorrowCow
source§fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, str>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, str>, D::Error>where
D: Deserializer<'de>,
source§impl<'a, T> JsonSchemaAs<Cow<'a, T>> for BorrowCow
Available on crate feature schemars_0_8
only.
impl<'a, T> JsonSchemaAs<Cow<'a, T>> for BorrowCow
schemars_0_8
only.source§fn schema_name() -> String
fn schema_name() -> String
source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read more