From f6ab69cc48661c18f487d5fb343517bb615c9946 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 2 Jun 2022 13:02:29 -0700 Subject: [PATCH 1/2] Add syn::punctuated::Pair::punct_mut --- src/punctuated.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/punctuated.rs b/src/punctuated.rs index 6525bfa935..0fcbaca59e 100644 --- a/src/punctuated.rs +++ b/src/punctuated.rs @@ -945,6 +945,15 @@ impl Pair { } } + /// Mutably borrows the punctuation from this punctuated pair, unless the + /// pair is the final one and there is no trailing punctuation. + pub fn punct_mut(&mut self) -> Option<&mut P> { + match self { + Pair::Punctuated(_, p) => Some(p), + Pair::End(_) => None, + } + } + /// Creates a punctuated pair out of a syntax tree node and an optional /// following punctuation. pub fn new(t: T, p: Option

) -> Self { From 45b10cbb6abd6cf60fbf12010f6d56a11f03faa5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 2 Jun 2022 13:05:20 -0700 Subject: [PATCH 2/2] Add example of using pair.punct_mut() --- src/punctuated.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/punctuated.rs b/src/punctuated.rs index 0fcbaca59e..0fe1078cf4 100644 --- a/src/punctuated.rs +++ b/src/punctuated.rs @@ -947,6 +947,22 @@ impl Pair { /// Mutably borrows the punctuation from this punctuated pair, unless the /// pair is the final one and there is no trailing punctuation. + /// + /// # Example + /// + /// ``` + /// # use proc_macro2::Span; + /// # use syn::punctuated::Punctuated; + /// # use syn::{parse_quote, Token, TypeParamBound}; + /// # + /// # let mut punctuated = Punctuated::::new(); + /// # let span = Span::call_site(); + /// # + /// punctuated.insert(0, parse_quote!('lifetime)); + /// if let Some(punct) = punctuated.pairs_mut().next().unwrap().punct_mut() { + /// punct.span = span; + /// } + /// ``` pub fn punct_mut(&mut self) -> Option<&mut P> { match self { Pair::Punctuated(_, p) => Some(p),