Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3c81ead494d0897d6cd49f20d51302df
pub struct StrWriter<'w, W> {
inner: &'w mut W,
}
impl<'w, W> StrWriter<'w, W> {
pub fn reborrow(&mut self) -> StrWriter<'_, W> {
Self {
inner: self.inner,
}
}
pub fn reborrow_explicit_lts<'a>(&'a mut self) -> StrWriter<'a, W> {
Self {
inner: self.inner,
}
}
}
The current output is:
error: lifetime may not live long enough
--> src/lib.rs:8:20
|
5 | impl<'w, W> StrWriter<'w, W> {
| -- lifetime `'w` defined here
6 | pub fn reborrow(&mut self) -> StrWriter<'_, W> {
| - let's call the lifetime of this reference `'1`
7 | Self {
8 | inner: self.inner,
| ^^^^^^^^^^ this usage requires that `'1` must outlive `'w`
error: lifetime may not live long enough
--> src/lib.rs:14:20
|
5 | impl<'w, W> StrWriter<'w, W> {
| -- lifetime `'w` defined here
...
12 | pub fn reborrow_explicit_lts<'a>(&'a mut self) -> StrWriter<'a, W> {
| -- lifetime `'a` defined here
13 | Self {
14 | inner: self.inner,
| ^^^^^^^^^^ this usage requires that `'a` must outlive `'w`
|
= help: consider adding the following bound: `'a: 'w`
Ideally the output should look like:
error: lifetime of `Self` does not live long enough
--> src/lib.rs:8:20
|
5 | impl<'w, W> StrWriter<'w, W> {
| -- lifetime `'w` defined here
6 | pub fn reborrow(&mut self) -> StrWriter<'_, W> {
| - let's call the lifetime of this reference `'1`
7 | Self {
7 | ^^^^ help: build the struct value with `StrWriter` instead of `Self`
8 | inner: self.inner,
| ^^^^^^^^^^ this usage requires that `'1` must outlive `'w`
error: lifetime of `Self` does not live long enough
--> src/lib.rs:14:20
|
5 | impl<'w, W> StrWriter<'w, W> {
| -- lifetime `'w` defined here
...
12 | pub fn reborrow_explicit_lts<'a>(&'a mut self) -> StrWriter<'a, W> {
| -- lifetime `'a` defined here
13 | Self {
| ^^^^ help: build the struct value with `StrWriter` instead of `Self`
14 | inner: self.inner,
| ^^^^^^^^^^ this usage requires that `'a` must outlive `'w`
Using Self instead of the struct name when self and the return type of the method only differs in lifetimes will never work, and the user must write the returned struct value with the struct name, without referring to Self. The help note for the reborrow_explicit_lts is especially egregious because adding the bound 'a: 'w actually makes the method completely defeat its own purpose of returning the same value with a shorter lifetime.
Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3c81ead494d0897d6cd49f20d51302df
The current output is:
Ideally the output should look like:
Using
Selfinstead of the struct name whenselfand the return type of the method only differs in lifetimes will never work, and the user must write the returned struct value with the struct name, without referring toSelf. The help note for thereborrow_explicit_ltsis especially egregious because adding the bound'a: 'wactually makes the method completely defeat its own purpose of returning the same value with a shorter lifetime.