From c4d5245b0b305d94115b93a1c5e94127bf8599a4 Mon Sep 17 00:00:00 2001 From: Ryan Johnson Date: Mon, 12 Aug 2024 15:10:13 -0700 Subject: [PATCH] address review: simplify function type erasure logic --- src/bindgen/ir/ty.rs | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/bindgen/ir/ty.rs b/src/bindgen/ir/ty.rs index c396eeda..78690eca 100644 --- a/src/bindgen/ir/ty.rs +++ b/src/bindgen/ir/ty.rs @@ -600,30 +600,24 @@ impl Type { is_nullable, never_return, } => { - // Predict that ret+args will all be erased, but decrement the count whenever we're - // wrong. If the count drops to 0, then type erasure was a no-op after all. - let mut num_erased = 1 + args.len(); - let erased_ret = eraser - .erase_transparent_types(library, ret, mappings) - .unwrap_or_else(|| { - num_erased -= 1; - ret.as_ref().clone() - }); - + // Attempt to erase ret and all args; if any of them were actually erased, then + // assemble and return the simplified function signature that results. + let mut erased_any = false; + let mut try_erase = |ty| { + if let Some(erased) = eraser.erase_transparent_types(library, ty, mappings) { + erased_any = true; + erased + } else { + ty.clone() + } + }; + let erased_ret = try_erase(ret); let erased_args = args .iter() - .map(|(name, ty)| { - let erased_ty = eraser - .erase_transparent_types(library, ty, mappings) - .unwrap_or_else(|| { - num_erased -= 1; - ty.clone() - }); - (name.clone(), erased_ty) - }) + .map(|(name, ty)| (name.clone(), try_erase(ty))) .collect(); - if num_erased > 0 { + if erased_any { return Some(Type::FuncPtr { ret: Box::new(erased_ret), args: erased_args,