//! Associated types can be part of generic bounds. //! Based on `generic_submodules.rs` use shaku::{module, Component, HasComponent, Interface}; use std::sync::Arc; trait DbPool: Interface { fn get_connection(&self) -> &C; } trait Connection: Interface { type Database; } #[derive(Debug, Default)] struct DbConnection; struct MyDatabase; impl Connection for DbConnection { type Database = MyDatabase; } #[derive(Component)] #[shaku(interface = DbPool)] struct DbPoolImpl + Default> { #[shaku(default)] connection: C, } impl + Default> DbPool for DbPoolImpl { fn get_connection(&self) -> &C { &self.connection } } module! { MyModule + Default> { components = [DbPoolImpl], providers = [] } } module! { RootModule + Default> { components = [], providers = [], use MyModule { components = [DbPool], providers = [] } } } #[test] fn generic_submodules() { let my_module = Arc::new(MyModule::builder().build()); let root_module = RootModule::builder(my_module).build(); let db_pool: &dyn DbPool = root_module.resolve_ref(); let connection = db_pool.get_connection(); assert_eq!(format!("{:?}", connection), "DbConnection") }