Trait / generic issue

This is a followup to 'field selector' generic? - #3 by kpreid

In particular, we have:

pub trait HasFields {
  type A;
  type B;
  type C;
  type D;
}

pub trait Select<F> {
    type Out;
}

pub struct SelA;
pub struct SelB;
pub struct SelC;
pub struct SelD;
impl<T: HasFields> Select<SelA> for T { type Out = T::A; }
impl<T: HasFields> Select<SelB> for T { type Out = T::B; }
impl<T: HasFields> Select<SelC> for T { type Out = T::C; }
impl<T: HasFields> Select<SelD> for T { type Out = T::D; }

pub struct Foo<T: HasFields + Select<F>, F> {
  inner: <T as Select<F>>::Out,
}

I want to say pub struct Bar<T> ... { ... } conditioned on Select<T> being implemented. Is there a way to add this constraint ?

In the previous thread I mentioned using a bound like

T: Select<SelA> + Select<SelB> + Select<SelC> + Select<SelD>

If that's not what you mean, and neither is Foo, can you clarify exactly what effect you want?

  1. I think my original question was pretty poorly written. Part of the fault: I'm still struggling with Rust generics / traits / their interactions. I'm still not sure how to formulate this question properly.

  2. This issue is now 'resolved' not via solving it, but I redesigned some other stuff and no longer need it.

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.