Skip to content

Commit

Permalink
Merge pull request #45 from kory33/remove-contains-distinct-t-constraint
Browse files Browse the repository at this point in the history
Remove `ContainsDistinctT` constraint
  • Loading branch information
kory33 authored Mar 6, 2022
2 parents fd6b037 + 7aad06a commit 7dcd769
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ type CodecBinding[A] = (PacketId, ByteCodec[A])
/**
* An object that associates packet IDs with corresponding datatypes' codec.
*
* [[PacketTup]] is a tuple of packets with no duplicates, used in associating codecs. It is
* also required that [[bindings]] should not contain two entries with the same packet ID.
* [[PacketTup]] is a tuple of packets with no duplicates, and is used in associating codecs. It
* is also required that [[bindings]] should not contain two entries with the same packet ID.
*/
class PacketIdBindings[PacketTup <: Tuple](
bindings: Tuple.Map[PacketTup, CodecBinding]
)(using Require[ContainsDistinctT[PacketTup]]) {
) {

require(
{
Expand Down Expand Up @@ -65,12 +65,8 @@ class PacketIdBindings[PacketTup <: Tuple](
}

object PacketIdBindings {
def apply[BindingsTup <: Tuple](
bindingsTup: BindingsTup
)(
def apply[BindingsTup <: Tuple](bindingsTup: BindingsTup)(
using ev: Tuple.IsMappedBy[CodecBinding][BindingsTup]
)(
using Require[ContainsDistinctT[Tuple.InverseMap[BindingsTup, CodecBinding]]]
): PacketIdBindings[Tuple.InverseMap[BindingsTup, CodecBinding]] = {
new PacketIdBindings[InverseMap[BindingsTup, CodecBinding]](
ev(bindingsTup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ type IncludedInLockedT[T <: Tuple, A] <: Boolean =
case ? *: tail => IncludedInLockedT[tail, A]
}

/**
* INTERNAL. A type-level boolean indicating if T, mapped with [[Lock]], contains no duplicate
* types.
*
* Takes O(|T|^2) to compute.
*/
type ContainsDistinctLockedT[T <: Tuple] <: Boolean =
T match {
case Lock[head] *: tail => ![IncludedInLockedT[tail, head]] && ContainsDistinctLockedT[tail]
case EmptyTuple => true
}

/**
* INTERNAL. Lock tuple [[T]] using [[Lock]] constructor.
*/
Expand All @@ -55,13 +43,6 @@ type Includes[A] = [T <: Tuple] =>> Require[IncludedInT[T, A]]
*/
type IncludedBy[T <: Tuple] = [A] =>> Require[IncludedInT[T, A]]

/**
* A type-level boolean indicating if [[T]] only contains distinct types.
*
* Takes O(|T|^2) to compute.
*/
type ContainsDistinctT[T <: Tuple] = ContainsDistinctLockedT[LockTuple[T]]

/**
* INTERNAL. The index of [[A]] in a tuple [[T]] mapped with [[Lock]].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ class TupleSpec extends AnyFlatSpec with should.Matchers {
summon[IncludedInT[(Int, String, Double), Float] =:= false]
}

"ContainsDistinctT" should "tell whether a tuple contains distinct types" in {
summon[ContainsDistinctT[EmptyTuple] =:= true]
summon[ContainsDistinctT[Int *: EmptyTuple] =:= true]
summon[ContainsDistinctT[(Int, String, Double)] =:= true]
summon[ContainsDistinctT[(Int, 1, 2)] =:= true]
summon[ContainsDistinctT[(Int, Int | String, String)] =:= true]

summon[ContainsDistinctT[(Int, Int)] =:= false]
summon[ContainsDistinctT[(Int, String, Int)] =:= false]
summon[ContainsDistinctT[(String, Int, Int)] =:= false]
summon[ContainsDistinctT[(Int | String, String, Int, Int | String)] =:= false]
summon[ContainsDistinctT[(true, 1, 2, 3, 1, false)] =:= 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]
Expand Down

0 comments on commit 7dcd769

Please sign in to comment.