API reference¶
use capnp exposes the serialization API: everything below except the C
ABI (capnp_cabi) and RPC (capnp_rpc, capnp_posix,
capnp_rpc_transport) sections, which are separate modules used
explicitly. Every fallible operation returns an err code; CAPNP_OK (0)
means success. Byte buffers are integer(int8) arrays, 0-based by
convention. Word offsets and lengths are integer(int64).
Error codes (capnp_kinds)¶
Code |
Meaning |
|---|---|
|
success |
|
access outside a segment |
|
pointer kind mismatch (e.g. list where struct expected) |
|
nesting depth limit hit |
|
traversal word limit hit |
|
builder allocation failure |
|
malformed segment table |
|
malformed packed stream |
|
invalid argument |
|
bad segment id in a far pointer |
|
file I/O failure |
Guards default to CAPNP_DEFAULT_TRAVERSAL_WORDS (8 Mi words) and
CAPNP_DEFAULT_DEPTH_LIMIT (64); both are per-message and settable through
the deserialize entry points.
Element-size codes for capnp_new_list: CAPNP_SZ_VOID (0), _BIT,
_BYTE, _TWO, _FOUR, _EIGHT, _PTR, _COMPOSITE (7). Handle kinds:
CAPNP_PK_NULL, _STRUCT, _LIST, _CAP.
Messages (capnp_arena)¶
Handles hold a pointer to their message, so message variables must carry
the target attribute.
Procedure |
Signature |
Notes |
|---|---|---|
|
|
fresh builder; reserves the zeroed root word |
|
|
releases owned segments; safe on views |
Objects and pointers (capnp_message)¶
capnp_ptr_t is a value-type handle: kind, position, and struct/list
geometry. p%kind == CAPNP_PK_NULL denotes a null pointer; readers treat
null structs and lists as empty/all-defaults, as the C++ implementation
does.
Procedure |
Signature |
Notes |
|---|---|---|
|
|
resolve the root pointer |
|
|
write the root pointer |
|
|
|
|
|
primitive/pointer lists |
|
|
writes the tag word |
|
|
read pointer slot; downgrades composite lists when asked for pointers |
|
|
same-segment relative, cross-segment far, cross-message deep copy |
|
|
|
|
|
element of a PTR list |
|
|
composite element; upgrades primitive lists to struct views |
|
|
recursive deep copy into another message |
|
|
orphan: zero the slot, return the object; re-link with |
|
|
words reachable from |
Field accessors (capnp_message)¶
Scalar accessors take byte offsets into the data section (bit offsets for
bool) and an optional default that is XORed on the wire, so generated
code passes declared defaults straight through:
capnp_get_i8/i16/i32/i64 (p, byte_off [, default]),capnp_set_i8/... (p, byte_off, v, err [, default])unsigned views widen the result kind:
u8 -> int16,u16 -> int32,u32 -> int64;capnp_get_u64/set_u64alias the i64 wire opscapnp_get_f32/f64,capnp_set_f32/f64(bit-pattern XOR defaults)capnp_get_bool (p, bit_off [, default]),capnp_set_bool
List element accessors mirror them: capnp_list_get_<T> (l, i, err) /
capnp_list_set_<T> (l, i, v, err) for i8..i64, f32, f64, plus
capnp_list_get_bool/set_bool for bit lists. Whole-list transfers
(capn_getv/capn_setv parity): capnp_list_get_all_<T> (l, arr, err)
allocates and fills arr; capnp_list_set_all_<T> requires
size(arr) equal to the list length. Element reads also work through
schema-evolution views (primitive list read as struct, composite list read
as primitive via field @0).
Text and Data¶
Procedure |
Signature |
Notes |
|---|---|---|
|
|
null slot yields |
|
|
writes NUL-terminated byte list |
|
|
length without copying, NUL excluded |
|
|
|
|
|
allocates and copies |
|
|
|
|
|
zero-copy pointer slice into the message segment |
|
|
zero-copy character bytes, NUL excluded |
Unions (capnp_union)¶
Procedure |
Signature |
Notes |
|---|---|---|
|
|
discriminant at 16-bit offset |
|
|
generated setters call this before writing the member |
Serialization (capnp_serialize, capnp_stream)¶
Procedure |
Signature |
Notes |
|---|---|---|
|
|
segment table + segments |
|
|
copies segments; frees |
|
|
zero-copy: segments alias the caller’s |
|
as above |
packed framing |
|
|
raw byte I/O |
|
|
framed message file I/O |
|
|
|
|
|
one framed message from an open stream unit; back-to-back messages read in sequence |
|
|
same for packed streams (C++ |
Packed codec (capnp_packed)¶
Procedure |
Signature |
Notes |
|---|---|---|
|
|
whole buffers |
|
|
incremental; chunks may split anywhere |
|
|
incremental; byte-identical to |
Canonical form (capnp_canonical)¶
capnp_canonicalize (msg, bytes, err) produces the canonical single
segment (no segment table): preorder layout, trailing-zero truncation of
data sections, uniform composite-element trimming. Byte-compatible with
capnp convert binary:canonical.
Wire-level helpers (capnp_endian, capnp_pointer)¶
cp_get_*/cp_put_* compose little-endian scalars from individual bytes
(endianness-independent by construction); cp_f32_bits/cp_bits_f32 and
the f64 pair convert between reals and bit patterns. wp_* build and field
raw pointer words; these exist for tests and the code generator, generated
code does not need them.
C ABI (capnp_cabi)¶
bind(c) entry points named cabi_* mirror c-capnproto’s capn_*
surface over pooled integer handles: builder lifecycle, struct/list
allocation (composite and primitive), pointer wiring, scalar and text/data
accessors, union discriminants, serialize/deserialize (flat and packed),
and canonicalization. See interop/README.md and the declarations at the
top of interop/golden_master.c.
RPC (capnp_rpc, capnp_posix, capnp_rpc_transport)¶
Two-party RPC at level 1, single-threaded and message-driven. The
protocol layer (rpc_capnp, rpc_twoparty_capnp) is generated by
capnpc-fortran from the vendored rpc.capnp schemas.
Procedure |
Signature |
Notes |
|---|---|---|
|
|
|
|
|
half-closes and frees tables |
|
|
returns a pipeline cap, usable immediately |
|
|
fill |
|
|
|
|
|
promise pipelining into unreturned results |
|
|
pumps until the question returns |
|
|
|
|
|
settles a result capability into an import |
|
|
|
|
|
|
|
|
handle exactly one incoming message (servers loop on this) |
|
|
stage a capability in a dispatch’s results |
|
|
capability pointer for content slots |
|
|
|
|
|
send without waiting; blocks only to retire the oldest call when full |
|
|
drain the window; first failure wins, later sends fail fast |
Capability implementations extend rpc_server_t and implement
dispatch(ctx, err); ctx carries interface_id, method_id, the
resolved params content, and the results payload. Answering
RPC_PERSISTENT_IFACE / RPC_PERSISTENT_SAVE opts a capability into
level 2 persistence with application-defined SturdyRefs. Level 3/4
messages (provide/accept/join) are answered with Message.unimplemented
per the spec, matching capnp-C++. capnp_posix provides the socket
surface (socketpair, TCP listen/accept/connect, poll) as pure
iso_c_binding interfaces into libc.
Code generator (capnpc-fortran)¶
Run as a capnp plugin: capnp compile -o <path-to-capnpc-fortran> file.capnp, one module per schema file (<file>_capnp.f90). Generated
surface per struct Foo: foo_t handle type, FOO_DWORDS/FOO_PWORDS,
foo_new/foo_new_root/foo_read_root, per-field
<field>_get/<field>_set (scalars carry declared defaults),
<field>_init for pointer fields, _get_elem/_set_elem for
List(Text)/List(Data), a typed _get_elem handle accessor for
struct lists, <union>_which plus <PFX>_<MEMBER>_TAG
constants, group _select setters, enum <TYPE>_<MEMBER> constants, and
constants (scalar parameters; Data as byte arrays; struct/list consts as
accessor functions over embedded blobs).
Interfaces emit an INTERFACE_ID parameter, a <iface>_client_t handle,
per-method _begin/_wait helpers over the vat, and an abstract
<iface>_server_t whose generated dispatch routes method ordinals to
deferred typed procedures. -> stream methods pair the generated
_begin with rpc_stream_send/rpc_stream_finish.
Branded generic uses (Box(Text)) produce brand-resolved
instantiations: a box_text_t handle plus the generic’s accessors with
type parameters substituted by their bindings (box_text_value_get
takes text). Substitution reaches list elements (List(Box(Text))
elements come back as box_text_t via _get_elem), list bindings
(Box(List(Text)) gets the typed List(Text) surface including
element helpers), and brands nested inside another generic (Box(T)
fields of Nest(T) settle through the instantiation’s own bindings).
Unbound parameters keep AnyPointer accessors. Interface-typed struct
fields emit capability-slot pointer accessors for use with
rpc_make_cap_ptr/rpc_result_cap.