Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversions to BitVector and ByteVector #1514

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion core/shared/src/main/scala/fs2/Chunk.scala
Original file line number Diff line number Diff line change
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