feat: add new function to Relaxed trait

This commit is contained in:
naskya 2024-07-16 22:37:16 +09:00
parent 2012cef856
commit 79ec0721bf
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
2 changed files with 13 additions and 0 deletions

View file

@ -143,6 +143,11 @@ fn derive_impl(input: syn::DeriveInput) -> syn::Result<TokenStream> {
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<TokenStream> {
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),*

View file

@ -9,6 +9,8 @@ pub trait Relax: TryFrom<Self::Partial, Error = RequiredFieldNotSet> {
pub trait Relaxed: From<Self::Complete> {
type Complete;
fn new() -> Self;
fn merge(self, optb: Self) -> Self;
}