shepmaster / snafu

Easily assign underlying errors into domain-specific errors while adding context
https://docs.rs/snafu/
Apache License 2.0
1.45k stars 61 forks source link

How do I make an error with Box<dyn std::error::Error> 'no_std' compatible? #257

Closed pranav-bhatt closed 4 years ago

pranav-bhatt commented 4 years ago

I couldn't find anything regarding this in the docs. I'm trying to make a library no_std compatible, however I'm getting stuck in the following lines :

#[snafu(display("Other error: {:?}", source))]
Other { source: Box<dyn std::error::Error> }

Here, I need to replace std::error:Error with an appropriate trait. I assume the inbuilt "std::error alternative" would work fine for this, but I don't think I can access it :P

Any advice on what I could do? Thanks!

shepmaster commented 4 years ago

In a no_std environment, there is no Error trait and there is no Box type (or allocation of any kind!), so your goal is not attainable.

I'd recommend against having something like Other { source: Box<dyn std::error::Error> }. Instead, I think it's better to have distinct error variants for each unique failure case. By doing that, you get better error messages for your users and get no-std compatibility for "free".