This is an automated email from the ASF dual-hosted git repository.

mdedetrich pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-pekko.git


The following commit(s) were added to refs/heads/main by this push:
     new c4e33c3972 Add Scala 2.12 equivalent Iterator apply method for 
ByteString
c4e33c3972 is described below

commit c4e33c39724bd0753f54c384b37f1d7c8843bf2a
Author: Matthew de Detrich <[email protected]>
AuthorDate: Tue Mar 21 13:16:35 2023 +0100

    Add Scala 2.12 equivalent Iterator apply method for ByteString
---
 .../test/scala/org/apache/pekko/util/ByteStringSpec.scala   |  7 +++++++
 .../main/scala-2.12/org/apache/pekko/util/ByteString.scala  | 13 +++++++++++++
 2 files changed, 20 insertions(+)

diff --git 
a/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala 
b/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala
index 78797f5239..9a3b30980c 100644
--- a/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala
+++ b/actor-tests/src/test/scala/org/apache/pekko/util/ByteStringSpec.scala
@@ -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
diff --git a/actor/src/main/scala-2.12/org/apache/pekko/util/ByteString.scala 
b/actor/src/main/scala-2.12/org/apache/pekko/util/ByteString.scala
index 26d2e93c32..c891604f72 100644
--- a/actor/src/main/scala-2.12/org/apache/pekko/util/ByteString.scala
+++ b/actor/src/main/scala-2.12/org/apache/pekko/util/ByteString.scala
@@ -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.
    */


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to