'field selector' generic?

You need to define a separate trait to act as the type level function between a selector type and the field type. (In the future when we have general const generics, the selector could be an enum instead of four separate types.)

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,
}

Note that you might want to eliminate HasFields entirely, and just have the implementors directly implement Select four times. This might be usefully more flexible and simplify trait bounds, or it might complicate the job of implementors without any benefits, depending on the situation.

2 Likes