我想编写处理与事务提交相关的逻辑的函数transactional
fn get_client() -> Client {
return Client{};
}
struct Client {}
impl Client {
pub async fn commit(&mut self) -> Result<(), Error> {
return Ok(());
}
pub async fn find_and_update(&mut self) -> Vec<u64> {
return vec![];
}
}
pub async fn transactional<F, Fut, R>(action: F) -> Result<R, Error>
where
F: Fn(&mut Client) -> Fut,
Fut: Future<Output = R>
{
let mut client = get_client();
loop {
let action_result = action(&mut client).await;
if let Err(err) = client.commit().await {
continue;
}
return Ok(action_result);
}
}
pub async fn make_request() -> Vec<u64> {
return transactional(
async move |session| session.find_and_update().await
).await.unwrap();
}
#[tokio::main]
async fn main() -> Result<(), io::Error>{
let r = make_request().await;
return Ok(())
}
但我得到了以下错误
| async move |session| session.find_and_update().await
| ^^^^^^^^^^^^--------
| | | |
| | | return type of closure `impl futures::Future<Output = Vec<u64>>` contains a lifetime `'2`
| | has type `&'1 mut Client`
| returning this value requires that `'1` must outlive `'2`
是否可以指定&;客户端的生命周期 比Future长,而两者的生命周期 都比循环迭代短?
有没有可能在不使用指针的情况下修复这个问题?
cargo --version
cargo 1.64.0-nightly (a5e08c470 2022-06-23)