Deconstructor
zio.blocks.schema.binding.Deconstructor
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
- Known subtypes
-
class ConstantDeconstructor[A]
- Self type
Members list
In this article