ByteCodec

org.sigilaris.core.codec.byte.ByteCodec
See theByteCodec companion object
trait ByteCodec[A] extends ByteDecoder[A], ByteEncoder[A]

Bidirectional byte codec combining encoder and decoder.

ByteCodec extends both ByteEncoder and ByteDecoder, providing a single type class for types that support both encoding and decoding.

Most types have both encoder and decoder instances, so ByteCodec is automatically derived when both are available.

Attributes

See also

ByteEncoder for encoding operations

ByteDecoder for decoding operations

types.md for encoding/decoding rules per type

Companion
object
Source
ByteCodec.scala
Graph
Supertypes
trait ByteEncoder[A]
trait ByteDecoder[A]
class Object
trait Matchable
class Any
Known subtypes
trait OrderedCodec[A]
Self type

Members list

Value members

Concrete methods

def imap[B](to: A => B, from: B => A): ByteCodec[B]

Maps a codec through an isomorphism.

Maps a codec through an isomorphism.

Useful for opaque wrappers whose representation already has a codec.

Type parameters

B

the target type

Value parameters

from

conversion from B to A

to

conversion from A to B

Attributes

Returns

a new ByteCodec for type B

Source
ByteCodec.scala

Inherited methods

def contramap[B](f: B => A): ByteEncoder[B]

Creates a new encoder by applying a function before encoding.

Creates a new encoder by applying a function before encoding.

This is the contravariant map operation for encoders.

Value parameters

f

function to transform input before encoding

Attributes

Returns

new encoder for type B

Example
 case class UserId(value: Long)
 given ByteEncoder[UserId] = ByteEncoder[Long].contramap(_.value)
Inherited from:
ByteEncoder
Source
ByteEncoder.scala
def emap[B](f: A => Either[DecodeFailure, B]): ByteDecoder[B]

Creates a new decoder with validation.

Creates a new decoder with validation.

Value parameters

f

function that may fail with DecodeFailure

Attributes

Returns

new decoder for type B

Example
 val positiveDecoder = ByteDecoder[Long].emap { n =>
   Either.cond(n > 0, n, DecodeFailure("Must be positive"))
 }
Inherited from:
ByteDecoder
Source
ByteDecoder.scala
def flatMap[B](f: A => ByteDecoder[B]): ByteDecoder[B]

Creates a new decoder by chaining decoders.

Creates a new decoder by chaining decoders.

Value parameters

f

function that creates a decoder based on decoded value

Attributes

Returns

new decoder for type B

Inherited from:
ByteDecoder
Source
ByteDecoder.scala
def map[B](f: A => B): ByteDecoder[B]

Creates a new decoder by transforming the decoded value.

Creates a new decoder by transforming the decoded value.

Value parameters

f

function to transform decoded value

Attributes

Returns

new decoder for type B

Inherited from:
ByteDecoder
Source
ByteDecoder.scala
def widen[AA >: A]: ByteDecoder[AA]

Widens the decoder to a supertype.

Widens the decoder to a supertype.

Type parameters

AA

the supertype

Attributes

Returns

a decoder for the wider type

Inherited from:
ByteDecoder
Source
ByteDecoder.scala

Inherited and Abstract methods

def decode(bytes: ByteVector): Either[DecodeFailure, DecodeResult[A]]

Decodes bytes to a value.

Decodes bytes to a value.

Value parameters

bytes

the byte sequence to decode

Attributes

Returns

Either failure or decoded value with remainder

Inherited from:
ByteDecoder
Source
ByteDecoder.scala
def encode(value: A): ByteVector

Encodes a value to a deterministic byte sequence.

Encodes a value to a deterministic byte sequence.

Value parameters

value

the value to encode

Attributes

Returns

the encoded byte sequence

Inherited from:
ByteEncoder
Source
ByteEncoder.scala