~ireas/public-inbox

merge-rs: Implement derive(Merge) for structs with generics v1 PROPOSED

René Kijewski: 1
 Implement derive(Merge) for structs with generics

 2 files changed, 54 insertions(+), 3 deletions(-)
#729648 archlinux-msrv.yml failed
#729649 archlinux.yml failed
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~ireas/public-inbox/patches/30794/mbox | git am -3
Learn more about email & git

[PATCH merge-rs] Implement derive(Merge) for structs with generics Export this patch

---
 merge_derive/src/lib.rs |  9 +++++---
 tests/derive.rs         | 48 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/merge_derive/src/lib.rs b/merge_derive/src/lib.rs
index 5b111c5..2f51617 100644
--- a/merge_derive/src/lib.rs
+++ b/merge_derive/src/lib.rs
@@ -43,8 +43,9 @@ fn impl_merge(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let default_strategy = FieldAttrs::from(ast.attrs.iter());

    let (impl_generics, orig_ty_generics, where_clause) = ast.generics.split_for_impl();
    set_dummy(quote! {
        impl ::merge::Merge for #name {
        impl #impl_generics ::merge::Merge for #name #orig_ty_generics #where_clause {
            fn merge(&mut self, other: Self) {
                unimplemented!()
            }
@@ -52,7 +53,7 @@ fn impl_merge(ast: &syn::DeriveInput) -> TokenStream {
    });

    if let syn::Data::Struct(syn::DataStruct { ref fields, .. }) = ast.data {
        impl_merge_for_struct(name, fields, default_strategy)
        impl_merge_for_struct(name, fields, default_strategy, &ast.generics)
    } else {
        abort_call_site!("merge::Merge can only be derived for structs")
    }
@@ -62,11 +63,13 @@ fn impl_merge_for_struct(
    name: &syn::Ident,
    fields: &syn::Fields,
    default_strategy: FieldAttrs,
    generics: &syn::Generics,
) -> TokenStream {
    let assignments = gen_assignments(fields, default_strategy);

    let (impl_generics, orig_ty_generics, where_clause) = generics.split_for_impl();
    quote! {
        impl ::merge::Merge for #name {
        impl #impl_generics ::merge::Merge for #name #orig_ty_generics #where_clause {
            fn merge(&mut self, other: Self) {
                #assignments
            }
diff --git a/tests/derive.rs b/tests/derive.rs
index f010f25..a1cf335 100644
--- a/tests/derive.rs
+++ b/tests/derive.rs
@@ -564,3 +564,51 @@ fn test_default_strategy() {
        #[merge(strategy = Merge::merge)] N,
    );
}

#[test]
fn test_generics() {
    #[derive(Debug, Merge, PartialEq)]
    #[merge(strategy = merge::option::overwrite_none)]
    struct TupleWithGenerics<A: core::fmt::Display, B: core::fmt::Debug>(Option<A>, Option<B>);

    #[derive(Debug, Merge, PartialEq)]
    #[merge(strategy = merge::option::overwrite_none)]
    struct TupleWithWhere<A, B>(Option<A>, Option<B>)
    where
        A: core::fmt::Display,
        B: core::fmt::Debug;

    #[derive(Debug, Merge, PartialEq)]
    #[merge(strategy = merge::option::overwrite_none)]
    struct TupleWithBoth<A: core::fmt::Display, B>(Option<A>, Option<B>)
    where
        B: core::fmt::Debug;

    #[derive(Debug, Merge, PartialEq)]
    #[merge(strategy = merge::option::overwrite_none)]
    struct StructWithGenerics<A: core::fmt::Display, B: core::fmt::Debug> {
        a: Option<A>,
        b: Option<B>,
    }

    #[derive(Debug, Merge, PartialEq)]
    #[merge(strategy = merge::option::overwrite_none)]
    struct StructWithWhere<A, B>
    where
        A: core::fmt::Display,
        B: core::fmt::Debug
    {
        a: Option<A>,
        b: Option<B>,
    }

    #[derive(Debug, Merge, PartialEq)]
    #[merge(strategy = merge::option::overwrite_none)]
    struct StructWithBoth<A: core::fmt::Display, B>
    where
        B: core::fmt::Debug
    {
        a: Option<A>,
        b: Option<B>,
    }
}
-- 
2.35.1
merge-rs/patches: FAILED in 2m50s

[Implement derive(Merge) for structs with generics][0] from [René Kijewski][1]

[0]: https://lists.sr.ht/~ireas/public-inbox/patches/30794
[1]: mailto:rene.kijewski@fu-berlin.de

✗ #729648 FAILED merge-rs/patches/archlinux-msrv.yml https://builds.sr.ht/~ireas/job/729648
✗ #729649 FAILED merge-rs/patches/archlinux.yml      https://builds.sr.ht/~ireas/job/729649