将异步闭包应用于互斥体内容的组合器?

问题描述 投票:0回答:0

在我的想法中,

run
函数无法编译,因为闭包的返回类型取决于可变引用的生命周期。我无法在
F
上表达正确的界限来解决它。可能吗?

pub async fn with_mutex<'a, I, O, F, Fut>(mutex: &'a Mutex<I>, f: F) -> O
where
  // F: for<'b> (FnOnce(&'b mut I) -> impl (Future<Output = O> + 'b)),
  F: FnOnce(&mut I) -> Fut + 'a,
  Fut: Future<Output = O> + 'a,
{
  let mut guard = mutex.lock().await;
  f(&mut guard).await
}

pub async fn run() {
  let mutex = Mutex::new(5);
  let fut = with_mutex(&mutex, |value| async {
    *value += 1;
  })
  .await;
}
rust async-await closures mutex rust-tokio
© www.soinside.com 2019 - 2024. All rights reserved.