My goal is to define async method that takes async closure in async trait, in an attempt to wrap async DB operations in a transaction.
To do so I defined a trait Transaction with a method execute that takes closure op that returns Future, using async_trait create.
I also have a struct Usecase with transaction field holding a reference to a trait object that implements the Transaction trait.
But when I tried to compile, it failed with the following error.
I followed the link in the error and knew that the trait is not object safe because execute method has type parameters.
error[E0038]: the trait `Transaction` cannot be made into an object
--> src/app/sync_usercase.rs:19:22
|
19 | transaction: Arc<dyn Transaction>,
| ^^^^^^^^^^^^^^^ `Transaction` cannot be made into an object
I don't have much experience in Rust and my knowledge is shallow so could you tell me
(1) if I am doing correct to achieve my goal?
(2) if so how can I fix the error, if not what are the correct directions?
error[E0277]: `dyn Future<Output = Result<(), anyhow::Error>>` cannot be unpinned
--> src/main.rs:31:13
|
31 | op().await;
| ----^^^^^^
| | |
| | the trait `Unpin` is not implemented for `dyn Future<Output = Result<(), anyhow::Error>>`
| | help: remove the `.await`
| this call returns `dyn Future<Output = Result<(), anyhow::Error>>`
|
= note: consider using the `pin!` macro
consider using `Box::pin` if you need to access the pinned value outside of the current scope
= note: required for `Box<dyn Future<Output = Result<(), anyhow::Error>>>` to implement `Future`
= note: required for `Box<dyn Future<Output = Result<(), anyhow::Error>>>` to implement `IntoFuture`