In Golang I can define an interface like this:
type DBRepo interface {
PlayerByID(id uint64) (*domain.Player, error)
TeamByID(id uint64) (*domain.Team, error)
// many others
and I can implement them like this using different files:
// file: real_db.go
type RealDB struct {
db *realDB
}
// file: player.go
func (r RealDB) PlayerByID(id uint64) (*domain.Player, error) {
return r.db... // get from DB
}
// file: team.go
func (r RealDB) TeamByID(id uint64) (*domain.Team, error) {
return r.db... // get from DB
}
// many others (files and methods)
I cannot undestand how to do the same in Rust:
#[async_trait::async_trait]
pub trait DBRepo: Send + Sync {
async fn player_by_id(&self, id: i64) -> Result<()>;
async fn team_by_id(&self, id: i64) -> Result<()>;
}
but if I write the below code in different files (and different mods too):
// file: player.rs
#[async_trait::async_trait]
impl DBRepo for Repo {
async fn player_by_id(&self, id: i64) -> Result<()> {
Ok(()) // get from DB
}
}
// file: team.rs
#[async_trait::async_trait]
impl DBRepo for Repo {
async fn team_by_id(&self, id: i64) -> Result<()> {
Ok(()) // get from DB
}
}
I get from the compiler:
error[E0119]: conflicting implementations of trait `DBRepo` for type `Repo`
--> src\team.rs:22:1
|
22 | impl DBRepo for Repo {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Repo`
|
::: src\player.rs:22:1
|
22 | impl DBRepo for Repo {
| ----------------------------------- first implementation here
For more information about this error, try `rustc --explain E0119`.
How can I fix this?
I need to use all the methods on trait DBRepo, I cannot split it in many traits.