From dbae03a9c84242d48612c56422b85d643c55790c Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 14 Jul 2024 07:44:55 +0800 Subject: [PATCH] Small code refactor (#6263) ## Description - Optimize code ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: IGI-111 Co-authored-by: Joshua Batty --- sway-core/src/abi_generation/abi_str.rs | 31 +++++++++---------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/sway-core/src/abi_generation/abi_str.rs b/sway-core/src/abi_generation/abi_str.rs index 3c94fc3a731..bc3618abac3 100644 --- a/sway-core/src/abi_generation/abi_str.rs +++ b/sway-core/src/abi_generation/abi_str.rs @@ -17,20 +17,17 @@ impl TypeId { resolved_type_id: TypeId, ) -> String { let type_engine = engines.te(); + let self_abi_str = type_engine.get(*self).abi_str(ctx, engines); if self.is_generic_parameter(engines, resolved_type_id) { - format!("generic {}", type_engine.get(*self).abi_str(ctx, engines)) + format!("generic {}", self_abi_str) } else { match ( &*type_engine.get(*self), &*type_engine.get(resolved_type_id), ) { - (TypeInfo::Custom { .. }, TypeInfo::Struct { .. }) => { - type_engine.get(resolved_type_id).abi_str(ctx, engines) - } - (TypeInfo::Custom { .. }, TypeInfo::Enum { .. }) => { - type_engine.get(resolved_type_id).abi_str(ctx, engines) - } - (TypeInfo::Custom { .. }, TypeInfo::Alias { .. }) => { + (TypeInfo::Custom { .. }, TypeInfo::Struct { .. }) + | (TypeInfo::Custom { .. }, TypeInfo::Enum { .. }) + | (TypeInfo::Custom { .. }, TypeInfo::Alias { .. }) => { type_engine.get(resolved_type_id).abi_str(ctx, engines) } (TypeInfo::Tuple(fields), TypeInfo::Tuple(resolved_fields)) => { @@ -57,7 +54,7 @@ impl TypeId { format!("[{}; {}]", inner_type, count.val()) } (TypeInfo::Custom { .. }, _) => { - format!("generic {}", type_engine.get(*self).abi_str(ctx, engines)) + format!("generic {}", self_abi_str) } _ => type_engine.get(resolved_type_id).abi_str(ctx, engines), } @@ -185,19 +182,13 @@ fn call_path_display(ctx: &AbiStrContext, call_path: &CallPath) -> String { return call_path.suffix.as_str().to_string(); } let mut buf = String::new(); + let root_name = ctx.program_name.as_deref(); for (index, prefix) in call_path.prefixes.iter().enumerate() { - let mut skip_prefix = false; - if index == 0 { - if let Some(root_name) = &ctx.program_name { - if prefix.as_str() == root_name.as_str() { - skip_prefix = true; - } - } - } - if !skip_prefix { - buf.push_str(prefix.as_str()); - buf.push_str("::"); + if index == 0 && Some(prefix.as_str()) == root_name { + continue; } + buf.push_str(prefix.as_str()); + buf.push_str("::"); } buf.push_str(&call_path.suffix.to_string());