Created
March 16, 2025 12:36
-
-
Save Kingdutch/7bd4472853b79d0e799530328b0a80ca to your computer and use it in GitHub Desktop.
Allow `.context` to be called on a future that returns result without awaiting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::error::Error; | |
| use std::result::{Result as StdResult}; | |
| use anyhow::{Context, Result}; | |
| trait FutureExt<O> { | |
| async fn context(self, ctx: &'static str) -> Result<O, anyhow::Error>; | |
| } | |
| impl<T, O, E> FutureExt<O> for T | |
| where | |
| T: Future<Output = StdResult<O, E>>, | |
| E: Error + Send + Sync + 'static, | |
| { | |
| async fn context(self, ctx: &'static str) -> Result<O> { | |
| self.await.context(ctx) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment