From a98b1fb6537da0d14f2ff7f9ff9df38d4ba9625c Mon Sep 17 00:00:00 2001 From: Luca Fulchir Date: Sun, 10 Nov 2024 20:59:48 +0100 Subject: [PATCH] Pkg dyn base Signed-off-by: Luca Fulchir --- bok-macro/src/lib.rs | 14 +++++++++++++- bok/src/lib.rs | 15 ++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/bok-macro/src/lib.rs b/bok-macro/src/lib.rs index b16bfa4..3e9bd00 100644 --- a/bok-macro/src/lib.rs +++ b/bok-macro/src/lib.rs @@ -209,10 +209,15 @@ pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream { let mut ast = parse_macro_input!(input as ::syn::ItemImpl); let base: ::syn::ImplItem = ::syn::parse_quote! { - fn base(&self) -> ::core::option::Option<&impl ::bok::Pkg> { + fn base(&self) -> ::core::option::Option<&dyn ::bok::Pkg> { Some(&self.base) } }; + let base_mut: ::syn::ImplItem = ::syn::parse_quote! { + fn base_mut(&mut self) -> ::core::option::Option<&mut dyn ::bok::Pkg> { + Some(&mut self.base) + } + }; for mut impl_item in ast.items.iter_mut() { if let ::syn::ImplItem::Fn(ref mut fn_impl) = &mut impl_item { @@ -227,6 +232,7 @@ pub fn impl_package(_attrs: TokenStream, input: TokenStream) -> TokenStream { } ast.items.push(base); + ast.items.push(base_mut); quote! { #ast @@ -491,6 +497,12 @@ pub fn derive_package(input: TokenStream) -> TokenStream { pub fn as_any_mut(&mut self) -> &mut dyn ::std::any::Any { self } + pub fn as_pkg(&self) -> &dyn ::bok::Pkg { + self + } + pub fn as_pkg_mut(&mut self) -> &mut dyn ::bok::Pkg { + self + } pub fn builder() -> #name_builder { #name_builder::default() } diff --git a/bok/src/lib.rs b/bok/src/lib.rs index f89799c..215966e 100644 --- a/bok/src/lib.rs +++ b/bok/src/lib.rs @@ -89,12 +89,10 @@ impl Repository for RepositoryEmpty {} /// Implement common package operations pub trait Pkg: - ::std::default::Default - + ::core::fmt::Debug - + ::core::fmt::Display - + ::std::any::Any + ::core::fmt::Debug + ::core::fmt::Display + ::std::any::Any { - fn base(&self) -> ::core::option::Option<&impl Pkg>; + fn base(&self) -> ::core::option::Option<&dyn Pkg>; + fn base_mut(&mut self) -> ::core::option::Option<&mut dyn Pkg>; fn prepare(&self) -> ::core::result::Result<(), ()> { self.base().unwrap().prepare() } @@ -162,8 +160,11 @@ impl ::core::fmt::Display for PkgEmpty { } impl Pkg for PkgEmpty { - fn base(&self) -> Option<&impl Pkg> { - None::.as_ref() + fn base(&self) -> Option<&dyn Pkg> { + None + } + fn base_mut(&mut self) -> Option<&mut dyn Pkg> { + None } fn prepare(&self) -> ::core::result::Result<(), ()> { ::core::result::Result::Ok(())