From a615ee13ba05733d31f7d7b47700acff43906936 Mon Sep 17 00:00:00 2001 From: Luca Fulchir Date: Wed, 4 Dec 2024 13:40:44 +0100 Subject: [PATCH] pkg: add trait deps Signed-off-by: Luca Fulchir --- bok-macro/src/pkgs.rs | 35 ++++++++++++++++++++++----------- bok-utils/src/repos/pkgs/two.rs | 6 ++---- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/bok-macro/src/pkgs.rs b/bok-macro/src/pkgs.rs index 617b510..13cbebd 100644 --- a/bok-macro/src/pkgs.rs +++ b/bok-macro/src/pkgs.rs @@ -306,9 +306,20 @@ pub(crate) fn package_impl( // // only add the generic parameter and nothing else // - let t_id = &ast.self_ty; + let ::syn::Type::Path(t_id) = &ast.self_ty.as_ref() else { + return ::syn::Error::new( + ast.span(), + "package_impl: Type is not Path", + ) + .to_compile_error() + .into(); + }; + let trait_deps = ::quote::format_ident!( + "BokDeps{}", + t_id.path.segments.last().unwrap().ident + ); let g: ::syn::ItemImpl = ::syn::parse_quote! { - impl trait_name for #t_id where R: ::bok::Repository {} + impl trait_name for #t_id where R: ::bok::Repository + #trait_deps {} }; ast.generics = g.generics; ast.self_ty = g.self_ty; @@ -586,11 +597,11 @@ pub(crate) fn package_impl_base_add( } } - let pkg_type = ast.self_ty.clone(); + let trait_deps = ::quote::format_ident!("BokDeps{}", name_pkg); let generic_struct: ::syn::ItemImpl = ::syn::parse_quote! { - impl test for #pkg_type + impl test for #name_pkg where - R: ::bok::Repository + 'static + R: ::bok::Repository + 'static + #trait_deps, {} }; @@ -856,10 +867,12 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream { .expect("can't write package struct into string"); } + let trait_deps = ::quote::format_ident!("BokDeps{}", name); + let expanded = quote! { impl ::core::fmt::Display for #name where - R: ::bok::Repository, + R: ::bok::Repository + #trait_deps, { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) ->::core::fmt::Result @@ -869,7 +882,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream { } impl ::bok::PkgCode for #name where - R: ::bok::Repository +'static, + R: ::bok::Repository + 'static + #trait_deps, { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) ->::core::fmt::Result @@ -932,7 +945,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream { } impl #name where - R: ::bok::Repository + 'static + ::core::default::Default, + R: ::bok::Repository + 'static + #trait_deps + ::core::default::Default, { pub fn as_pkg(&self) -> &dyn ::bok::Pkg { self @@ -953,7 +966,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream { #[derive(::std::default::Default, ::std::fmt::Debug)] pub struct #name_builder where - R: ::bok::Repository + 'static, + R: ::bok::Repository + 'static + #trait_deps, { _bok_repo: ::std::marker::PhantomData, #(#non_opt_fields: ::std::option::Option<#non_opt_types>,)* @@ -961,7 +974,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream { } impl ::bok::PkgBuilder for #name_builder2 where - R: ::bok::Repository + 'static, + R: ::bok::Repository + 'static + #trait_deps, { fn name(&self) -> ::bok::PkgName { use ::bok::Pkg; @@ -1000,7 +1013,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream { } impl #name_builder3 where - R: ::bok::Repository +'static, + R: ::bok::Repository +'static + #trait_deps, { pub fn as_any(&self) -> &dyn ::std::any::Any { self diff --git a/bok-utils/src/repos/pkgs/two.rs b/bok-utils/src/repos/pkgs/two.rs index a2a15a7..188c223 100644 --- a/bok-utils/src/repos/pkgs/two.rs +++ b/bok-utils/src/repos/pkgs/two.rs @@ -22,10 +22,8 @@ pub struct Two { pub my_attr2: u32, } -impl ::std::default::Default for Two -where - R: ::bok::Repository, -{ +#[::bok::package_impl] +impl ::std::default::Default for Two { fn default() -> Self { Two { _bok_base: ::std::marker::PhantomData::default(),