@@ -4,7 +4,7 @@ import scala.collection.mutable
4
4
object Deriving {
5
5
6
6
enum Shape {
7
- case Sum [Alts <: Tuple ]
7
+ case Cases [Alts <: Tuple ]
8
8
case Case [T , Elems <: Tuple ]
9
9
}
10
10
@@ -29,7 +29,7 @@ object Lst {
29
29
// common compiler-generated infrastructure
30
30
import Deriving ._
31
31
32
- type LstShape [T ] = Shape .Sum [(
32
+ type LstShape [T ] = Shape .Cases [(
33
33
Shape .Case [Cons [T ], (T , Lst [T ])],
34
34
Shape .Case [Nil .type , Unit ]
35
35
)]
@@ -96,7 +96,7 @@ object Eq {
96
96
inline def equalsCase [T , Elems <: Tuple ](mapper : GenericMapper [T ], x : T , y : T ) =
97
97
equalsElems[Elems ](mapper.toGenericCase(x).elems, mapper.toGenericCase(y).elems, 0 )
98
98
99
- inline def equalsSum [T , Alts <: Tuple ](mapper : GenericMapper [T ], x : T , y : T ): Boolean =
99
+ inline def equalsCases [T , Alts <: Tuple ](mapper : GenericMapper [T ], x : T , y : T ): Boolean =
100
100
inline erasedValue[Alts ] match {
101
101
case _ : (Shape .Case [alt, elems] *: alts1) =>
102
102
x match {
@@ -105,16 +105,16 @@ object Eq {
105
105
case y : `alt` => equalsCase[T , elems](mapper, x, y)
106
106
case _ => false
107
107
}
108
- case _ => equalsSum [T , alts1](mapper, x, y)
108
+ case _ => equalsCases [T , alts1](mapper, x, y)
109
109
}
110
110
case _ : Unit =>
111
111
false
112
112
}
113
113
114
114
inline def derived [T , S <: Shape ](implicit ev : HasShape [T , S ]): Eq [T ] = new {
115
115
def equals (x : T , y : T ): Boolean = inline erasedValue[S ] match {
116
- case _ : Shape .Sum [alts] =>
117
- equalsSum [T , alts](ev, x, y)
116
+ case _ : Shape .Cases [alts] =>
117
+ equalsCases [T , alts](ev, x, y)
118
118
case _ : Shape .Case [alt, elems] =>
119
119
equalsCase[T , elems](ev, x, y)
120
120
}
@@ -152,15 +152,15 @@ object Pickler {
152
152
inline def pickleCase [T , Elems <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], x : T ): Unit =
153
153
pickleElems[Elems ](buf, mapper.toGenericCase(x).elems, 0 )
154
154
155
- inline def pickleSum [T , Alts <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], x : T , n : Int ): Unit =
155
+ inline def pickleCases [T , Alts <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], x : T , n : Int ): Unit =
156
156
inline erasedValue[Alts ] match {
157
157
case _ : (Shape .Case [alt, elems] *: alts1) =>
158
158
x match {
159
159
case x : `alt` =>
160
160
buf += n
161
161
pickleCase[T , elems](mapper, buf, x)
162
162
case _ =>
163
- pickleSum [T , alts1](mapper, buf, x, n + 1 )
163
+ pickleCases [T , alts1](mapper, buf, x, n + 1 )
164
164
}
165
165
case _ : Unit =>
166
166
}
@@ -183,25 +183,25 @@ object Pickler {
183
183
mapper.fromGenericCase(GenericCase (ordinal, elems))
184
184
}
185
185
186
- inline def unpickleSum [T , Alts <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], ordinal : Int , n : Int ): T =
186
+ inline def unpickleCases [T , Alts <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], ordinal : Int , n : Int ): T =
187
187
inline erasedValue[Alts ] match {
188
188
case _ : (Shape .Case [alt, elems] *: alts1) =>
189
189
if (n == ordinal) unpickleCase[T , elems](mapper, buf, ordinal)
190
- else unpickleSum [T , alts1](mapper, buf, ordinal, n + 1 )
190
+ else unpickleCases [T , alts1](mapper, buf, ordinal, n + 1 )
191
191
case _ =>
192
192
throw new IndexOutOfBoundsException (s " unexpected ordinal number: $ordinal" )
193
193
}
194
194
195
195
inline def derived [T , S <: Shape ](implicit ev : HasShape [T , S ]): Pickler [T ] = new {
196
196
def pickle (buf : mutable.ListBuffer [Int ], x : T ): Unit = inline erasedValue[S ] match {
197
- case _ : Shape .Sum [alts] =>
198
- pickleSum [T , alts](ev, buf, x, 0 )
197
+ case _ : Shape .Cases [alts] =>
198
+ pickleCases [T , alts](ev, buf, x, 0 )
199
199
case _ : Shape .Case [alt, elems] =>
200
200
pickleCase[T , elems](ev, buf, x)
201
201
}
202
202
def unpickle (buf : mutable.ListBuffer [Int ]): T = inline erasedValue[S ] match {
203
- case _ : Shape .Sum [alts] =>
204
- unpickleSum [T , alts](ev, buf, nextInt(buf), 0 )
203
+ case _ : Shape .Cases [alts] =>
204
+ unpickleCases [T , alts](ev, buf, nextInt(buf), 0 )
205
205
case _ : Shape .Case [alt, elems] =>
206
206
unpickleCase[T , elems](ev, buf, 0 )
207
207
}
@@ -260,6 +260,7 @@ object Test extends App {
260
260
pklp.pickle(buf, p1)
261
261
println(buf)
262
262
val p1a = pklp.unpickle(buf)
263
+ println(p1a)
263
264
assert(p1 == p1a)
264
265
assert(eqp.equals(p1, p1a))
265
266
}
0 commit comments