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

Allow #[serde(borrow)] for non-std Cow #1754

Merged
merged 1 commit into from Mar 18, 2020
Merged

Allow #[serde(borrow)] for non-std Cow #1754

merged 1 commit into from Mar 18, 2020

Conversation

maciejhirsz
Copy link
Contributor

@maciejhirsz maciejhirsz commented Mar 18, 2020

I've written a custom Cow implementation that's meant to be (mostly) a drop-in replacement for std::borrow::Cow.

The #[derive(Deserialize)] macro currently does specialization for Cow by ident name when using #[serde(borrow)], which is smart, but it also means we get the false positives, as it is in this case:

use beef::Cow;

#[derive(Deserialize)]
struct Foo<'a> {
    #[serde(borrow)]
    foo: Cow<'a, str>,
}

Will produce following compile error:

#[derive(Deserialize)]
         ^^^^^^^^^^^
         |
         expected struct `beef::generic::Cow`, found enum `std::borrow::Cow`
         in this macro invocation

This PR modifies serde::private::de::borrow_cow_* functions to return a generic type R with a bound R: From<std::borrow::Cow>. It would allow any custom type named Cow that also implements From<std::borrow::Cow<T>> (which it probably should) to be consistent with std::borrow::Cow in deserializing behavior. This should obviously just add a no-op for std Cow.

Breakage?

All the usage of the function within the derive macros should continue working since types will be always easily inferred. Given this function is not part of public documentation I reckon this should be fine even though it's technically a breaking change. Thoughts?

This was referenced Mar 18, 2020
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

@dtolnay dtolnay merged commit c33fb95 into serde-rs:master Mar 18, 2020
@dtolnay
Copy link
Member

dtolnay commented Mar 18, 2020

Published in 1.0.105.

@maciejhirsz maciejhirsz deleted the non-std-cow-borrow branch March 18, 2020 18:45
@maciejhirsz
Copy link
Contributor Author

Works like a charm :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants