Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 9dec8f3
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion core/shared/src/main/scala/fs2/Chunk.scala
Expand Up @@ -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,
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit 9dec8f3

Please sign in to comment.