Module message

Source
Expand description

Untyped root container for a Cap’n Proto value.

§Notes about type specialization

This module provides TypedReader and TypedBuilder structs which are strongly-typed variants of Reader and Builder.

Code autogenerated by capnpc will have an individual module for each of structures and each of modules will have Owned struct which implements Owned trait.

Example from a real auto-generated file:

pub mod simple_struct {
    #[derive(Copy, Clone)]
    pub struct Owned(());
    impl <'a> ::capnp::traits::Owned<'a> for Owned { type Reader = Reader<'a>; type Builder = Builder<'a>; }
    ....
}

TypedReader and TypedBuilder accept generic type parameter T. This parameter must be a corresponding Owned type which was auto-generated inside the corresponding module.

For example, for auto-generated module crate::test_data::simple_struct you’d supply crate::test_data::simple_struct::Owned type into TypedReader/TypedBuilder

include!(concat!(env!("OUT_DIR"), "/simple_struct_capnp.rs"));

use capnp::message::{self, TypedBuilder, TypedReader};

fn main() {
    let mut builder = TypedBuilder::<simple_struct::Owned>::new_default();
    let mut builder_root = builder.init_root();
    builder_root.set_x(10);
    builder_root.set_y(20);

    let mut buffer = vec![];
    capnp::serialize_packed::write_message(&mut buffer, builder.borrow_inner()).unwrap();

    let reader = capnp::serialize_packed::read_message(buffer.as_slice(), ReaderOptions::new()).unwrap();
    let typed_reader = TypedReader::<_, simple_struct::Owned>::new(reader);

    let reader_root = typed_reader.get().unwrap();
    assert_eq!(reader_root.get_x(), 10);
    assert_eq!(reader_root.get_x(), 20);
}

Structs§

Builder
A container used to build a message.
HeapAllocator
Standard segment allocator. Allocates each segment via alloc::alloc::alloc_zeroed().
Reader
A container used to read a message.
ReaderOptions
Options controlling how data is read.
ScratchSpaceHeapAllocator
An Allocator whose first segment is a backed by a user-provided buffer.
SegmentArray
An array of segments.
SingleSegmentAllocator
An Allocator whose first and only segment is a backed by a user-provided buffer. If the segment fills up, subsequent allocations trigger panics.
TypedBuilder
Stongly typed variant of the Builder
TypedReader
A message reader whose value is known to be of type T. Please see module documentation for more info about reader type specialization.

Enums§

AllocationStrategy

Constants§

DEFAULT_READER_OPTIONS
SUGGESTED_ALLOCATION_STRATEGY
SUGGESTED_FIRST_SEGMENT_WORDS

Traits§

Allocator
An object that allocates memory for a Cap’n Proto message as it is being built.
ReaderSegments
An object that manages the buffers underlying a Cap’n Proto message reader.