Expand description
A small library with a Cow
wrappers that implements serde::Deserialize
in the ways that should be expected.
By default, Cow
is always deserialized to the Cow::Owned
variant due to a lack of specialisation, but this library
provides CowStr
and CowBytes
which deserialize to Cow::Borrowed
if possible, borrowing from the original data.
§Example
use std::borrow::Cow;
use serde_cow::CowStr;
let source = r#""Hello World""#;
let normal: Cow<str> = serde_json::from_str(source).unwrap();
assert!(matches!(normal, Cow::Owned(_))); // Wasteful!
let efficent: CowStr = serde_json::from_str(source).unwrap();
assert!(matches!(efficent.0, Cow::Borrowed(_))); // Zero copy!
§Minimum Supported Rust Version
This is currently 1.56 and is considered a breaking change to increase.
Structs§
- CowBytes
- A wrapper around
Cow<[u8]>
to implementserde::Deserialize
in the expected way. - CowStr
- A wrapper around
Cow<str>
to implementserde::Deserialize
in the expected way.