Refactoring help

what I have:

pub struct Foo<T> {
    _t: PhantomData<T>,
}

pub struct Bar<T> {
    _t: PhantomData<T>,
}

pub struct Cat<T> {
    _t: PhantomData<T>,
}

pub struct Dog<T> {
    _t: PhantomData<T>,
}

pub struct A {}
pub struct B {}
pub struct C {}
pub struct D {}
pub struct E {}
pub struct F {}
pub struct G {}
pub struct H {}

pub enum Foo_E {
    A(Foo<A>),
    B(Foo<B>),
    C(Foo<C>),
    D(Foo<D>),
    E(Foo<E>),
    F(Foo<F>),
    G(Foo<G>),
    H(Foo<H>),
}

pub enum Bar_E {
    A(Bar<A>),
    B(Bar<B>),
    C(Bar<C>),
    D(Bar<D>),
    E(Bar<E>),
    F(Bar<F>),
    G(Bar<G>),
    H(Bar<H>),
}

pub enum Cat_E {
    A(Cat<A>),
    B(Cat<B>),
    C(Cat<C>),
    D(Cat<D>),
    E(Cat<E>),
    F(Cat<F>),
    G(Cat<G>),
    H(Cat<H>),
}

pub enum Dog_E {
    A(Dog<A>),
    B(Dog<B>),
    C(Dog<C>),
    D(Dog<D>),
    E(Dog<E>),
    F(Dog<F>),
    G(Dog<G>),
    H(Dog<H>),
}

what I want to write:


pub enum Does_Not_Compile<T> {
    A(T<A>),
    B(T<B>),
    C(T<C>),
    D(T<D>),
    E(T<E>),
    F(T<F>),
    G(T<G>),
    H(T<H>),
}

type Foo_E = Does_Not_Compile<Foo>;
type Bar_E = Does_Not_Compile<Bar>;
type Cat_E = Does_Not_Compile<Cat>;
type Dog_E = Does_Not_Compile<Dog>;

In A<B> we can define things here B is generic; is there anyway to define things where the A is generic ?

1 Like

You can use generic associated types to emulate this feature: Rust Playground

4 Likes

Mechanically, I understand how this works.

Intuitively, I'm still a bit confused, especially by the

pub trait TyCtor {
    type Ty<T>;
}

Correct me if I'm wrong: GATs has only been in Rust stable for < 1.5 years ?

TyCtor represents a "type constructor", that is a way to construct a new type given a T.

Yes.

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.