//! Interface for structs that can be injected into or be injected to. use std::fmt::Debug; use std::rc::Rc; use std::sync::Arc; use crate::errors::injectable::InjectableError; use crate::ptr::TransientPtr; use crate::ptr_buffer::{PtrBuffer, SmartPtr}; use crate::util::use_double; use_double!(crate::dependency_history::DependencyHistory); /// Interface for structs that can be injected into or be injected to. pub trait Injectable: 'static { /// Resolves the dependencies of the injectable. /// /// # Errors /// Will return `Err` if resolving the dependencies fails. fn resolve( di_container: &DIContainerT, dependency_history: DependencyHistory, ) -> Result, InjectableError> where Self: Sized; /// A. fn into_ptr_buffer_box(self: Box) -> PtrBuffer; /// A. fn into_ptr_buffer_rc(self: Rc) -> PtrBuffer; /// A. fn into_ptr_buffer_arc(self: Arc) -> PtrBuffer; } impl Debug for dyn Injectable { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("{}") } } impl Injectable for T where T: Default + 'static, { fn resolve( _: &DIContainerT, _: DependencyHistory, ) -> Result, InjectableError> { Ok(TransientPtr::new(Self::default())) } fn into_ptr_buffer_box(self: Box) -> PtrBuffer { PtrBuffer::new_from(SmartPtr::Box(self)) } fn into_ptr_buffer_rc(self: Rc) -> PtrBuffer { PtrBuffer::new_from(SmartPtr::Rc(self)) } fn into_ptr_buffer_arc(self: Arc) -> PtrBuffer { PtrBuffer::new_from(SmartPtr::Arc(self)) } }