How to store in a struct a (effectively dynamic) builder return value with very complex types

A builder (tower::ServiceBuilder, if you are interested) returns a value of this type:

AsyncFilter<AndThen<S, fn post_integration_chain_filter(Response<Body>) -> impl Future<Output = Result<Response<Body>, Box<dyn Error + Send + Sync>>>>, fn pre_integration_chain_filter(Request<Body>) -> impl Future<Output = Result<Request<Body>, Box<dyn Error + Send + Sync>>>>

I have to store that value in a struct ... how on EARTH do I define the type of the struct field???

There are two options:

  • just make it generic
  • decide what you want to use it for, and store it as a trait object (Box<dyn TheTraitYouNeed>)

I tried the "just make it generic" route - which brought up a question that I haven't tried out ... I have to implement a ONE-generic trait on this struct, but making it generic would cause the struct to have two generic arguments ... is that an issue?

I don't know, is it for you?

LOL - I'll see what happens - but actually the Box maybe easier to manage! Thx

Well either way is turning put to be nigh-on impossible - SO many trait constraints to take into account in either the Box trait (tower::Service) or the generic type.

do you have to store this kind of types at the first place? such complex type typically is not meant to spell out, it is mainly used for local let bindings.

if you are making some kind of wrapper, think what is the real trait bound you require, what is the purpose your wrapper serve, not how the concrete (yet opaque) type is named.