Enhance Rust errors with file and line details
I don&#x27;t like having to wrap my function with a macro, with all disadvantages (e.g. worse IDE support), just to get error location. Especially given we can do this without macros at all:<p><pre><code>    use std::error::Error as StdError;
    use std::panic::Location;
    use std::fmt;

    #[derive(Debug)]
    pub struct MyError {
        pub error: Box&lt;dyn StdError + Send + Sync&gt;,
        pub location: &amp;&#x27;static Location&lt;&#x27;static&gt;,
    }
    
    impl&lt;E: StdError + Send + Sync + &#x27;static&gt; From&lt;E&gt; for MyError {
        #[track_caller]
        #[inline]
        fn from(error: E) -&gt; Self {
            Self {
                error: Box::new(error),
                location: Location::caller(),
            }
        }
    }
    
    impl fmt::Display for MyError {
        fn fmt(&amp;self, f: &amp;mut fmt::Formatter&lt;&#x27;_&gt;) -&gt; fmt::Result {
            write!(f, &quot;{}, {}&quot;, self.error, self.location)
        }
    }
</code></pre>
We can also optimize this more (e.g. a simple optimization to be two words instead of three: <a href="https:&#x2F;&#x2F;play.rust-lang.org&#x2F;?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=2a46e8a71103b4a2f432defc320dc06a" rel="nofollow noreferrer">https:&#x2F;&#x2F;play.rust-lang.org&#x2F;?version=stable&amp;mode=debug&amp;editio...</a>. Also allows opting `Send` and `Sync` out or using non-`&#x27;static` lifetimes), but this works as a baseline. The only disadvantage is that if called in a function with `#[track_caller]` this will report the location of the caller... But this is something I can live with.