@@ -100,46 +100,44 @@ Here is an example of how to define a trait that works on tuples of any size whe
100100use tuplities :: prelude :: * ;
101101use core :: fmt :: Display ;
102102
103- // 1. Define the trait for the nested structure (recursive)
104- trait MyVariadicTrait {
105- fn print_all (& self );
103+ // 1. Define the trait for flat & nested tuples
104+ trait PrintTuple {
105+ fn print_tuple (& self );
106+ }
107+ trait PrintNested {
108+ fn print_nested (& self );
106109}
107110
108- // 2. Implement for the base cases (unit and single-element tuples)
109- impl MyVariadicTrait for () {
110- fn print_all (& self ) {}
111+ // 2. Implement for the base cases
112+ impl PrintNested for () {
113+ fn print_nested (& self ) {}
111114}
112- impl <Head : Display > MyVariadicTrait for (Head ,) {
113- fn print_all (& self ) {
115+ impl <Head : Display > PrintNested for (Head ,) {
116+ fn print_nested (& self ) {
114117 println! (" {}" , self . 0 );
115118 }
116119}
117120
118- // 3. Implement for the recursive case (Head, Tail)
119- impl <Head , Tail > MyVariadicTrait for (Head , Tail )
121+ // 3. Implement for the recursive case
122+ impl <Head , Tail > PrintNested for (Head , Tail )
120123where
121- Head : Display , // Constraint on the current element
122- Tail : MyVariadicTrait , // Recursive constraint on the rest
124+ Head : Display , // Current element constraint
125+ Tail : PrintNested , // Recursive constraint
123126{
124- fn print_all (& self ) {
127+ fn print_nested (& self ) {
125128 println! (" {}" , self . 0 );
126- self . 1. print_all ();
129+ self . 1. print_nested ();
127130 }
128131}
129132
130- // 4. Define the trait for flat tuples
131- trait PrintTuple {
132- fn print_tuple (& self );
133- }
134-
135- // 5. Blanket implementation for any tuple that can be nested
136- // and whose nested form implements MyVariadicTrait
133+ // 4. Blanket implementation for any nestable tuple
134+ // whose nested form implements PrintNested
137135impl <T > PrintTuple for T
138136where
139- for <'a > & 'a T : NestTuple <Nested : MyVariadicTrait >,
137+ for <'a > & 'a T : NestTuple <Nested : PrintNested >,
140138{
141139 fn print_tuple (& self ) {
142- self . nest (). print_all ();
140+ self . nest (). print_nested ();
143141 }
144142}
145143
0 commit comments