#How to create function with closure argument that returns generic Error?

16 messages · Page 1 of 1 (latest)

hot elm
#

How to create function with closure argument that returns generic Error?

lilac moon
#

That's not exactly what the problem is, but it's basically accurate

#

The actual issue is that the impl is impl<E: Error> Error for Box<E>

#

Only boxes of sized errors are errors

#

As for the solution: do you need pass_closure to have an Error bound?

hot elm
#

Well minimally I could work with Display orDebug but it feels kind of ugly to make it a public facing API. I might also need Error::source to provide extra debug information as the actual use case goes pretty deep (moving the closure as Lua closures and then called from C whenever the binary needs to call it, ofcourse with a bunch of wrappers in between for ABI compatibility)
Though does it make sense to accept a Display instead of Error in a public API?
Assuming I need the Error bound what would my options be?

noble arrow
#

you can write a type that contains Box<dyn Error> and implements Error and forwards

#

it's only Box<dyn Error> exactly that is stuck with this doesn't-implement-Error thing

#

struct MyOwnDynError(Box<dyn Error>) can do whatever you want

hot elm
#

That would work but it means a user would have to know and use this niche type.

#

Since its a public API I want to keep it as simple as possible.

hot elm
#

Actually the more I think about it introducing a struct is making more sense. Still open to other solutions though.

hidden ore
#

hyper and friends work around this with the bound E: Into<Box<dyn Error + Send + Sync + 'static>> instead of E: Error + 'static, and the blanket impl<T> Into<T> for T allows using Box<dyn Error + Send + Sync + 'static> exactly

#

the tradeoff is that you cannot use E as a impl Error, the only thing you can do with it is turn it into a box