Skip to content

Commit

Permalink
refactor(di): simplify by removing has_explicit_lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
chesedo committed Sep 24, 2024
1 parent 85d24d9 commit 0294b4d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 95 deletions.
5 changes: 0 additions & 5 deletions despatma-dependency-container/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ impl From<processing::Dependency> for Dependency {
block,
is_async,
is_boxed: _,
has_explicit_lifetime: _,
lifetime,
ty,
field_ty: _,
Expand Down Expand Up @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand Down Expand Up @@ -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<dyn DB + 'a> },
field_ty: parse_quote! { std::boxed::Box<dyn DB + 'a> },
Expand Down
16 changes: 8 additions & 8 deletions despatma-dependency-container/src/processing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ChildDependency>,
}

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 {
Expand Down Expand Up @@ -115,7 +119,6 @@ impl From<ImplItemFn> for Dependency {
block,
is_async: false,
is_boxed: false,
has_explicit_lifetime: false,
lifetime: Lifetime::Transient(None),
field_ty: ty.clone(),
ty,
Expand Down Expand Up @@ -148,14 +151,11 @@ impl Container {
self.process_visitor::<ExtractBoxType>();
self.process_visitor::<UnsupportedRegisteredTypes>();

// Needs lifetimes to be extracted and boxes to be extracted
self.process_visitor::<SetHasExplicitLifetime>();

// Needs dependencies to be linked and lifetimes to be extracted
// But boxes should not be wrapped yet
self.process_visitor::<AddWildcardLifetime>();

// Needs has_explicit_lifetime to be set
// Needs lifetimes and boxes to be extracted first
self.process_visitor::<WrapBoxType>();
}

Expand Down
2 changes: 0 additions & 2 deletions despatma-dependency-container/src/processing/visitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ 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 {
fn visit_dependency_mut(&mut self, dependency: &mut Dependency) {
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>);
}
}
Expand All @@ -39,7 +40,7 @@ mod tests {
input,
processing::{
self,
visitor::{ExtractBoxType, ExtractLifetime, SetHasExplicitLifetime, VisitableMut},
visitor::{ExtractBoxType, ExtractLifetime, VisitableMut},
},
};

Expand Down Expand Up @@ -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<dyn DAL>)
);
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<Utc>)
);
assert!(!container.dependencies[2].borrow().has_explicit_lifetime);
assert_eq!(
container.dependencies[2].borrow().ty,
parse_quote!(Service<impl DAL>),
Expand All @@ -107,7 +104,7 @@ mod tests {
);
assert_eq!(
container.dependencies[1].borrow().field_ty,
parse_quote!(Box<Utc>)
parse_quote!(std::boxed::Box<Utc>)
);
assert_eq!(
container.dependencies[2].borrow().ty,
Expand Down

0 comments on commit 0294b4d

Please sign in to comment.