Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/agent-client-protocol/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,26 @@ pub async fn both<E>(
/// Returns the result of `foreground`. If `background` errors before
/// `foreground` completes, the error is propagated. If `background`
/// completes with `Ok(())`, we continue waiting for `foreground`.
pub async fn run_until<T, E>(
pub fn run_until<T, E>(
background: impl Future<Output = Result<(), E>>,
foreground: impl Future<Output = Result<T, E>>,
) -> Result<T, E> {
) -> impl Future<Output = Result<T, E>> {
use futures::future::{Either, select};
use std::pin::pin;

match select(pin!(background), pin!(foreground)).await {
Either::Left((bg_result, fg_future)) => {
// Background finished first
bg_result?; // propagate error, or if Ok(()), keep waiting
fg_future.await
}
Either::Right((fg_result, _bg_future)) => {
// Foreground finished first, drop background
fg_result
Box::pin(async move {
match select(pin!(background), pin!(foreground)).await {
Either::Left((bg_result, fg_future)) => {
// Background finished first
bg_result?; // propagate error, or if Ok(()), keep waiting
fg_future.await
}
Either::Right((fg_result, _bg_future)) => {
// Foreground finished first, drop background
fg_result
}
}
}
})
}

/// Process items from a stream concurrently.
Expand Down