# 'a: T bound

**URL:** https://internals.rust-lang.org/t/a-t-bound/13924
**Category:** Uncategorized
**Created:** [January 28, 2021, 3:28pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924 "2021-01-28T15:28:03Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [January 28, 2021, 3:28pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/1 "2021-01-28T15:28:04Z")

</div>

Here's a crazy idea! Could we support `'a: T` with the semantics that `'a` must live at least as long as all the lifetime parameters in `T`? Currently, this can be simulated with traits:

[https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b6579aa577c0b679d28c50b51da5ffdc](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b6579aa577c0b679d28c50b51da5ffdc)

```rust
pub trait MaxLifetime<'a> {}

```

The rules for implementing `MaxLifetime<'a>` for a given type `T`:

All the fields of `T` should implement `MaxLifetime<'a>`. All types with no type or lifetime parameters implement `for<'a> MaxLifetime<'a>`, and references `MaxLifetime<'a>` like so

```rust
impl<'a: 'b, 'b, T: ?Sized> MaxLifetime<'a> for &'b T {}

```

These rules could be encoded via a derive macro, however, this feels like something that the language should handle.

Spawned from this [URLO question](https://users.rust-lang.org/t/reduce-lifetime-of-fields-in-generic-parameter/54782)

---

<div class="post-metadata">

### Author: ![elidupree](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/elidupree/32/4304_2.png) [@elidupree](https://internals.rust-lang.org/u/elidupree)
#### Post date: [January 28, 2021, 4:08pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/2 "2021-01-28T16:08:32Z")

</div>

The syntax `'a: T` seems _much_ too clever and would add to lifetime confusion by Rust learners. I don't mean to doubt that this would be a useful feature, but an uncommonly-used feature like this should have a verbose name that gives people who haven't seen it before a clearer hint about the meaning.

---

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [January 28, 2021, 7:53pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/3 "2021-01-28T19:53:06Z")

</div>

I agree that any new extension to lifetimes should be met with scrutiny, in large part because beginners get tripped up with lifetimes. However since the syntax and semantics are so similar to existing constructs, I don't think this is too much of an additional burden. Said another way, if you understand `T: 'a`, then `'a: T` is not that big of a step because they are analogous in syntax _and_ semantics.

I agree that this is a niche use-case, but it has come up more than a few times in my code and in others' as well.

---

<div class="post-metadata">

### Author: ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)
#### Post date: [January 28, 2021, 7:56pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/4 "2021-01-28T19:56:34Z")

</div>

What would be the semantics of this when `T` doesn't contain any lifetimes? And error or would it be the same as `'a: 'static`?

---

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [January 28, 2021, 7:57pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/5 "2021-01-28T19:57:48Z")

</div>

This rules specifies it

> All types with no type or lifetime parameters implement `for<'a> MaxLifetime<'a>`

---

<div class="post-metadata">

### Author: ![programmerjake](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/programmerjake/32/5893_2.png) [@programmerjake](https://internals.rust-lang.org/u/programmerjake)
#### Post date: [January 28, 2021, 8:01pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/6 "2021-01-28T20:01:52Z")

</div>

This means `struct A(&'static str);` couldn't be used where `struct A(String);` could, which doesn't seem right...

This is because you could have code:

```rust
fn f<'a, T>(v: T, r: &'a u8) where 'a: T {
}

struct A<T>(T);

fn g() {
    let v: u8 = 0;
    f::<A<&'static str>>(A(""), &v); // fails because v doesn't live as long as 'static
    f::<A<String>>(A(String::new()), &v); // succeeds because String has no lifetimes
}

```

---

<div class="post-metadata">

### Author: ![programmerjake](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/programmerjake/32/5893_2.png) [@programmerjake](https://internals.rust-lang.org/u/programmerjake)
#### Post date: [January 28, 2021, 8:18pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/7 "2021-01-28T20:18:17Z")

</div>

A solution could be to have the compiler special-case explicit `'static` bounds to mean that references should be treated like they has no lifetimes

---

<div class="post-metadata">

### Author: ![atagunov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/atagunov/32/5877_2.png) [@atagunov](https://internals.rust-lang.org/u/atagunov)
#### Post date: [January 28, 2021, 8:47pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/8 "2021-01-28T20:47:06Z")

</div>

> [@programmerjake](#):
>
> `f::<A<String>>(A(String::new()), &v); // succeeds because String has no lifetimes`

Me - still a relative newbie - reads this as requiring `&v` to be `'static`. Which it is not, so this should fail.

Have I misread the proposal? If I have is this a way to enhance it?

---

<div class="post-metadata">

### Author: ![elidupree](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/elidupree/32/4304_2.png) [@elidupree](https://internals.rust-lang.org/u/elidupree)
#### Post date: [January 28, 2021, 8:48pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/9 "2021-01-28T20:48:54Z")

</div>

> [@RustyYato](#):
>
> Said another way, if you understand `T: 'a` , then `'a: T` is not that big of a step because they are analogous in syntax _and_ semantics.

I dispute this, because it took me tens of minutes to understand what this feature means, and now that I _do_ understand what it means, it seems like it is not what I would expect from the syntax `'a: T`. In particular, `T: 'a` means that `T`'s lifetime includes all of `'a`, so I would expect that `'a: T` would mean that `'a` includes all of `T`'s lifetime (so, for example, `'a: i32` would imply that `'a` is `'static`).

I also haven't been able to grok what the URLO thread is trying to do that requires this, so it's hard for me to come up with an alternative syntax that I would prefer (since that would require me to understand the purpose of the feature).

---

<div class="post-metadata">

### Author: ![atagunov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/atagunov/32/5877_2.png) [@atagunov](https://internals.rust-lang.org/u/atagunov)
#### Post date: [January 28, 2021, 8:51pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/10 "2021-01-28T20:51:25Z")

</div>

> [@elidupree](#):
>
> so I would expect that `'a: T` would mean that `'a` includes all of `T` 's lifetime

Exactly my reading. Is this not what @RustyYato suggested?

---

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [January 28, 2021, 8:52pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/11 "2021-01-28T20:52:12Z")

</div>

After thinking about this some more, I have to agree with @elidupree here. @atagunov's confusion on this proposal and @programmerjake's example which highlights my own misunderstanding of how this ought to be are telling that this proposal isn't as simple as I had thought. Thanks for all the feedback!

> [@atagunov](#):
>
> Me - still a relative newbie - reads this as requiring `&v` to be `'static` . Which it is not, so this should fail.

That does sound most consistent, unfortunately that's not too useful so I'm unsure how to fix the proposal.

---

<div class="post-metadata">

### Author: ![programmerjake](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/programmerjake/32/5893_2.png) [@programmerjake](https://internals.rust-lang.org/u/programmerjake)
#### Post date: [January 28, 2021, 9:27pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/12 "2021-01-28T21:27:29Z")

</div>

> [@atagunov](#):
>
> > [@programmerjake](#):
> >
> > `f::<A<String>>(A(String::new()), &v); // succeeds because String has no lifetimes`
> 
> Me - still a relative newbie - reads this as requiring `&v` to be `'static` . Which it is not, so this should fail.
> 
> Have I misread the proposal? If I have is this a way to enhance it?

Since `String` is a type with no lifetimes, it falls under the rule: "All types with no type or lifetime parameters implement `for<'a> MaxLifetime<'a>`", which means `'a: String` for all lifetimes `'a`, in particular including the lifetime of `v`.

---

<div class="post-metadata">

### Author: ![nakacristo](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nakacristo/32/10249_2.png) [@nakacristo](https://internals.rust-lang.org/u/nakacristo)
#### Post date: [January 28, 2021, 9:30pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/13 "2021-01-28T21:30:30Z")

</div>

To address the purpose of this, it is not enough to tie the lifetime in the return type? Like `fn borrow_from<'a,T>(&'a self) -> Bind<'a,T>{...}`

[playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f33e09765ead2d252ba06f69e7b24fe4)

---

<div class="post-metadata">

### Author: ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)
#### Post date: [April 28, 2021, 9:31pm UTC](https://internals.rust-lang.org/t/a-t-bound/13924/14 "2021-04-28T21:31:06Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
