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

Deriving Serialize for a struct from external crate which uses generics #2327

Closed
AntoineBalaine opened this issue Nov 27, 2022 · 3 comments · Fixed by #2330
Closed

Deriving Serialize for a struct from external crate which uses generics #2327

AntoineBalaine opened this issue Nov 27, 2022 · 3 comments · Fixed by #2330

Comments

@AntoineBalaine
Copy link

AntoineBalaine commented Nov 27, 2022

Hello,
I'm trying to implement Serialize for nom_locate's LocatedSpan. It's a struct that encapsulates a token with some location information. can't figure out how to implement Serialize for it.

I created a WrappedSpan to build the bridge with LocatedSpan, but the derive macro doesn't chew up the passed-in generic. Is there a way around this?

#[derive(Serialize, Deserialize)]
#[serde(remote = "LocatedSpan<T>")] // <= This <T> produces an un-parseable token
struct WrappedSpan<T: AsBytes>{
    #[serde(getter = "LocatedSpan::location_offset")]
    offset: usize,
    #[serde(getter = "LocatedSpan::location_line")]
    line: u32,
    #[serde(getter = "LocatedSpan::fragment")]
    fragment: T,
};

// Provide a conversion to construct the remote type.
impl <T: AsBytes>From<WrappedSpan<T>> for LocatedSpan<T> {
    fn from(def: WrappedSpan<T>) -> LocatedSpan<T> {
        LocatedSpan{
            offset: def.offset,
            line: def.line,
            fragment: def.fragment,
            extra: (),
        }
    }
}
@AntoineBalaine
Copy link
Author

Thank you for this update!

@dtolnay
Copy link
Member

dtolnay commented Nov 28, 2022

Fixed in serde_derive 1.0.148. You need to use serde(remote = "LocatedSpan").

@AntoineBalaine
Copy link
Author

AntoineBalaine commented Nov 28, 2022

Could you be a bit more specific ? I can't seem to figure it out with the update:

#[derive(Serialize, Deserialize, PartialEq, Eq)]
#[serde(remote = "LocatedSpan")]
struct WrappedSpan<T: AsBytes> {
    #[serde(getter = "LocatedSpan::location_offset")]
    offset: usize,
    #[serde(getter = "LocatedSpan::location_line")]
    line: u32,
    #[serde(getter = "LocatedSpan::fragment")]
    fragment: T,
}

impl<T: AsBytes> From<WrappedSpan<T>> for LocatedSpan<T> {
    fn from(def: WrappedSpan<T>) -> LocatedSpan<T> {
        LocatedSpan::new(def.fragment)
    }
}

#[test]
fn serialize_located_span() {
    let input = LocatedSpan::new("");
    let serialized = serde_json::to_string(&input).unwrap(); //<= the trait bound `LocatedSpan<&str>: Serialize` is not satisfied
    let converted = "{\"offset\":0,\"line\":1,\"fragment\":\"\",\"column\":1}";
    assert_eq!(serialized, converted)
}

crapStone added a commit to Calciumdibromid/CaBr2 that referenced this issue Dec 1, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [serde](https://serde.rs) ([source](https://github.com/serde-rs/serde)) | dependencies | patch | `1.0.147` -> `1.0.148` |

---

### Release Notes

<details>
<summary>serde-rs/serde</summary>

### [`v1.0.148`](https://github.com/serde-rs/serde/releases/tag/v1.0.148)

[Compare Source](serde-rs/serde@v1.0.147...v1.0.148)

-   Support `remote` derive for generic types that have private fields ([#&#8203;2327](serde-rs/serde#2327))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC40MC4wIiwidXBkYXRlZEluVmVyIjoiMzQuNDIuMCJ9-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1657
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants