Add Scala 2.12 equivalent Iterator apply method for ByteString

This commit is contained in:
Matthew de Detrich 2023-03-21 13:16:35 +01:00 committed by Matthew de Detrich
parent 44d03df694
commit c4e33c3972
2 changed files with 20 additions and 0 deletions

View file

@ -814,6 +814,13 @@ class ByteStringSpec extends AnyWordSpec with Matchers with Checkers {
}
}
"created from iterator" in {
check { (a: ByteString) =>
val asIterator = a.iterator
ByteString(asIterator) == a
}
}
"compacting" in {
check { (a: ByteString) =>
val wasCompact = a.isCompact

View file

@ -39,6 +39,11 @@ object ByteString {
*/
def apply(bytes: Byte*): ByteString = CompactByteString(bytes: _*)
/**
* Creates a new ByteString by iterating over bytes.
*/
def apply(bytes: Iterator[Byte]): ByteString = CompactByteString(bytes)
/**
* Creates a new ByteString by converting from integral numbers to bytes.
*/
@ -945,6 +950,14 @@ object CompactByteString {
}
}
/**
* Creates a new CompactByteString by traversing bytes.
*/
def apply(bytes: Iterator[Byte]): CompactByteString = {
if (bytes.isEmpty) empty
else ByteString.ByteString1C(bytes.toArray)
}
/**
* Creates a new CompactByteString by converting from integral numbers to bytes.
*/