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

GAT lifetime not added to 'de bounds #2542

Closed
tguichaoua opened this issue Jul 29, 2023 · 1 comment
Closed

GAT lifetime not added to 'de bounds #2542

tguichaoua opened this issue Jul 29, 2023 · 1 comment

Comments

@tguichaoua
Copy link

I'm trying to use Generic Associated Types in structure that derive serde::Deserialize.

But because of the GAT the bound 'de: 'a is not added for A.

I know it's possible to use #[serde(bound = "'de: 'a")] but it's surprising the bounds used by GAT are ignored.

trait Trait {
    type Gat<'a>;
}

struct Foo;

impl Trait for Foo {
    type Gat<'a> = &'a str;
}

struct Goo;

impl Trait for Goo {
    type Gat<'a> = String;
}

#[derive(serde::Deserialize)]
struct A<'a> {
    x: <Foo as Trait>::Gat<'a>, // lifetime may not live long enough consider adding the following bound: `'de: 'a`
    y: <Goo as Trait>::Gat<'a>, // this one is OK
}

#[derive(serde::Deserialize)]
struct B<'a> {
    x: &'a str, // no problem when inlined
}

Output of cargo expand

impl<'de, 'a> _serde::Deserialize<'de> for A<'a> {
// ...
impl<'de: 'a, 'a> _serde::Deserialize<'de> for B<'a> {
@tguichaoua tguichaoua changed the title GAT lifetime not recognized GAT lifetime not added to 'de bounds Jul 29, 2023
@tguichaoua
Copy link
Author

I check the docs and it's seems the solution is to use #[serde(borrow)].
https://serde.rs/lifetimes.html#borrowing-data-in-a-derived-impl

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

No branches or pull requests

1 participant