Download codex-rs/async-utils/src/lib.rs from SaylorTwift/codex: direct link, hf CLI and curl.
- Browser
- Download file 2.15 kB
-
https://huggingface.co/SaylorTwift/codex/resolve/main/codex-rs/async-utils/src/lib.rs
- Command line
-
hf download hf://SaylorTwift/codex/codex-rs/async-utils/src/lib.rs
-
curl -L -o lib.rs https://huggingface.co/SaylorTwift/codex/resolve/main/codex-rs/async-utils/src/lib.rs
2.15 kB
| use std::future::Future; | |
| use tokio_util::sync::CancellationToken; | |
| /// Stack budget for threads that poll Codex async work. | |
| pub const THREAD_STACK_SIZE_BYTES: usize = 16 * 1024 * 1024; | |
| pub enum CancelErr { | |
| Cancelled, | |
| } | |
| pub trait OrCancelExt: Sized { | |
| type Output; | |
| fn or_cancel( | |
| self, | |
| token: &CancellationToken, | |
| ) -> impl Future<Output = Result<Self::Output, CancelErr>> + Send; | |
| } | |
| impl<F> OrCancelExt for F | |
| where | |
| F: Future + Send, | |
| F::Output: Send, | |
| { | |
| type Output = F::Output; | |
| async fn or_cancel(self, token: &CancellationToken) -> Result<Self::Output, CancelErr> { | |
| tokio::select! { | |
| _ = token.cancelled() => Err(CancelErr::Cancelled), | |
| res = self => Ok(res), | |
| } | |
| } | |
| } | |
| mod tests { | |
| use super::*; | |
| use pretty_assertions::assert_eq; | |
| use std::time::Duration; | |
| use tokio::task; | |
| use tokio::time::sleep; | |
| async fn returns_ok_when_future_completes_first() { | |
| let token = CancellationToken::new(); | |
| let value = async { 42 }; | |
| let result = value.or_cancel(&token).await; | |
| assert_eq!(Ok(42), result); | |
| } | |
| async fn returns_err_when_token_cancelled_first() { | |
| let token = CancellationToken::new(); | |
| let token_clone = token.clone(); | |
| let cancel_handle = task::spawn(async move { | |
| sleep(Duration::from_millis(10)).await; | |
| token_clone.cancel(); | |
| }); | |
| let result = async { | |
| sleep(Duration::from_millis(100)).await; | |
| 7 | |
| } | |
| .or_cancel(&token) | |
| .await; | |
| cancel_handle.await.expect("cancel task panicked"); | |
| assert_eq!(Err(CancelErr::Cancelled), result); | |
| } | |
| async fn returns_err_when_token_already_cancelled() { | |
| let token = CancellationToken::new(); | |
| token.cancel(); | |
| let result = async { | |
| sleep(Duration::from_millis(50)).await; | |
| 5 | |
| } | |
| .or_cancel(&token) | |
| .await; | |
| assert_eq!(Err(CancelErr::Cancelled), result); | |
| } | |
| } | |