Module capnp::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"), "/"));

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

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

    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);





  • An object that allocates memory for a Cap’n Proto message as it is being built. Users of capnproto-rust who wish to provide memory in non-standard ways should implement this trait. Objects implementing this trait are intended to be wrapped by capnp::private::BuilderArena, which handles calling the methods at the appropriate times, including calling deallocate_segment() on drop.
  • An object that manages the buffers underlying a Cap’n Proto message reader.