1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2026 Ethan Wu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
//! Serialization families for Consortium IPC and TEE parameter conversion.
//!
//! A Consortium link carries typed messages, but the wire carries bytes. This
//! crate is the seam: [`Codec`] names a serialization *family*, [`CodecFor<T>`]
//! says that family can handle a particular message type, and everything above —
//! `consortium_ipc::Channel`, `consortium_tee::TeeParam` — is generic over it.
//!
//! # Architecture
//!
//! The trait pair is split so that a family's error type is declared once:
//!
//! ```text
//! Codec one impl per family; declares Error
//! └── CodecFor<T> one impl per (family, message type); encode/decode
//! ```
//!
//! The load-bearing detail is that [`CodecFor::Decoded`] is an associated *type
//! constructor*, not `T`:
//!
//! | Codec | `Decoded<'buf>` | Cost of a `recv` |
//! | --------------- | -------------------- | --------------------------------- |
//! | `PostcardCodec` | `T` | deserializes into an owned value |
//! | `ProstCodec` | `T` | deserializes into an owned value |
//! | `RkyvCodec` | `&'buf T::Archived` | validates and borrows the buffer |
//!
//! Callers must therefore not assume `decode` produces an owned `T`.
//! `consortium_ipc::ReceivedMessage` wraps `Decoded<'buf>` and `Deref`s to `T`
//! where the codec allows it, so call sites written against it survive a codec
//! swap unchanged.
//!
//! Encoding is always into a caller-supplied `&mut [u8]` and returns the byte
//! count. There is no allocation in the trait signature, which is what lets the
//! same codec serve a `no_std` firmware channel and a Linux one.
//!
//! # Features
//!
//! `no_std` by default, and **there is no default codec** — a consumer names the
//! family it wants, so firmware does not pay for one it never calls.
//!
//! | Feature | Exposes |
//! | ---------- | ------------------------------------------------ |
//! | `postcard` | `PostcardCodec` (serde, compact, owned decode) |
//! | `prost` | `ProstCodec` (protobuf, owned decode) |
//! | `rkyv` | `RkyvCodec`, `TrustedArchive` (zero-copy) |
//! | `alloc` | heap-backed paths, including validated rkyv |
//! | `std` | `alloc` plus `tracing`-routed diagnostics |
//! | `defmt` | routes the same diagnostics to `defmt` |
//!
//! # Example
//!
//! ```rust,ignore
//! use consortium_codec::{CodecFor, PostcardCodec};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize)]
//! struct Telemetry { sequence: u32, ready: bool }
//!
//! let mut buf = [0u8; 32];
//! let n = PostcardCodec::encode(&Telemetry { sequence: 7, ready: true }, &mut buf)?;
//! let back: Telemetry = <PostcardCodec as CodecFor<Telemetry>>::decode(&buf[..n])?;
//! ```
//!
//! # Implementing a codec
//!
//! Add a unit struct, `impl Codec` for it to declare `Error`, then
//! `impl<T> CodecFor<T> for MyCodec where T: <your serialization bound>`. Set
//! `Decoded<'buf>` to `T` for an owning codec or to a borrow of `'buf` for a
//! zero-copy one. Encode must report the exact byte count written and must fail
//! rather than truncate when `buf` is too small — a transport sends `buf[..n]`
//! verbatim.
pub
pub
pub
pub use PostcardCodec;
pub use ProstCodec;
pub use RkyvCodec;
/// A serialization family.
///
/// Implemented once per family (not per message type) so that the family's error
/// type is declared in one place and [`CodecFor<T>`] can stay a blanket impl over
/// whatever bound the underlying library needs. Codecs are zero-sized markers;
/// they are named as type parameters and never instantiated.
/// A [`Codec`] that can round-trip the message type `T`.
///
/// Both directions work against a caller-supplied slice, so the trait is usable
/// from `no_std` firmware with a `static` scratch buffer.