pkg: add trait deps

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2024-12-04 13:40:44 +01:00
parent ae44556dd7
commit a615ee13ba
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
2 changed files with 26 additions and 15 deletions

View File

@ -306,9 +306,20 @@ pub(crate) fn package_impl(
// //
// only add the generic parameter and nothing else // 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! { let g: ::syn::ItemImpl = ::syn::parse_quote! {
impl<R> trait_name for #t_id<R> where R: ::bok::Repository {} impl<R> trait_name for #t_id<R> where R: ::bok::Repository + #trait_deps {}
}; };
ast.generics = g.generics; ast.generics = g.generics;
ast.self_ty = g.self_ty; 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! { let generic_struct: ::syn::ItemImpl = ::syn::parse_quote! {
impl<R> test for #pkg_type <R> impl<R> test for #name_pkg <R>
where 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"); .expect("can't write package struct into string");
} }
let trait_deps = ::quote::format_ident!("BokDeps{}", name);
let expanded = quote! { let expanded = quote! {
impl<R> ::core::fmt::Display for #name<R> impl<R> ::core::fmt::Display for #name<R>
where where
R: ::bok::Repository, R: ::bok::Repository + #trait_deps,
{ {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>)
->::core::fmt::Result ->::core::fmt::Result
@ -869,7 +882,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream {
} }
impl<R> ::bok::PkgCode for #name<R> impl<R> ::bok::PkgCode for #name<R>
where where
R: ::bok::Repository +'static, R: ::bok::Repository + 'static + #trait_deps,
{ {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>)
->::core::fmt::Result ->::core::fmt::Result
@ -932,7 +945,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream {
} }
impl<R> #name<R> impl<R> #name<R>
where 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 { pub fn as_pkg(&self) -> &dyn ::bok::Pkg {
self self
@ -953,7 +966,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream {
#[derive(::std::default::Default, ::std::fmt::Debug)] #[derive(::std::default::Default, ::std::fmt::Debug)]
pub struct #name_builder<R> pub struct #name_builder<R>
where where
R: ::bok::Repository + 'static, R: ::bok::Repository + 'static + #trait_deps,
{ {
_bok_repo: ::std::marker::PhantomData<R>, _bok_repo: ::std::marker::PhantomData<R>,
#(#non_opt_fields: ::std::option::Option<#non_opt_types>,)* #(#non_opt_fields: ::std::option::Option<#non_opt_types>,)*
@ -961,7 +974,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream {
} }
impl<R> ::bok::PkgBuilder for #name_builder2<R> impl<R> ::bok::PkgBuilder for #name_builder2<R>
where where
R: ::bok::Repository + 'static, R: ::bok::Repository + 'static + #trait_deps,
{ {
fn name(&self) -> ::bok::PkgName { fn name(&self) -> ::bok::PkgName {
use ::bok::Pkg; use ::bok::Pkg;
@ -1000,7 +1013,7 @@ pub(crate) fn derive_package(input: TokenStream) -> TokenStream {
} }
impl<R> #name_builder3<R> impl<R> #name_builder3<R>
where where
R: ::bok::Repository +'static, R: ::bok::Repository +'static + #trait_deps,
{ {
pub fn as_any(&self) -> &dyn ::std::any::Any { pub fn as_any(&self) -> &dyn ::std::any::Any {
self self

View File

@ -22,10 +22,8 @@ pub struct Two {
pub my_attr2: u32, pub my_attr2: u32,
} }
impl<R> ::std::default::Default for Two<R> #[::bok::package_impl]
where impl ::std::default::Default for Two {
R: ::bok::Repository,
{
fn default() -> Self { fn default() -> Self {
Two { Two {
_bok_base: ::std::marker::PhantomData::default(), _bok_base: ::std::marker::PhantomData::default(),