diff --git a/despatma-dependency-container/src/output.rs b/despatma-dependency-container/src/output.rs index 77abd43..6148302 100644 --- a/despatma-dependency-container/src/output.rs +++ b/despatma-dependency-container/src/output.rs @@ -193,7 +193,6 @@ impl From for Dependency { block, is_async, is_boxed: _, - has_explicit_lifetime: _, lifetime, ty, field_ty: _, @@ -408,7 +407,6 @@ mod tests { block: parse_quote!({ Config::new().await }), is_async: true, is_boxed: false, - has_explicit_lifetime: false, lifetime: Lifetime::Singleton(Span::call_site()), ty: parse_quote! { Config }, field_ty: parse_quote! { Config }, @@ -422,7 +420,6 @@ mod tests { block: parse_quote!({ Sqlite::new(config.conn_str) }), is_async: true, is_boxed: false, - has_explicit_lifetime: false, lifetime: Lifetime::Singleton(Span::call_site()), ty: parse_quote! { Sqlite }, field_ty: parse_quote! { Sqlite }, @@ -445,7 +442,6 @@ mod tests { block: parse_quote!({ Service::new(db) }), is_async: true, is_boxed: false, - has_explicit_lifetime: false, lifetime: Lifetime::Transient(None), ty: parse_quote! { Service }, field_ty: parse_quote! { Service }, @@ -531,7 +527,6 @@ mod tests { block: parse_quote!({ Box::new(Sqlite::new()) }), is_async: false, is_boxed: true, - has_explicit_lifetime: false, lifetime: Lifetime::Scoped(Span::call_site()), ty: parse_quote! { std::boxed::Box }, field_ty: parse_quote! { std::boxed::Box }, diff --git a/despatma-dependency-container/src/processing/mod.rs b/despatma-dependency-container/src/processing/mod.rs index e03679e..2b1d7d4 100644 --- a/despatma-dependency-container/src/processing/mod.rs +++ b/despatma-dependency-container/src/processing/mod.rs @@ -8,8 +8,7 @@ use crate::input; use self::visitor::{ AddWildcardLifetime, ErrorVisitorMut, ExtractAsync, ExtractBoxType, ExtractLifetime, ImplTraitButRegisteredConcrete, ImplTraitFields, LinkDependencies, OwningManagedDependency, - ReplaceImplGenericsWithConcrete, SetHasExplicitLifetime, UnsupportedRegisteredTypes, - VisitableMut, WrapBoxType, + ReplaceImplGenericsWithConcrete, UnsupportedRegisteredTypes, VisitableMut, WrapBoxType, }; mod visitor; @@ -29,13 +28,18 @@ pub struct Dependency { pub(crate) block: Block, pub(crate) is_async: bool, pub(crate) is_boxed: bool, - pub(crate) has_explicit_lifetime: bool, pub(crate) lifetime: Lifetime, pub(crate) ty: Type, pub(crate) field_ty: Type, pub(crate) dependencies: Vec, } +impl Dependency { + pub fn box_needs_explicit_lifetime(&self) -> bool { + self.is_boxed && self.lifetime.is_managed() + } +} + #[derive(Clone)] #[cfg_attr(test, derive(Eq, PartialEq, Debug))] pub struct ChildDependency { @@ -115,7 +119,6 @@ impl From for Dependency { block, is_async: false, is_boxed: false, - has_explicit_lifetime: false, lifetime: Lifetime::Transient(None), field_ty: ty.clone(), ty, @@ -148,14 +151,11 @@ impl Container { self.process_visitor::(); self.process_visitor::(); - // Needs lifetimes to be extracted and boxes to be extracted - self.process_visitor::(); - // Needs dependencies to be linked and lifetimes to be extracted // But boxes should not be wrapped yet self.process_visitor::(); - // Needs has_explicit_lifetime to be set + // Needs lifetimes and boxes to be extracted first self.process_visitor::(); } diff --git a/despatma-dependency-container/src/processing/visitor/mod.rs b/despatma-dependency-container/src/processing/visitor/mod.rs index 6125fb1..7fffc91 100644 --- a/despatma-dependency-container/src/processing/visitor/mod.rs +++ b/despatma-dependency-container/src/processing/visitor/mod.rs @@ -11,7 +11,6 @@ pub use impl_trait_fields::ImplTraitFields; pub use link_dependencies::LinkDependencies; pub use owning_managed_dependency::OwningManagedDependency; pub use replace_impl_generics_with_concrete::ReplaceImplGenericsWithConcrete; -pub use set_has_explicit_lifetime::SetHasExplicitLifetime; pub use unsupported_registered_types::UnsupportedRegisteredTypes; pub use wrap_box_type::WrapBoxType; @@ -24,7 +23,6 @@ mod impl_trait_fields; mod link_dependencies; mod owning_managed_dependency; mod replace_impl_generics_with_concrete; -mod set_has_explicit_lifetime; mod unsupported_registered_types; mod wrap_box_type; diff --git a/despatma-dependency-container/src/processing/visitor/set_has_explicit_lifetime.rs b/despatma-dependency-container/src/processing/visitor/set_has_explicit_lifetime.rs deleted file mode 100644 index d6f5c47..0000000 --- a/despatma-dependency-container/src/processing/visitor/set_has_explicit_lifetime.rs +++ /dev/null @@ -1,72 +0,0 @@ -use crate::processing::Dependency; - -use super::{ErrorVisitorMut, VisitorMut}; - -/// Correctly identifies dependencies which needs an explicit lifetime to be added to their outputs and tree. -/// -/// Needs to be called after any boxes are extracted. -/// And after lifetimes are extracted. -pub struct SetHasExplicitLifetime; - -impl VisitorMut for SetHasExplicitLifetime { - fn visit_dependency_mut(&mut self, dependency: &mut Dependency) { - if dependency.is_boxed && dependency.lifetime.is_managed() { - dependency.has_explicit_lifetime = true; - } - } -} - -impl ErrorVisitorMut for SetHasExplicitLifetime { - fn new() -> Self { - Self - } -} - -#[cfg(test)] -mod tests { - use syn::parse_quote; - - use crate::{ - input, - processing::{ - self, - visitor::{ExtractBoxType, ExtractLifetime, VisitableMut}, - }, - }; - - use super::*; - - #[test] - fn set_has_explicit_lifetime() { - let mut container: processing::Container = input::Container::from_item_impl(parse_quote!( - impl Container { - #[Singleton] - fn dal(&self) -> Box { - Box::new(Postgres) - } - - fn datetime(&self) -> Box { - Box::new(Utc::now()) - } - - fn service(&self, dal: impl DAL, datetime: Utc) -> Service { - Service::new(dal, datetime) - } - } - )) - .into(); - - container.apply_mut(&mut ExtractBoxType); - container.apply_mut(&mut ExtractLifetime); - - assert!(!container.dependencies[0].borrow().has_explicit_lifetime); - assert!(!container.dependencies[1].borrow().has_explicit_lifetime); - assert!(!container.dependencies[2].borrow().has_explicit_lifetime); - - container.apply_mut(&mut SetHasExplicitLifetime); - - assert!(container.dependencies[0].borrow().has_explicit_lifetime); - assert!(!container.dependencies[1].borrow().has_explicit_lifetime); - assert!(!container.dependencies[2].borrow().has_explicit_lifetime); - } -} diff --git a/despatma-dependency-container/src/processing/visitor/wrap_box_type.rs b/despatma-dependency-container/src/processing/visitor/wrap_box_type.rs index 6a67e07..f9955a7 100644 --- a/despatma-dependency-container/src/processing/visitor/wrap_box_type.rs +++ b/despatma-dependency-container/src/processing/visitor/wrap_box_type.rs @@ -6,7 +6,7 @@ use super::{ErrorVisitorMut, VisitorMut}; /// Correctly restores the boxes that were removed from any types /// -/// Needs to be called after `has_explicit_lifetime` is set. +/// Needs to happen after boxes and lifetimes are extracted pub struct WrapBoxType; impl VisitorMut for WrapBoxType { @@ -14,10 +14,11 @@ impl VisitorMut for WrapBoxType { if dependency.is_boxed { let ty = &dependency.ty; - if dependency.has_explicit_lifetime { + if dependency.box_needs_explicit_lifetime() { dependency.field_ty = parse_quote!(std::boxed::Box<#ty + 'a>); dependency.ty = parse_quote!(std::boxed::Box<#ty + 'a>); } else { + dependency.field_ty = parse_quote!(std::boxed::Box<#ty>); dependency.ty = parse_quote!(std::boxed::Box<#ty>); } } @@ -39,7 +40,7 @@ mod tests { input, processing::{ self, - visitor::{ExtractBoxType, ExtractLifetime, SetHasExplicitLifetime, VisitableMut}, + visitor::{ExtractBoxType, ExtractLifetime, VisitableMut}, }, }; @@ -67,21 +68,17 @@ mod tests { container.apply_mut(&mut ExtractBoxType); container.apply_mut(&mut ExtractLifetime); - container.apply_mut(&mut SetHasExplicitLifetime); - assert!(container.dependencies[0].borrow().has_explicit_lifetime); assert_eq!(container.dependencies[0].borrow().ty, parse_quote!(dyn DAL)); assert_eq!( container.dependencies[0].borrow().field_ty, parse_quote!(Box) ); - assert!(!container.dependencies[1].borrow().has_explicit_lifetime); assert_eq!(container.dependencies[1].borrow().ty, parse_quote!(Utc)); assert_eq!( container.dependencies[1].borrow().field_ty, parse_quote!(Box) ); - assert!(!container.dependencies[2].borrow().has_explicit_lifetime); assert_eq!( container.dependencies[2].borrow().ty, parse_quote!(Service), @@ -107,7 +104,7 @@ mod tests { ); assert_eq!( container.dependencies[1].borrow().field_ty, - parse_quote!(Box) + parse_quote!(std::boxed::Box) ); assert_eq!( container.dependencies[2].borrow().ty,