withoutboats / failure_derive

derive macro for the Fail trait
Apache License 2.0
24 stars 6 forks source link

Allow using generic type parameters that don't implement `Fail` #10

Open jvff opened 6 years ago

jvff commented 6 years ago

Currently Fail can be derived for types with generic type parameters, as long as they also implement the Fail trait. This means that when I derive fail for MyType<T>, the implementation has type bounds as follows impl<T: Fail> Fail for MyType<T>.

I would like the derived implementation to have custom bounds, so that I can use types that only implement Debug for example.

My use case is that I'm creating an error type for a custom tokio_service::Service that has the generic request and response types. My custom service only allows some requests to be handled, so I want to add an error variant for an unexpected request, something like:

#[derive(Debug, Fail)]
pub enum ServiceError<T: 'static + Debug + Send + Sync> {
    // ...
    #[fail(display = "Unexpected request: {:?}", _0)]
    UnexpectedRequest(T),
    // ...
}

I would suggest that the generic bounds be obtained from the definition of the type to be derived (in the example above, T: 'static + Debug + Send + Sync).