Skip to content

Commit b49fde8

Browse files
committed
New encoding of FromDigits that has better visibility
1 parent b46ac5a commit b49fde8

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

core/shared/src/main/scala-0.22/scodec/bits/ByteVectorPlatform.scala

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@ import scala.quoted._
44
import scala.quoted.matching._
55
import scala.util.FromDigits
66

7-
// TODO: as of 0.22, making this private[bits] results in errors like the following at call sites:
8-
// scala> val b: ByteVector = 0x00112233_44556677_8899aabb_ccddeeff_fedba098
9-
// trait ByteVectorPlatform cannot be accessed as a member of scodec.bits from module class rs$line$2$.
10-
trait ByteVectorPlatform { self: ByteVector.type =>
7+
private[bits] trait ByteVectorPlatform { self: ByteVector.type =>
8+
given as FromDigits.WithRadix[ByteVector] = ByteVectorFromDigits.Instance
9+
}
10+
11+
object ByteVectorFromDigits {
12+
13+
private def digitsToByteVector(digits: String, radix: Int): ByteVector =
14+
if (radix == 16) ByteVector.fromValidHex(digits.tail)
15+
else throw FromDigits.MalformedNumber(s"unsupported radix $radix")
1116

12-
class FromDigits extends FromDigits.WithRadix[ByteVector] {
17+
private[bits] class Base extends FromDigits.WithRadix[ByteVector] {
1318
def fromDigits(digits: String, radix: Int): ByteVector =
1419
digitsToByteVector(digits, radix)
1520
}
1621

17-
given FromDigits {
22+
private[bits] object Instance extends Base {
1823
override inline def fromDigits(digits: String): ByteVector =
1924
${digitsToByteVectorMacro('digits, Expr(10))}
2025
override inline def fromDigits(digits: String, radix: Int): ByteVector =
2126
${digitsToByteVectorMacro('digits, 'radix)}
2227
}
23-
}
24-
25-
private[bits] def digitsToByteVector(digits: String, radix: Int): ByteVector =
26-
if (radix == 16) ByteVector.fromValidHex(digits.tail)
27-
else throw FromDigits.MalformedNumber(s"unsupported radix $radix")
28-
2928

30-
def digitsToByteVectorMacro(digits: Expr[String], radix: Expr[Int])(given qctx: QuoteContext): Expr[ByteVector] =
31-
(digits, radix) match {
32-
case (Const(ds), Const(r)) =>
33-
if (r == 16) {
34-
'{ByteVector.fromValidHex($digits.tail)}
35-
} else {
36-
qctx.error(s"unsupported radix $r", radix)
37-
'{ByteVector.empty}
38-
}
39-
case other =>
40-
'{digitsToByteVector($digits, $radix)}
41-
}
29+
private def digitsToByteVectorMacro(digits: Expr[String], radix: Expr[Int])(given qctx: QuoteContext): Expr[ByteVector] =
30+
(digits, radix) match {
31+
case (Const(ds), Const(r)) =>
32+
if (r == 16) {
33+
'{ByteVector.fromValidHex($digits.tail)}
34+
} else {
35+
qctx.error(s"unsupported radix $r", radix)
36+
'{ByteVector.empty}
37+
}
38+
case other =>
39+
'{digitsToByteVector($digits, $radix)}
40+
}
41+
}

0 commit comments

Comments
 (0)