From bcde7b17a61fe9a96ea903f864bbe1d1aa6d9a69 Mon Sep 17 00:00:00 2001 From: rzvxa Date: Sun, 1 Sep 2024 13:18:32 +0330 Subject: [PATCH] refactor: cleanup. --- tasks/ast_tools/src/codegen.rs | 37 ++++++++++++------------- tasks/ast_tools/src/derives/clone_in.rs | 2 -- tasks/ast_tools/src/derives/get_span.rs | 2 -- tasks/ast_tools/src/derives/mod.rs | 2 ++ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/tasks/ast_tools/src/codegen.rs b/tasks/ast_tools/src/codegen.rs index eb4bff99b7f633..4722b444855c9a 100644 --- a/tasks/ast_tools/src/codegen.rs +++ b/tasks/ast_tools/src/codegen.rs @@ -27,39 +27,36 @@ pub struct AstCodegenResult { pub outputs: Vec, } -pub enum SideEffect { - /// File write side-effect - Write(/* path */ PathBuf, /* output */ Vec), -} +pub struct SideEffect(/* path */ pub PathBuf, /* output */ pub Vec); impl SideEffect { /// Apply the side-effect pub fn apply(self) -> std::io::Result<()> { - match self { - Self::Write(path, data) => { - let path = path.into_os_string(); - let path = path.to_str().unwrap(); - write_all_to(&data, path)?; - Ok(()) - } - } + let Self(path, data) = self; + let path = path.into_os_string(); + let path = path.to_str().unwrap(); + write_all_to(&data, path)?; + Ok(()) } #[allow(clippy::unnecessary_wraps)] pub fn path(&self) -> Option { - match self { - Self::Write(path, _) => { - let path = path.to_string_lossy(); - Some(path.replace('\\', "/")) - } - } + let Self(path, _) = self; + let path = path.to_string_lossy(); + Some(path.replace('\\', "/")) } } impl From<(PathBuf, TokenStream)> for SideEffect { fn from((path, stream): (PathBuf, TokenStream)) -> Self { let content = pretty_print(&stream); - Self::Write(path, content.as_bytes().into()) + Self(path, content.as_bytes().into()) + } +} + +impl From for SideEffect { + fn from(output: GeneratorOutput) -> Self { + Self::from((output.0, output.1)) } } @@ -207,7 +204,7 @@ impl AstCodegen { .generators .into_iter() .map(|mut runner| runner.run(&ctx)) - .map_ok(|output| SideEffect::from((output.0, output.1))) + .map_ok(SideEffect::from) .chain(derives) .collect::>>()?; diff --git a/tasks/ast_tools/src/derives/clone_in.rs b/tasks/ast_tools/src/derives/clone_in.rs index 8621b95e8c17ab..6099b50ad82fbf 100644 --- a/tasks/ast_tools/src/derives/clone_in.rs +++ b/tasks/ast_tools/src/derives/clone_in.rs @@ -91,7 +91,6 @@ fn impl_clone_in( ) -> TokenStream { if has_lifetime { quote! { - ///@@line_break impl <'old_alloc, 'new_alloc> CloneIn<'new_alloc> for #ty_ident<'old_alloc> { type Cloned = #ty_ident<'new_alloc>; fn clone_in(&self, #alloc_ident: &'new_alloc Allocator) -> Self::Cloned { @@ -101,7 +100,6 @@ fn impl_clone_in( } } else { quote! { - ///@@line_break impl <'alloc> CloneIn<'alloc> for #ty_ident { type Cloned = #ty_ident; fn clone_in(&self, #alloc_ident: &'alloc Allocator) -> Self::Cloned { diff --git a/tasks/ast_tools/src/derives/get_span.rs b/tasks/ast_tools/src/derives/get_span.rs index 204194b8274a0d..2eb0d9840d1d21 100644 --- a/tasks/ast_tools/src/derives/get_span.rs +++ b/tasks/ast_tools/src/derives/get_span.rs @@ -98,7 +98,6 @@ fn derive_enum( }); quote! { - ///@@line_break impl #generics #trait_name for #target_type { fn #method_name(#self_type) -> #result_type { match self { @@ -129,7 +128,6 @@ fn derive_struct( }; quote! { - ///@@line_break impl #generics #trait_name for #target_type { #[inline] fn #method_name(#self_type) -> #result_type { diff --git a/tasks/ast_tools/src/derives/mod.rs b/tasks/ast_tools/src/derives/mod.rs index b3cd9f3b72bc17..dbcf4c76ba5acb 100644 --- a/tasks/ast_tools/src/derives/mod.rs +++ b/tasks/ast_tools/src/derives/mod.rs @@ -27,6 +27,7 @@ pub struct DeriveOutput(pub Vec<(PathBuf, TokenStream)>); macro_rules! define_derive { ($vis:vis struct $ident:ident $($lifetime:lifetime)? $($rest:tt)*) => { $vis struct $ident $($lifetime)? $($rest)* + impl $($lifetime)? $crate::derives::DeriveTemplate for $ident $($lifetime)? { fn template(module_paths: Vec<&str>, impls: TokenStream) -> TokenStream { use itertools::Itertools; @@ -61,6 +62,7 @@ macro_rules! define_derive { } } } + impl $($lifetime)? $crate::codegen::Runner for $ident $($lifetime)? { type Context = $crate::codegen::LateCtx; type Output = $crate::derives::DeriveOutput;