Skip to content

The CUSTOM type

j3pic edited this page Jun 2, 2018 · 4 revisions

Synopsis

(read-binary-type '(custom &key (lisp-type t) reader writer) stream) align element-align)

Description

The CUSTOM type allows you to explicitly provide code to read and write a datum, and also to optionally declare what Lisp type the resulting value will be.

The reader is a function that accepts a STREAM argument. It must return two values:

  1. The fully decoded result of reading from the stream.
  2. The number of bytes read.

The writer is a function that accepts the lambda-list (object stream), where object is the object to be written, and stream is the stream to be written to.

In DEFBINARY, CUSTOM readers and writers are closures.

If a custom type appears in a DEFBINARY definition and its reader and/or writer are lambda expressions, then those lambdas are closures created in a scope where the slots read so far (or all the slots for the writer) are bound to the values that were read or written. You can setf these bindings, and the changes will be reflected in the struct.