# Generic parameter bound by \`Deserialize\<'de\>\`. Is this pattern possible in Rust?

**URL:** <https://users.rust-lang.org/t/generic-parameter-bound-by-deserialize-de-is-this-pattern-possible-in-rust/82163>\
**Category:** help\
**Created:** [October 2, 2022, 5:31pm UTC](https://users.rust-lang.org/t/generic-parameter-bound-by-deserialize-de-is-this-pattern-possible-in-rust/82163 "2022-10-02T17:31:45Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [October 2, 2022, 10:14pm UTC](https://users.rust-lang.org/t/generic-parameter-bound-by-deserialize-de-is-this-pattern-possible-in-rust/82163/3 "2022-10-02T22:14:21Z")

</div>

Talking about the errors / trait bounds / what is possible, and ignoring design considerations...

> [@jukanntenn](#):
>
> Apparently, there is no lifetime issue at runtime. But since Res was bound to lifetime 'de declared in `run` function, so rust complains. (My understanding of lifetime here is I said `Res` must live long enough than the scope of fuction `run`, but actually it is not).

A lifetime parameter on a function means, "I can work with any lifetime _that the **caller** chooses_." All you know is that it lasts _at least as long_ as your function body -- callers can't name any shorter lifetimes than that -- and any such lifetime is longer than you can borrow a local variable for.

For lifetimes shorter than the function body, you need higher-ranked trait bounds (HRTBs): `for<'any> [bound...]`. HTRBs mean something more like "the generic type that the caller supplies can work with any lifetime (that I, the function writer, choose)". Then you can work with lifetimes shorter than your function body (e.g. that borrow locals). As it so happens, you were already using one; these are all the same thing with varying amounts of syntactic sugar:

```rust
fn run<Callback>(mut callback: Callback) where Callback: FnMut(Info),
fn run<Callback>(mut callback: Callback) where Callback: FnMut(Info<'_>),
fn run<Callback>(mut callback: Callback)
where
    Callback: for<'any> FnMut(Info<'any>),

```

And that's why your original compiled. (Incidentally, I recommend [always writing `Info<'_>` instead of just `Info`,](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#elided-lifetimes-in-paths) et cetera, to make implicit HRTB and implicit borrows more obvious in the code.)

* * *

After reading that, you might be tempted to try:

```rust
fn run<Res: for<'de> Deserialize<'de>, Callback>(mut callback: Callback)
where
    Callback: FnMut(Res),

```

[The method will compile with such a bound, but you won't be able to actually use it like you think.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0b9847eec89e4d225022c1148d7d97c6) (Also in case you didn't know, `DeserializedOwned` is a convenient wrapper around that exact bound.)

There are two related problems, with the same root cause: Types that vary by lifetime _are still distinct types_. `Info` is not a type; it is a _type constructor_. What is a type is `Info<'a>` where `'a` has taken on some specific, concrete lifetime. Another thing to understand is that generic type parameters _must represent a single type,_ and not a type constructor like `Info` without a concrete lifetime.

The first problem is what the error message is about: There is no `Info<'concrete>` where `Info<'concrete>: for<'any> Deserialize<'any>`. Instead, `Info<'concrete>` implements `Deserialize<'concrete>`.

The second problem is that you need your closure to be generic over the input lifetime; you want a `for<'any> FnMut(Info<'any>)` like you had before. But you're trying to replace that with `FnMut(Res)` where `Res` is a type parameter. This won't work because, again, `Res` must represent a single type, not a type constructor.

* * *

Is there any way forward? Yes -- you can emulate generic type constructors by using a trait with a GAT, or with a GAT-emulating lifetime-taking trait.

[However, the abstraction is somewhat complicated and it tends to completely wreck inference.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=75741143424f5b71e4c496393d3772fc)

[You can sometimes recover inference by replacing enough generic implementations with concrete ones.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fa1493648af6e9c95070a75fbfe3bb8b) If you do so, the boilerplate grows enough you might want a macro you call on each type. (At this point there's an argument you're not gaining much with the generic framework over just macroing your original repetitive code.)

[Discussion of a similar, more general use case.](https://users.rust-lang.org/t/problems-matching-up-lifetimes-between-various-traits-and-closure-parameters/71994/7)

---

_[View the full topic](https://users.rust-lang.org/t/generic-parameter-bound-by-deserialize-de-is-this-pattern-possible-in-rust/82163)._
