Complicated associated types and impl trait in return position

Suppose I have a trait such as

trait Service {
    type ResponseBody: Body;
    type Future: Future<Output = Self::ResponseBody>;

and I would like to return an impl Service from a function like

        fn(..) -> impl Service<
                ResponseBody = impl Body<Data = Bytes, Error = impl ... + Send> + ...,
                Future = impl Send,
            > + ...

and it is important that I get to specify that Future is Send.
Now, while this works it generates a warning of the type opaque_hidden_inferred_bound. So I should actually write Future = impl Send + Future<Output = ?>. I do not know how to specify this ? part as what would be the analogue of the Self::ResponseBody in the trait definition in this context? The compiler suggests using a fully qualified name, but I do not know how to refer to the ResponseBody inside the impl.

You can't name it unless ResponseBody is a concrete type you can name instead of using impl Body, or maybe until we get TAIT (type alias impl Trait) so you can name the type. I say maybe as if you can't name the type, TAIT currently gets thrown for a loop.

The lint is fairly new and the idea is that you can't assume the constraints imposed by the associated type bound from the opaque "type" impl Send [+ Future<...>]. In particular there's no way without TAIT to say "the output of this future is the same impl Body as ResponseBody" in the function signature. If that's not important, you could just silence the lint I suppose.

However, even if not important to your particular use case, I suggest creating an issue mentioning the lint PR and pointing out TAIT doesn't seem to help if you can't name the concrete type. If you can't name a concrete type here, there seems to be no way to satisfy the lint even on nightly.

Edit: TAIT can be made to work, see next post. I still think it's worth an issue as you can't necessarily satisfy the lint on stable.

1 Like

There is a workaround for the TAIT thing actually.

Thanks so much for the insightful explanation and the TAIT example.

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.