Hi Everyone
I have a Repository supertrait and a few subtrait for repositories for different domains. There are concrete implementations as struct in several database types,like Inmemory and MySQL. I want to have a single struct UnitOfWork which can store any concrete implementation, as long as it implements the Repository trait and the subtrait can be specified as generic type.
I have implemented it with:
trait Repository {}
trait InvoiceRepository: Repository {}
struct MysqlInvoiceRepository {}
impl InvoiceRepository for MysqlInvoiceRepository {}
impl Repository for MysqlInvoiceRepository {}
struct InmemoryInvoiceRepository {}
impl InvoiceRepository for InmemoryInvoiceRepository {}
impl Repository for InmemoryInvoiceRepository {}
trait OrderRepository: Repository {}
struct MysqlOrderRepository {}
impl OrderRepository for MysqlOrderRepository {}
impl Repository for MysqlOrderRepository {}
struct InmemoryOrderRepository {}
impl OrderRepository for InmemoryOrderRepository {}
impl Repository for InmemoryOrderRepository {}
struct UoW<T: Repository> {
repository: Box<T>,
}
enum RepoType {
MySQL,
InMemory,
}
fn create_uow<T: InvoiceRepository + 'static>(
repo_type: RepoType,
) -> UoW<Box<dyn InvoiceRepository>> {
match repo_type {
RepoType::MySQL => UoW {
repository: Box::new(MysqlInvoiceRepository {}),
},
RepoType::InMemory => Box::new(InmemoryInvoiceRepository {}),
}
}
But I can not get rid of the error
error[E0277]: the trait bound `std::boxed::Box<(dyn uow::InvoiceRepository + 'static)>: uow::Repository` is not satisfied
--> crates/ddd/src/uow.rs:38:6
|
38 | ) -> UoW<Box<dyn InvoiceRepository>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `uow::Repository` is not implemented for `std::boxed::Box<(dyn uow::InvoiceRepository + 'static)>`
|
= help: the following other types implement trait `uow::Repository`:
uow::InmemoryInvoiceRepository
uow::InmemoryOrderRepository
uow::MysqlInvoiceRepository
uow::MysqlOrderRepository
note: required by a bound in `uow::UoW`
--> crates/ddd/src/uow.rs:27:15
|
27 | struct UoW<T: Repository> {
| ^^^^^^^^^^ required by this bound in `UoW`
Can you give me some pointers for a solution?