I have the following code (simplified)
let store = store::InMemory::default();
let repository = Repository::from(store.clone());
let service = Service::from(repository);
I thought to reduce this to
let service = Service::new();
where
#[derive(Clone)]
pub struct Service<R>
where
R: Repository<A, B>,
{
repository: R,
}
impl<R> Service<R>
where
R: Repository<A, B>,
{
fn new() -> Self {
let store = store::InMemory::default();
let repository = Repository::from(store.clone());
let service = Self::from(repository);
service
}
}
impl<R> From<R> for Service<R>
where
R: Repository<A, B>,
{
fn from(repository: R) -> Self {
Self {
repository,
}
}
}
It appears the source of the error is using Self:from(..)
inside new()
.
I've obviously forgotten something fundamental/simple, because the compiler error suggests this:
error[E0277]: the trait bound `Service<R>: std::convert::From<Repository<A, B, InMemory<String, C>>>` is not satisfied
--> src/application.rs:43:27
|
43 | let application = Self::from(repository);
| ^^^^^^^^^^ the trait `std::convert::From<EventSourced<A, B, InMemory<String, C>>>` is not implemented for `Service<R>`
|
help: consider extending the `where` bound, but there might be an alternative better way to express this requirement
|
37 | R: aggregate::Repository<A, B>, Service<R>: std::convert::From<EventSourced<A, B, InMemory<String, C>>>
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For more information about this error, try `rustc --explain E0277`.
If I understand the suggestion correctly, it is to add a self referring trait bound (which fails):
impl<R> From<R> for Service<R>
where
R: Repository<A, B>,
Service<R>: std::convert::From<EventSourced<A, B, InMemory<String, C>>>
{
fn from(repository: R) -> Self {
Self {
repository,
}
}
}
So I'm wondering what have I missed and what are the alternative better ways?
The E02077 did not really help me so I'm likely over thinking what is wrong.
Appreciate any tips.