Skip to content

Commit

Permalink
Compatibility with 1.65 MSRV
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranron committed Jul 31, 2024
1 parent 8a8eac9 commit 46362f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
7 changes: 6 additions & 1 deletion impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ impl<'a> Expansion<'a> {
fn generate_body(&self) -> syn::Result<TokenStream> {
if let Some(fmt) = &self.attr.fmt {
return Ok(if let Some((expr, trait_ident)) = fmt.transparent_call() {
let expr = if self.fields.fmt_args_idents().any(|field| expr == field) {
let expr = if self
.fields
.fmt_args_idents()
.into_iter()
.any(|field| expr == field)
{
quote! { #expr }
} else {
quote! { &(#expr) }
Expand Down
17 changes: 10 additions & 7 deletions impl/src/fmt/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,16 @@ impl<'a> Expansion<'a> {

quote! { &derive_more::core::format_args!(#fmt, #(#deref_args),*) }
} else if let Some((expr, trait_ident)) = fmt.transparent_call() {
let expr =
if self.fields.fmt_args_idents().any(|field| expr == field)
{
quote! { #expr }
} else {
quote! { &(#expr) }
};
let expr = if self
.fields
.fmt_args_idents()
.into_iter()
.any(|field| expr == field)
{
quote! { #expr }
} else {
quote! { &(#expr) }
};

quote! {
derive_more::core::fmt::#trait_ident::fmt(#expr, __derive_more_f)
Expand Down
24 changes: 15 additions & 9 deletions impl/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,16 @@ impl FmtAttribute {
})
.collect::<Vec<_>>();

fields.fmt_args_idents().filter_map(move |field_name| {
(used_args.iter().any(|arg| field_name == arg)
&& !self.args.iter().any(|arg| {
arg.alias.as_ref().map_or(false, |(n, _)| n == &field_name)
}))
.then(|| quote! { #field_name = *#field_name })
})
fields
.fmt_args_idents()
.into_iter()
.filter_map(move |field_name| {
(used_args.iter().any(|arg| field_name == arg)
&& !self.args.iter().any(|arg| {
arg.alias.as_ref().map_or(false, |(n, _)| n == &field_name)
}))
.then(|| quote! { #field_name = *#field_name })
})
}

/// Errors in case legacy syntax is encountered: `fmt = "...", (arg),*`.
Expand Down Expand Up @@ -671,14 +674,17 @@ trait FieldsExt {
/// [`FmtAttribute`] as [`FmtArgument`]s or named [`Placeholder`]s.
///
/// [`syn::Ident`]: struct@syn::Ident
fn fmt_args_idents(&self) -> impl Iterator<Item = syn::Ident> + '_;
// TODO: Return `impl Iterator<Item = syn::Ident> + '_` once MSRV is bumped up to 1.75 or
// higher.
fn fmt_args_idents(&self) -> Vec<syn::Ident>;
}

impl FieldsExt for syn::Fields {
fn fmt_args_idents(&self) -> impl Iterator<Item = syn::Ident> + '_ {
fn fmt_args_idents(&self) -> Vec<syn::Ident> {
self.iter()
.enumerate()
.map(|(i, f)| f.ident.clone().unwrap_or_else(|| format_ident!("_{i}")))
.collect()
}
}

Expand Down

0 comments on commit 46362f6

Please sign in to comment.