Skip to content
Permalink
Browse files

Merge pull request #1514 from SystemFw/chunk-to-bitvector

Add conversions to BitVector and ByteVector
  • Loading branch information...
mpilquist committed Jun 17, 2019
2 parents 5e64fa4 + d9ebb57 commit 9dec8f3219ddc45c3c5144885a390c2873ed36d1
Showing with 19 additions and 1 deletion.
  1. +19 −1 core/shared/src/main/scala/fs2/Chunk.scala
@@ -4,7 +4,7 @@ import scala.annotation.tailrec
import scala.collection.immutable.{Queue => SQueue}
import scala.collection.{IndexedSeq => GIndexedSeq, Seq => GSeq}
import scala.reflect.ClassTag
import scodec.bits.ByteVector
import scodec.bits.{BitVector, ByteVector}
import java.nio.{
Buffer => JBuffer,
ByteBuffer => JByteBuffer,
@@ -397,6 +397,24 @@ abstract class Chunk[+O] extends Serializable { self =>
buf.result
}

/** Converts this chunk to a scodec-bits ByteVector. */
def toByteVector[B >: O](implicit ev: B =:= Byte): ByteVector = {
val _ = ev // convince scalac that ev is used
this match {
case c: Chunk.ByteVectorChunk => c.toByteVector
case other => ByteVector.view(other.asInstanceOf[Chunk[Byte]].toArray)
}
}

/** Converts this chunk to a scodec-bits BitVector. */
def toBitVector[B >: O](implicit ev: B =:= Byte): BitVector = {
val _ = ev // convince scalac that ev is used
this match {
case c: Chunk.ByteVectorChunk => c.toByteVector.bits
case other => BitVector.view(other.asInstanceOf[Chunk[Byte]].toArray)
}
}

/**
* Returns true if this chunk is known to have elements of type `B`.
* This is determined by checking if the chunk type mixes in `Chunk.KnownElementType`.

0 comments on commit 9dec8f3

Please sign in to comment.
You can’t perform that action at this time.