From 79ec0721bf7272307e09c8d529b000ebd8c62837 Mon Sep 17 00:00:00 2001 From: naskya Date: Tue, 16 Jul 2024 22:37:16 +0900 Subject: [PATCH] feat: add `new` function to Relaxed trait --- crates/relax-macros/src/lib.rs | 11 +++++++++++ crates/relax/src/lib.rs | 2 ++ 2 files changed, 13 insertions(+) diff --git a/crates/relax-macros/src/lib.rs b/crates/relax-macros/src/lib.rs index c2e6b82..bc22927 100644 --- a/crates/relax-macros/src/lib.rs +++ b/crates/relax-macros/src/lib.rs @@ -143,6 +143,11 @@ fn derive_impl(input: syn::DeriveInput) -> syn::Result { let base = &input.ident; + let new_fields = fields.iter().map(|field| { + let name = field.ident.as_ref(); + quote! { #name: ::std::option::Option::None } + }); + let from_fields = fields.iter().map(|field| { let name = field.ident.as_ref(); let ty = &field.ty; @@ -216,6 +221,12 @@ fn derive_impl(input: syn::DeriveInput) -> syn::Result { impl #generics ::relax::Relaxed for #partial #generics { type Complete = #base #generics; + fn new() -> Self { + Self { + #(#new_fields),* + } + } + fn merge(self, optb: Self) -> Self { Self { #(#merge_fields),* diff --git a/crates/relax/src/lib.rs b/crates/relax/src/lib.rs index 5325a7c..12adb9a 100644 --- a/crates/relax/src/lib.rs +++ b/crates/relax/src/lib.rs @@ -9,6 +9,8 @@ pub trait Relax: TryFrom { pub trait Relaxed: From { type Complete; + + fn new() -> Self; fn merge(self, optb: Self) -> Self; }