Hello,
I am working on a proof-of-concept microservice using Rust and hex architecture. I am fairly new to Rust, so my code may not be idiomatic. I have a compilation error, 'lifetime may not live long enough', dealing with the lifetime of a trait reference. I have been going around in circles tracing, reviewing, tweaking, but to no avail. I suspect that I need to change the code to use 'static with the dyn traits, but when I did it, the code wouldn't build. I'd really appreciate any suggestions about how to resolve my issue. I am sure that I am missing a kernel of knowledge as a newcomer...
Here is a link to a minimal working version of this app at gitlab:
Thank you for your time and interest!
Mike
ERROR MESSAGE:
error: lifetime may not live long enough
--> src/main.rs:34:63
|
29 | fn find_one_project(database_connection: &InMemoryDatabaseConnection) {
| - let's call the lifetime of this reference `'1`
...
34 | let find_one_project_service = FindOneProjectService::new(find_one_project_persistence_adapter);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cast requires that `'1` must outlive `'static`
MAIN APPLICATION:
/////////////////////////////////////////////////////////////////////////
// main.rs
/////////////////////////////////////////////////////////////////////////
fn main() -> std::io::Result<()> {
let database_connection = InMemoryDatabaseConnection::new().unwrap();
find_one_project(&database_connection);
Ok(())
}
fn find_one_project(database_connection: &InMemoryDatabaseConnection) {
let find_one_project_repository = FindOneProjectRepository::new_with_db(database_connection).unwrap();
let find_one_project_persistence_adapter = Box::new(FindOneProjectPersistenceAdapter::new(find_one_project_repository));
let find_one_project_service = FindOneProjectService::new(find_one_project_persistence_adapter);
...
Ok(())
}
/////////////////////////////////////////////////////////////////////////
// find_one_project_service.rs
/////////////////////////////////////////////////////////////////////////
pub struct FindOneProjectService<'a> {
query_port: Box<(dyn FindOneProjectQueryPort<'a>)>
}
impl <'a>FindOneProjectService<'a> {
pub fn new(query_port: Box<(dyn FindOneProjectQueryPort<'a>)>) -> Self {
Self { query_port }
}
}
impl <'a>FindOneProjectUseCase<'a> for Box<FindOneProjectService<'a>> {
fn find_one(&self, query: FindOneProjectQuery) -> Result<Project, BoxError> {
self.query_port.find_one(query.name)
}
}