Deconstructor

zio.blocks.schema.binding.Deconstructor
abstract class Deconstructor[-A]

A Deconstructor writes the fields of a value of type A into Registers: it is the deconstruction half of a Binding.Record. To encode a record, a codec calls deconstruct and then reads each field from the registers, at the positions that Reflect.Record#registers gives. Writing the fields into registers, instead of into a tuple or an array of boxed values, keeps primitive fields unboxed.

deconstruct writes at positions counted from its offset argument, with the same layout that the record's Constructor reads:

import zio.blocks.schema.binding._
import zio.blocks.schema.binding.RegisterOffset.RegisterOffset

case class Book(title: String, year: Int)

val bookDeconstructor: Deconstructor[Book] = new Deconstructor[Book] {
 def usedRegisters: RegisterOffset = RegisterOffset(objects = 1, ints = 1)

 def deconstruct(out: Registers, offset: RegisterOffset, in: Book): Unit = {
   out.setObject(offset, in.title)
   out.setInt(offset + RegisterOffset(objects = 1), in.year)
 }
}

val registers = Registers(bookDeconstructor.usedRegisters)
bookDeconstructor.deconstruct(registers, RegisterOffset.Zero, Book("Zionomicon", 2021))
val title = registers.getObject(RegisterOffset.Zero)     // "Zionomicon"
val year  = registers.getInt(RegisterOffset(objects = 1)) // 2021

Type parameters

A

the type of the values it takes apart

Attributes

See also
Source
Deconstructor.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
Self type

Members list

Value members

Abstract methods

def deconstruct(out: Registers, offset: RegisterOffset, in: A): Unit

Writes the fields of in into out, at positions counted from offset.

Writes the fields of in into out, at positions counted from offset.

Attributes

Source
Deconstructor.scala

The registers that deconstruct writes, counted from its offset: the number of object slots and the number of bytes. Callers size their Registers with it.

The registers that deconstruct writes, counted from its offset: the number of object slots and the number of bytes. Callers size their Registers with it.

Attributes

Source
Deconstructor.scala