Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for SIP-56 match types. #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.github.kory33.s2mctest.core.connection.codec.dsl.DecodeFiniteBytes
import io.github.kory33.s2mctest.core.generic.compiletime.*
import io.github.kory33.s2mctest.core.generic.extensions.MappedTupleExt.mapToList

import scala.Tuple.{Elem, InverseMap}
import scala.annotation.implicitNotFound

type PacketId = Int
Expand Down Expand Up @@ -66,10 +65,16 @@ class PacketIdBindings[PacketTup <: Tuple](

object PacketIdBindings {
def apply[BindingsTup <: Tuple](bindingsTup: BindingsTup)(
using ev: Tuple.IsMappedBy[CodecBinding][BindingsTup]
): PacketIdBindings[Tuple.InverseMap[BindingsTup, CodecBinding]] = {
new PacketIdBindings[InverseMap[BindingsTup, CodecBinding]](
using ev: BindingsTup =:= Tuple.Map[InverseCodecBindings[BindingsTup], CodecBinding]
): PacketIdBindings[InverseCodecBindings[BindingsTup]] = {
new PacketIdBindings[InverseCodecBindings[BindingsTup]](
ev(bindingsTup)
)
}

/** Converts a tuple `(CodecBinding[T1], ..., CodecBinding[Tn])` to `(T1, ... Tn)` */
type InverseCodecBindings[X <: Tuple] <: Tuple = X match {
case (PacketId, ByteCodec[x]) *: t => x *: InverseCodecBindings[t]
case EmptyTuple => EmptyTuple
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.annotation.implicitNotFound
* @see
* [[IncludedInLockedT]] for an example usage.
*/
type Lock[X]
sealed trait Lock[X]

/**
* An implicit evidence that the type [[S]] can be reduced to the singleton type [[true]].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ class TupleSpec extends AnyFlatSpec with should.Matchers {
summon[IncludedInT[(Int, String, Double), Double] =:= true]

summon[IncludedInT[EmptyTuple, Any] =:= false]
summon[IncludedInT[(Int, String, Double), Any] =:= false]
summon[IncludedInT[(Int, String, Double), 1] =:= false]
summon[IncludedInT[(Int, String, Double), Int | String] =:= false]
summon[IncludedInT[(Int, String, Double), Float] =:= false]
}

"IndexOfT" should "extract the index of a specific type from a tuple" in {
summon[IndexOfT[Int, (String, Int)] =:= 1]
summon[IndexOfT[42, (String, Int, 42)] =:= 2]
summon[IndexOfT[Int, (String | Int, Double, Int)] =:= 2]

"summon[IndexOfT[42, (0, 0)] =:= 0]" shouldNot compile
"summon[IndexOfT[42, (0, 0)] =:= 1]" shouldNot compile
Expand Down