tuple_tricks

Crates.iotuple_tricks
lib.rstuple_tricks
version0.2.1
sourcesrc
created_at2021-12-18 08:46:23.574689
updated_at2021-12-22 21:29:54.128476
descriptionA couple of traits on tuples that allow inductively building new traits
homepagehttps://github.com/jcrist1/tuple_tricks/
repositoryhttps://github.com/jcrist1/tuple_tricks/
max_upload_size
id499950
size5,044
(jcrist1)

documentation

README

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
{
    ...
}
Commit count: 26

cargo fmt