coffi/docs/articles/03-Builtin-Types.md
Joshua Suskalo 98cbf350f9
Revert several commits adding crossreferences in docs
Revert "Add functions header in builtin types"

This reverts commit 8c7a77aa92.

Revert "Add cross-link for tagged union type mention"

This reverts commit 5c1b2d3f7c.

Revert "Add section links to headers in docs"

This reverts commit 5175fdc5b3.
2024-11-22 10:16:34 -05:00

1.4 KiB

Built-in Types WIP

Primitives

Arrays

Pointers

Structs

Enums

Flagsets

Unions

Unions in coffi are rather limited. They can be serialized, but not deserialized without external information.

[::mem/union
 #{::mem/float ::mem/double}
 :dispatch #(cond
             (float? %) ::mem/float
             (double? %) ::mem/double)]

This is a minimal union in coffi. If the :dispatch keyword argument is not passed, then the union cannot be serialized, as coffi would not know which type to serialize the values as. In the example with a tagged union, a dispatch function was not provided because the type was only used for the native layout.

In addition to a dispatch function, when serializing a union an extract function may also be provided. In the case of the value in the tagged union from before, it could be represented for serialization purposes like so:

[::mem/union
 #{::mem/int ::mem/c-string}
 :dispatch #(case (first %)
              :ok ::mem/int
              :err ::mem/c-string)
 :extract second]

This union however would not include the tag when serialized.

If a union is deserialized, then all that coffi does is to allocate a new segment of the appropriate size with an implicit arena so that it may later be garbage collected, and copies the data from the source segment into it. It's up to the user to call deserialize-from on that segment with the appropriate type.

Raw Types