Type inference in anyhow::Context wrapper


I use anyhow for errors in a codebase where errors in some parts are frequent but have had to disable backtracing/capture of stacktrace (they have horrible performance on windows) but still need to propagate the context of en error upwards.

My attempt to do this is to create a extension trait extending anyhow::Result with a method .at() togeather with a macro at!() (allows me to use the line! and file! macros) with the intended use as:

inner_function().at(at!("error while eating icecream"))?;

However, I'm struggling to get the type inference to work - does anyone have any idea why the floowing does not work / what I can do about it?


Remove the unused generic parameter E from your trait.

trait MyContext<T> {
    fn at<C, F>(self, f: F) -> Result<T, Error>
        C: std::fmt::Display + Send + Sync + 'static,
        F: FnOnce() -> C;

impl<T> MyContext<T> for Result<T, anyhow::Error> {
    fn at<C, F>(self, f: F) -> Result<T, anyhow::Error>
        C: std::fmt::Display + Send + Sync + 'static,
        F: FnOnce() -> C,

The error is because it can't figure out what E should be.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.