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

associated types and bounds #2784

Open
sineptic opened this issue Aug 1, 2024 · 0 comments
Open

associated types and bounds #2784

sineptic opened this issue Aug 1, 2024 · 0 comments

Comments

@sineptic
Copy link

sineptic commented Aug 1, 2024

It's (I think) have save root of problem like in my previous issue #2771.

first try

trait SomeTrait<'a>: Serialize + Deserialize<'a> {
    type SomeType: Serialize + Deserialize<'a>;
    // ...
}

#[derive(Serialize, Deserialize)]
// I must use it because I don't have access to 'de lifetime
#[serde(bound(deserialize = "T: SomeTrait<'de>"))]
struct SomeStruct<T> {
    a: T,
    field2: T::SomeType,
}

COMPILE ERROR!!!: associated type SomeType not found for T


second try

#[derive(Serialize, Deserialize)]
#[serde(bound(deserialize = "T: SomeTrait<'de>"))]
struct SomeStruct<T> {
    a: T,
    // COMPILE ERROR!!!: associated type `SomeType` not found for `T`
    field2: T::SomeType,
}

... some other errors,
help: use fully-qualified syntax to disambiguate
|
14 | field2: <T as SomeTrait<'a>>::SomeType,
| ~~~~~~~~~~~~~~~~~~~~~~
help: use fully-qualified syntax to disambiguate
|
14 | field2: <T as SomeTrait<'de>>::SomeType,
| ~~~~~~~~~~~~~~~~~~~~~~~

hmm.. compiler see <T as SomeTrait<'de>>::SomeType?


third try

#[derive(Serialize, Deserialize)]
#[serde(bound(deserialize = "T: SomeTrait<'de>"))]
struct SomeStruct<T> {
    a: T,
    // COMPILE ERROR!!!: associated type `SomeType` not found for `T`
    field2: <T as SomeTrait<'de>>::SomeType,
}

aghh...
14 | field2: <T as SomeTrait<'de>>::SomeType,
| ^^^ undeclared lifetime


Ok, I know how to make it work:

#[derive(Serialize, Deserialize)]
#[serde(bound(deserialize = "'a: 'de, 'de: 'a"))]
struct SomeStruct<'a, T: SomeTrait<'a>> {
    a: T,
    field2: T::SomeType,
}

BUT

  1. It looks ugly.
  2. Now I must in every impl block add 'a twice.
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