2024-10-04 20:35:05 +00:00
|
|
|
# Built-in Types **WIP**
|
2024-10-04 20:27:06 +00:00
|
|
|
|
2024-11-22 15:01:52 +00:00
|
|
|
### TODO Primitives {#primitives}
|
2024-10-04 20:27:06 +00:00
|
|
|
|
2024-11-22 15:01:52 +00:00
|
|
|
### TODO Arrays {#arrays}
|
2024-10-04 20:27:06 +00:00
|
|
|
|
2024-11-22 15:01:52 +00:00
|
|
|
### TODO Pointers {#pointers}
|
2024-10-04 20:27:06 +00:00
|
|
|
|
2024-11-22 15:01:52 +00:00
|
|
|
### TODO Structs {#structs}
|
2024-10-04 20:27:06 +00:00
|
|
|
|
2024-11-22 15:01:52 +00:00
|
|
|
### TODO Enums {#enums}
|
2024-10-04 20:27:06 +00:00
|
|
|
|
2024-11-22 15:01:52 +00:00
|
|
|
### TODO Flagsets {#flagsets}
|
2024-10-04 20:27:06 +00:00
|
|
|
|
2024-11-22 15:01:52 +00:00
|
|
|
### Unions {#unions}
|
2024-10-04 20:27:06 +00:00
|
|
|
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
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```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.
|
|
|
|
|
|
2024-11-22 15:01:52 +00:00
|
|
|
### TODO Raw Types {#raw-types}
|