use auto_impl::auto_impl; #[auto_impl(Box, &)] trait DAL {} struct PostgresDAL; impl DAL for PostgresDAL {} struct SQLiteDAL; impl DAL for SQLiteDAL {} struct Service { dal: D, } impl Service { fn new(dal: D) -> Self { println!("Box dyn Trait singleton service started"); Self { dal } } } #[despatma_dependency_container::dependency_container] impl DependencyContainer { #[Singleton] fn dal(&self) -> Box { if true { Box::new(PostgresDAL) } else { Box::new(SQLiteDAL) } } fn service(&self, dal: &Box) -> Service<&Box> { Service::new(dal) } } fn main() { let container = DependencyContainer::new(); let _service = container.service(); }