From 39c8cdc6419a6c0697e50c14ff72331c04b482ae Mon Sep 17 00:00:00 2001 From: Chris Russell <8494645+chescock@users.noreply.github.com> Date: Sat, 5 Sep 2026 09:57:43 -0400 Subject: [PATCH] Elide lifetime parameters. --- src/short_name.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/short_name.rs b/src/short_name.rs index 6450a42..bb0162d 100644 --- a/src/short_name.rs +++ b/src/short_name.rs @@ -82,6 +82,23 @@ impl<'a> core::fmt::Debug for ShortName<'a> { let special_character = &rest_of_string[special_character_index..=special_character_index]; + if special_character == "<" { + let mut end_lifetime_index = special_character_index + 1; + // Elide lifetime parameters, which always come first in generics. + while rest_of_string[end_lifetime_index..].starts_with("'_, ") { + end_lifetime_index += 4; + } + if rest_of_string[end_lifetime_index..].starts_with("'_>") { + // If all parameters are lifetime parameters, + // also elide the angle brackets. + end_lifetime_index += 3; + } else { + f.write_str("<")?; + } + index += end_lifetime_index; + continue; + } + f.write_str(special_character)?; match special_character { @@ -236,4 +253,14 @@ mod name_formatting_tests { "*mut T<*mut U>" ); } + + #[test] + fn lifetimes() { + assert_eq!(ShortName("t::T<'_>").to_string(), "T"); + assert_eq!(ShortName("t::T<'_, '_>").to_string(), "T"); + assert_eq!(ShortName("t::T<'_, '_, '_>").to_string(), "T"); + assert_eq!(ShortName("t::T<'_, u::U>").to_string(), "T"); + assert_eq!(ShortName("t::T<'_, '_, u::U>").to_string(), "T"); + assert_eq!(ShortName("t::T<'_, '_, '_, u::U>").to_string(), "T"); + } }