-
Rust の DI を考える –– Part 2: Rust における DI の手法の整理 - paild tech blog
-
型引数を増やさずに静的ディスパッチをできる。
-
- 知識の最小化 = 呼び出しの
.を1つにする- ex. 犬の足に歩けと命じるのではなく、犬に歩けと命じ、犬が足に命ずる
- 知識の最小化 = 呼び出しの
pub trait UsesDatabase: Send + Sync + 'static {
fn find_user(&self, id: String) -> Result<Option<User>>;
fn update(&self, user: User) -> Result<()>;
}
pub trait Database: Send + Sync + 'static {}
pub trait ProvidesDatabase: Send + Sync + 'static {
type T: UsesDatabase;
fn database(&self) -> &Self::T;
}
impl Database for AppModule {}
impl ProvidesDatabase for AppModule {
type T = Self;
fn database(&self) -> &Self::T {
self
}
}