1 // These are simplifications of the tower traits by the same name:
3 pub trait Service<Request> {
11 // Any type will do here:
16 // This is encoding a trait alias.
18 pub trait ParticularService:
19 Service<Req, Response = Res> {
22 impl<T> ParticularService for T
24 T: Service<Req, Response = Res>,
28 // This is also a trait alias.
29 // The weird = <Self as ...> bound is there so that users of the trait do not
30 // need to repeat the bounds. See https://github.com/rust-lang/rust/issues/20671
31 // for context, and in particular the workaround in:
32 // https://github.com/rust-lang/rust/issues/20671#issuecomment-529752828
34 pub trait ParticularServiceLayer<C>:
35 Layer<C, Service = <Self as ParticularServiceLayer<C>>::Service>
37 type Service: ParticularService;
40 impl<T, C> ParticularServiceLayer<C> for T
43 T::Service: ParticularService,
45 type Service = T::Service;
48 // These are types that implement the traits that the trait aliases refer to.
49 // They should also implement the alias traits due to the blanket impls.
52 impl<C> Layer<C> for ALayer<C> {
53 type Service = AService;
57 impl Service<Req> for AService {
58 // However, AService does _not_ meet the blanket implementation,
59 // since its Response type is bool, not Res as it should be.
63 // This is a wrapper type around ALayer that uses the trait alias
64 // as a way to communicate the requirements of the provided types.
67 // The method and the free-standing function below both have the same bounds.
71 ALayer<C>: ParticularServiceLayer<C>,
76 fn check<C>(_: C) where ALayer<C>: ParticularServiceLayer<C> {}
78 // But, they give very different error messages.
81 // This gives a very poor error message that does nothing to point the user
82 // at the underlying cause of why the types involved do not meet the bounds.
83 Client(()).check(); //~ ERROR E0599
85 // This gives a good(ish) error message that points the user at _why_ the
86 // bound isn't met, and thus how they might fix it.
87 check(()); //~ ERROR E0271