diff options
author | HampusM <hampus@hampusmat.com> | 2022-08-03 11:50:59 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-08-03 11:50:59 +0200 |
commit | c33cf02c9a6fffc6149fd7b59c63ad0d15d61432 (patch) | |
tree | f72edf77d7f10de85012fa869ecd01e8b161c435 /macros/src/injectable_impl.rs | |
parent | 3db388dd567fbabb6a88ee7100de65442089c3f1 (diff) |
refactor: add Cargo feature for preventing circular dependencies
Diffstat (limited to 'macros/src/injectable_impl.rs')
-rw-r--r-- | macros/src/injectable_impl.rs | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/macros/src/injectable_impl.rs b/macros/src/injectable_impl.rs index 3bd7113..29e0094 100644 --- a/macros/src/injectable_impl.rs +++ b/macros/src/injectable_impl.rs @@ -66,6 +66,29 @@ impl InjectableImpl } }; + let maybe_prevent_circular_deps = if cfg!(feature = "prevent-circular") { + quote! { + if dependency_history.contains(&self_type_name) { + dependency_history.push(self_type_name); + + return Err( + report!(ResolveError) + .attach_printable(format!( + "Detected circular dependencies. {}", + syrette::dependency_trace::create_dependency_trace( + dependency_history.as_slice(), + self_type_name + ) + )) + ); + } + + dependency_history.push(self_type_name); + } + } else { + quote! {} + }; + quote! { #original_impl @@ -83,22 +106,7 @@ impl InjectableImpl let self_type_name = std::any::type_name::<#self_type>(); - if dependency_history.contains(&self_type_name) { - dependency_history.push(self_type_name); - - return Err( - report!(ResolveError) - .attach_printable(format!( - "Detected circular dependencies. {}", - syrette::dependency_trace::create_dependency_trace( - dependency_history.as_slice(), - self_type_name - ) - )) - ); - } - - dependency_history.push(self_type_name); + #maybe_prevent_circular_deps return Ok(syrette::ptr::TransientPtr::new(Self::new( #(#get_dependencies |