Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//> using scala 3.8.4

//> using dep io.github.halotukozak::made:0.1.2
//> using dep io.github.halotukozak::made:0.1.3

//> using test.dep org.scalameta::munit:1.3.3
//> using test.dep org.scalameta::munit-scalacheck:1.3.0
Expand Down
18 changes: 9 additions & 9 deletions src/mcodec/Derivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ trait Derivation:
deferred.underlying = built
built

transparent inline private def deriveDispatch[T](m: Made.Of[T]): MCodec[T] = inline m match
transparent inline def deriveDispatch[T](m: Made.Of[T]): MCodec[T] = inline m match
case pm: Made.ProductOf[T] => deriveProduct[T](pm)
case sm: Made.SumOf[T] => deriveSum[T](sm)
case tm: Made.TransparentOf[T] => deriveTransparent[T](tm)
case gm: Made.SingletonOf[T] => deriveSingleton[T](gm)

inline private def deriveProduct[T](m: Made.ProductOf[T]): MCodec[T] = ProductCodec[T](
inline def deriveProduct[T](m: Made.ProductOf[T]): MCodec[T] = ProductCodec[T](
compiletime.constValue[m.Label],
compiletime.constValueTuple[m.ElemLabels].toArrayOf[String],
compiletime.summonAll[Tuple.Map[m.ElemTypes, MCodec]].toArrayOf[MCodec[Any]](using containsOnly.refl),
Expand All @@ -43,12 +43,12 @@ trait Derivation:
.toArrayOf[MCodec[Any]](using containsOnly.refl),
)

inline private def isOptionFlags[Elems <: Tuple]: List[Boolean] = inline compiletime.erasedValue[Elems] match
inline def isOptionFlags[Elems <: Tuple]: List[Boolean] = inline compiletime.erasedValue[Elems] match
case _: EmptyTuple => Nil
case _: (Option[?] *: tail) => true :: isOptionFlags[tail]
case _: (head *: tail) => false :: isOptionFlags[tail]

inline private def deriveSum[T](m: Made.SumOf[T]): MCodec[T] =
inline def deriveSum[T](m: Made.SumOf[T]): MCodec[T] =
inline if m.hasAnnotation[mcodec.annotation.flatten] then
FlatSumCodec[T](
compiletime.constValue[m.Label],
Expand All @@ -68,7 +68,7 @@ trait Derivation:
)

// Compile-time guard (>1 @defaultCase is an error) + index selection.
transparent inline private def defaultCaseIdx(inline flags: Tuple): Int = inline flags match
transparent inline def defaultCaseIdx(inline flags: Tuple): Int = inline flags match
case _: EmptyTuple => -1
case fs: (head *: tail) =>
inline if compiletime.constValue[head & Boolean] then
Expand All @@ -79,13 +79,13 @@ trait Derivation:
case -1 => -1
case n => n + 1

inline private def rejectMoreDefaults(inline flags: Tuple): Unit = inline flags match
inline def rejectMoreDefaults(inline flags: Tuple): Unit = inline flags match
case _: EmptyTuple => ()
case fs: (head *: tail) =>
inline if compiletime.constValue[head & Boolean] then compiletime.error("more than one @defaultCase in hierarchy")
else rejectMoreDefaults(fs.tail)

inline private def summonOrDeriveCases[Elems <: Tuple]: List[MCodec[Any]] =
inline def summonOrDeriveCases[Elems <: Tuple]: List[MCodec[Any]] =
inline compiletime.erasedValue[Elems] match
case _: EmptyTuple => Nil
case _: (head *: tail) =>
Expand All @@ -95,9 +95,9 @@ trait Derivation:

c.asInstanceOf[MCodec[Any]] :: summonOrDeriveCases[tail]

inline private def deriveSingleton[T](m: Made.SingletonOf[T]): MCodec[T] = SingletonCodec[T](m.value)
inline def deriveSingleton[T](m: Made.SingletonOf[T]): MCodec[T] = SingletonCodec[T](m.value)

inline private def deriveTransparent[T](m: Made.TransparentOf[T]): MCodec[T] = TransparentCodec(
inline def deriveTransparent[T](m: Made.TransparentOf[T]): MCodec[T] = TransparentCodec(
m.wrap,
m.unwrap,
compiletime.summonInline[MCodec[m.ElemType]],
Expand Down
Loading