Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize Deserialize to always borrow Cow<str> #44

Merged
merged 3 commits into from
Jul 22, 2021

Conversation

maciejhirsz
Copy link
Owner

@maciejhirsz maciejhirsz commented Jul 22, 2021

Since beef::Cow is only compatible with str and [T], we can specialize the serde::Deserialize for Cow<str> so that it always borrows when possible. This has the advantage over std::borrow::Cow when deserializing into Cow<str> directly:

let json = r#""foo""#;
let std_cow: std::borrow::Cow<str> = serde_json::from_str(json).unwrap(); // will always deserialize to `Cow::Owned` with an allocation
let beef_cow: beef::Cow<str> = serde_json::from_str(json).unwrap(); // will be borrowed since `foo` can be borrowed from the `json` slice

@maciejhirsz maciejhirsz merged commit 86c93bb into master Jul 22, 2021
@maciejhirsz maciejhirsz deleted the serde-str-always-borrow branch July 22, 2021 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant