Skip to content

Instantly share code, notes, and snippets.

@Kingdutch
Created March 16, 2025 12:36
Show Gist options
  • Select an option

  • Save Kingdutch/7bd4472853b79d0e799530328b0a80ca to your computer and use it in GitHub Desktop.

Select an option

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.
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