#tuple #traits #building #head #tricks

tuple_tricks

A couple of traits on tuples that allow inductively building new traits

3 unstable releases

0.2.1 Dec 22, 2021
0.2.0 Dec 22, 2021
0.1.0 Dec 18, 2021

#2420 in Rust patterns

MIT license

6KB
88 lines

Building an inductive trait on all tuples (up to length 32).

First you will need to provide all tuples with a marker. This is done with

mark_tuples!(MyMarker)

Then you define a trait that you want to apply to aa tuples of up to length 32:

trait MyInductiveTrait {
    type AccumulatedTupleType;
    fn fn_to_build_accumulated_type_from_self(self) -> Self::AccumulatedTupleType;
}

Then define the trait for the one-tuple (i.e. the start of induction)

impl<A> MyInductiveTrait for (A,) {
    type AccumulatedTupleType = (MyStruct<A>,);

    fn fn_to_build_accumulated_type_from_self(self) -> Self::AccumulatedTupleType {
        let (a,) = self;
        (MyStruct::new(a),)
    }
}

The hardest is the induction step, where you will probably need a good many trait bounds:

impl<TupleType, Head, PrevTupleType, PrevAccumulatedTuple> MyInductiveTrait for TupleType
where
    TupleType: PreviousTuple<Head = Head, TailTuple = PrevTuple> + MyMarker,
    // You need the `MyMarker` trait because otherwise the compiler complains about potential
    // changes to the PreviousTuple implementation.
    PrevTuple: MyInductiveTrait<AccumulatedTupleType = PrevAccumulatedTuple>,
    PrevAccumulatedTuple: NestTuple,
    (PrevAccumulatedTuple::Nested, MyStruct<Head>): UnnestTuple
{
    ...
}

Dependencies