lapin icon indicating copy to clipboard operation
lapin copied to clipboard

Consumer delegate static lifetime

Open r2dliu opened this issue 2 years ago • 0 comments

Relatively new to rust so possible I'm just being dumb.

What would be a good workaround for accessing something in the closure of a consumer delegate without violating the static lifetime requirement?

In my use case, I have a connection pool for a database that I want to be able to access.

simplified pseudocode:

main() {
  let pool = create_db_pool();
  consumer.set_delegate(|delivery: DeliveryResult| async {
         do_db_things(&pool, delivery) // compiler is unhappy because of static lifetime
  }
}

errors:

`pool` is borrowed here
closure may outlive the current function, but it borrows `pool`, which is owned by the current function
may outlive borrowed value `pool`

closure obviously does not outlive the pool since the program will end there but not sure how to explicitly tell rust that it's fine to be using it

r2dliu avatar Mar 04 '22 22:03 r2dliu