2024-10-04 20:35:05 +00:00
|
|
|
# Built-in Types **WIP**
|
2024-10-04 20:27:06 +00:00
|
|
|
|
|
|
|
|
### Primitives
|
|
|
|
|
|
|
|
|
|
### Arrays
|
|
|
|
|
|
|
|
|
|
### Pointers
|
|
|
|
|
|
|
|
|
|
### Structs
|
|
|
|
|
|
|
|
|
|
### Enums
|
|
|
|
|
|
|
|
|
|
### Flagsets
|
|
|
|
|
|
2024-11-22 15:15:52 +00:00
|
|
|
### Functions
|
|
|
|
|
|
2024-10-04 20:27:06 +00:00
|
|
|
### Unions
|
|
|
|
|
Unions in coffi are rather limited. They can be serialized, but not deserialized
|
|
|
|
|
without external information.
|
|
|
|
|
|
|
|
|
|
```clojure
|
|
|
|
|
[::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
|
2024-11-22 15:14:52 +00:00
|
|
|
to serialize the values as. In [the example with a tagged
|
|
|
|
|
union](04-Custom-Types.md#tagged-union), a dispatch function was not provided
|
|
|
|
|
because the type was only used for the native layout.
|
2024-10-04 20:27:06 +00:00
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```clojure
|
|
|
|
|
[::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
|