hyprstream_rpc/capnp/traits.rs
1//! Core serialization traits for Cap'n Proto.
2
3use anyhow::Result;
4
5/// Trait for serializing Rust types to Cap'n Proto.
6///
7/// Types implementing this trait can be written to a Cap'n Proto builder.
8/// Use `#[derive(ToCapnp)]` to generate implementations automatically.
9///
10/// # Example
11///
12/// ```ignore
13/// use hyprstream_rpc::prelude::*;
14///
15/// #[derive(ToCapnp)]
16/// #[capnp(my_schema_capnp::my_request)]
17/// pub struct MyRequest {
18/// pub name: String,
19/// pub count: u32,
20/// }
21/// ```
22pub trait ToCapnp {
23 /// The Cap'n Proto builder type for this struct.
24 type Builder<'a>;
25
26 /// Write this struct's fields to the builder.
27 fn write_to(&self, builder: &mut Self::Builder<'_>);
28}
29
30/// Trait for deserializing Cap'n Proto to Rust types.
31///
32/// Types implementing this trait can be read from a Cap'n Proto reader.
33/// Use `#[derive(FromCapnp)]` to generate implementations automatically.
34///
35/// # Example
36///
37/// ```ignore
38/// use hyprstream_rpc::prelude::*;
39///
40/// #[derive(FromCapnp)]
41/// #[capnp(my_schema_capnp::my_response)]
42/// pub struct MyResponse {
43/// pub result: String,
44/// pub success: bool,
45/// }
46/// ```
47pub trait FromCapnp: Sized {
48 /// The Cap'n Proto reader type for this struct.
49 type Reader<'a>;
50
51 /// Read this struct's fields from the reader.
52 fn read_from(reader: Self::Reader<'_>) -> Result<Self>;
53}