OrderedCodec extends ByteCodec with an ordering preservation law.
Law: compare(x, y) ≡ encode(x).compare(encode(y))
This ensures that the encoded byte representation preserves the ordering relationship of the original values. This property is critical for blockchain applications where prefix-free encoding and key ordering matter.
The law states: For all x, y of type A, sign(compare(x, y)) == sign(encode(x).compare(encode(y)))
Where sign(n) is:
- negative if n < 0
- zero if n == 0
- positive if n > 0
Attributes
- Example
-
val codec = OrderedCodec[Long] val x = -100L val y = 200L // The law guarantees: // x.compare(y) < 0 ⟹ encode(x).compare(encode(y)) < 0 - Companion
- object
- Source
- OrderedCodec.scala
- Graph
-
- Supertypes
-
trait Ordering[A]trait PartialOrdering[A]trait Equiv[A]trait Serializabletrait Comparator[A]trait ByteCodec[A]trait ByteEncoder[A]trait ByteDecoder[A]class Objecttrait Matchableclass AnyShow all
Members list
Type members
Inherited classlikes
Attributes
- Inherited from:
- Ordering
- Supertypes
-
class Objecttrait Matchableclass Any
Value members
Concrete methods
Verifies the ordering preservation law for two values.
Verifies the ordering preservation law for two values.
Value parameters
- x
-
first value
- y
-
second value
Attributes
- Returns
-
true if the law holds for this pair
- Source
- OrderedCodec.scala
Inherited methods
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
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
Attributes
- Definition Classes
-
Ordering -> PartialOrdering -> Equiv
- Inherited from:
- Ordering
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
Attributes
- Definition Classes
-
Ordering -> PartialOrdering
- Inherited from:
- Ordering
Attributes
- Definition Classes
-
Ordering -> PartialOrdering
- Inherited from:
- Ordering
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
- Inherited from:
- ByteCodec
- Source
- ByteCodec.scala
Attributes
- Inherited from:
- Ordering
Attributes
- Definition Classes
-
Ordering -> PartialOrdering
- Inherited from:
- Ordering
Attributes
- Definition Classes
-
Ordering -> PartialOrdering
- Inherited from:
- Ordering
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
Attributes
- Inherited from:
- Ordering
Attributes
- Inherited from:
- Ordering
Attributes
- Inherited from:
- Ordering
Attributes
- Inherited from:
- Ordering
Attributes
- Inherited from:
- Ordering
Attributes
- Definition Classes
-
Ordering -> PartialOrdering
- Inherited from:
- Ordering
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Comparator
Attributes
- Inherited from:
- Ordering
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
Attributes
- Inherited from:
- Ordering
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
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
Implicits
Inherited implicits
Attributes
- Inherited from:
- Ordering