Just “parameter name instead of declaring a lifetime” doesn’t help with declaring a struct containing lifetimes or writing impl
blocks for it (although, re-reading the OP and very briefly scanning the thread I don’t see that mentioned at all).
What might be useful is to separate discussion of the two proposals, they seem to be mostly independent of each other (other than the fact they’re both aiming to help “make the ‘easy things easier’ in the lifetime system”). And I think having a shorthand for declaring and referring to an existing anonymous lifetime is less controversial (and less bikesheddy) than adding new syntax for explicitly anonymising existing named lifetimes.
Expanding the idea I had about allowing the “anonymous” syntax to be used in impl
blocks, I think it would be consistent to allow using the anonymous lifetime marker in an inherent impl
block or an impl Trait for
block where Trait
does not contain any lifetimes itself, e.g.
struct Foo<'a>(&'a [u8]);
impl<'a> Foo<'a> {
fn new(buf: &[u8]) -> Foo {
Foo(buf)
}
fn get(&self, i: usize) -> Option<&u8> {
self.0.get(i)
}
}
impl<'a> AsRef<[u8]> for Foo<'a> {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
could be rewritten as (using the <'>
contender as that’s my preferred choice out of what’s been proposed so far)
struct Foo<'>(&[u8]);
impl Foo<'> {
// Note that this is closer to writing
// fn new(buf: &[u8]) -> Foo<'a>
// above, which currently results in "expected Foo<'a>, found Foo<'_>"
// Hopefully it would be allowable to unify anonymous lifetimes and have this work as expected
fn new(buf: &[u8]) -> Foo<'> {
Foo(buf)
}
fn get(&self, i: usize) -> Option<&u8> {
self.0.get(i)
}
}
impl AsRef<[u8]> for Foo<'> {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
There may be some scope for expanding this to some cases of Trait
containing lifetimes, but I can’t think of what cases those would be right now.