facet_postcard/lib.rs
1//! Postcard binary format for facet.
2//!
3//! This crate provides serialization and deserialization for the postcard binary format.
4//!
5//! # Serialization
6//!
7//! Serialization supports all types that implement [`facet_core::Facet`]:
8//!
9//! ```
10//! use facet::Facet;
11//! use facet_postcard::to_vec;
12//!
13//! #[derive(Facet)]
14//! struct Point { x: i32, y: i32 }
15//!
16//! let point = Point { x: 10, y: 20 };
17//! let bytes = to_vec(&point).unwrap();
18//! ```
19//!
20//! # Deserialization
21//!
22//! There is a configurable [`Deserializer`] API plus convenience functions:
23//!
24//! - [`from_slice`]: Deserializes into owned types (`T: Facet<'static>`)
25//! - [`from_slice_borrowed`]: Deserializes with zero-copy borrowing from the input buffer
26//! - [`from_slice_with_shape`]: Deserializes into `Value` using runtime shape information
27//! - [`from_slice_into`]: Deserializes into an existing `Partial` (type-erased, owned)
28//! - [`from_slice_into_borrowed`]: Deserializes into an existing `Partial` (type-erased, zero-copy)
29//!
30//! ```
31//! use facet_postcard::from_slice;
32//!
33//! // Postcard encoding: [length=3, true, false, true]
34//! let bytes = &[0x03, 0x01, 0x00, 0x01];
35//! let result: Vec<bool> = from_slice(bytes).unwrap();
36//! assert_eq!(result, vec![true, false, true]);
37//! ```
38//!
39//! Both functions automatically select the best deserialization tier:
40//! - **Tier-2 (Format JIT)**: Fastest path for compatible types (primitives, structs, vecs, simple enums)
41//! - **Tier-0 (Reflection)**: Fallback for all other types (nested enums, complex types)
42//!
43//! This ensures all `Facet` types can be deserialized.
44
45// Note: unsafe code is used for lifetime transmutes in from_slice_into
46// when BORROW=false, mirroring the approach used in facet-json.
47
48extern crate alloc;
49
50mod error;
51mod parser;
52mod raw_postcard;
53mod serialize;
54mod shape_deser;
55
56#[cfg(feature = "jit")]
57pub mod jit;
58
59#[cfg(feature = "axum")]
60mod axum;
61
62#[cfg(feature = "axum")]
63pub use axum::{Postcard, PostcardRejection, PostcardSerializeRejection};
64pub use error::{PostcardError, SerializeError};
65#[cfg(feature = "jit")]
66pub use jit::PostcardJitFormat;
67pub use parser::PostcardParser;
68pub use raw_postcard::{RawPostcard, opaque_encoded_borrowed, opaque_encoded_owned};
69pub use serialize::{
70 ScatterPlan, Segment, Writer, peek_to_scatter_plan, peek_to_vec, to_scatter_plan, to_vec,
71 to_vec_with_shape, to_writer_fallible,
72};
73pub use shape_deser::from_slice_with_shape;
74
75// Re-export DeserializeError for convenience
76pub use facet_format::DeserializeError;
77
78/// Default maximum number of elements allowed in a decoded collection.
79///
80/// This limit applies to postcard length-prefixed collections (lists, maps,
81/// dynamic arrays/objects) and is enforced in both Tier-0 and Tier-2 JIT paths.
82pub const DEFAULT_MAX_COLLECTION_ELEMENTS: u64 = 1 << 24; // 16,777,216
83
84/// Deserialization safety/configuration options.
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub struct DeserializeConfig {
87 max_collection_elements: u64,
88}
89
90impl Default for DeserializeConfig {
91 fn default() -> Self {
92 Self {
93 max_collection_elements: DEFAULT_MAX_COLLECTION_ELEMENTS,
94 }
95 }
96}
97
98impl DeserializeConfig {
99 /// Create default deserialization settings.
100 pub const fn new() -> Self {
101 Self {
102 max_collection_elements: DEFAULT_MAX_COLLECTION_ELEMENTS,
103 }
104 }
105
106 /// Set the maximum number of elements permitted in any decoded collection.
107 pub const fn max_collection_elements(mut self, max_collection_elements: u64) -> Self {
108 self.max_collection_elements = max_collection_elements;
109 self
110 }
111
112 /// Get the configured maximum number of collection elements.
113 pub const fn get_max_collection_elements(self) -> u64 {
114 self.max_collection_elements
115 }
116}
117
118/// Builder-style postcard deserializer.
119///
120/// This single API supports all current entry points:
121/// typed owned/borrowed deserialization, shape-based value deserialization,
122/// and deserialization into existing `Partial` values.
123#[derive(Debug, Clone, Copy)]
124pub struct Deserializer<'input> {
125 input: &'input [u8],
126 config: DeserializeConfig,
127}
128
129impl<'input> Deserializer<'input> {
130 /// Create a deserializer for a postcard byte slice with default settings.
131 pub const fn new(input: &'input [u8]) -> Self {
132 Self {
133 input,
134 config: DeserializeConfig::new(),
135 }
136 }
137
138 /// Create a deserializer with explicit settings.
139 pub const fn with_config(input: &'input [u8], config: DeserializeConfig) -> Self {
140 Self { input, config }
141 }
142
143 /// Replace all deserialization settings.
144 pub const fn config(mut self, config: DeserializeConfig) -> Self {
145 self.config = config;
146 self
147 }
148
149 /// Configure the maximum collection element count.
150 pub const fn max_collection_elements(mut self, max_collection_elements: u64) -> Self {
151 self.config = self.config.max_collection_elements(max_collection_elements);
152 self
153 }
154
155 fn parser(self) -> PostcardParser<'input> {
156 PostcardParser::with_limits(self.input, self.config.get_max_collection_elements())
157 }
158
159 /// Deserialize into an owned typed value.
160 pub fn deserialize<T>(self) -> Result<T, DeserializeError>
161 where
162 T: facet_core::Facet<'static>,
163 {
164 use facet_format::FormatDeserializer;
165 let mut parser = self.parser();
166 let mut de = FormatDeserializer::new_owned(&mut parser);
167 de.deserialize()
168 }
169
170 /// Deserialize into a borrowed typed value.
171 pub fn deserialize_borrowed<'facet, T>(self) -> Result<T, DeserializeError>
172 where
173 T: facet_core::Facet<'facet>,
174 'input: 'facet,
175 {
176 use facet_format::FormatDeserializer;
177 let mut parser = self.parser();
178 let mut de = FormatDeserializer::new(&mut parser);
179 de.deserialize()
180 }
181
182 /// Deserialize into a dynamic `Value` using a runtime shape.
183 pub fn deserialize_with_shape(
184 self,
185 source_shape: &'static facet_core::Shape,
186 ) -> Result<facet_value::Value, DeserializeError> {
187 use facet_format::FormatDeserializer;
188 let mut parser = self.parser();
189 let mut de = FormatDeserializer::new_owned(&mut parser);
190 de.deserialize_with_shape(source_shape)
191 }
192
193 /// Deserialize into an existing owned `Partial`.
194 pub fn deserialize_into<'facet>(
195 self,
196 partial: facet_reflect::Partial<'facet, false>,
197 ) -> Result<facet_reflect::Partial<'facet, false>, DeserializeError> {
198 use facet_format::{FormatDeserializer, MetaSource};
199 let mut parser = self.parser();
200 let mut de = FormatDeserializer::new_owned(&mut parser);
201
202 #[allow(unsafe_code)]
203 let partial: facet_reflect::Partial<'_, false> = unsafe {
204 core::mem::transmute::<
205 facet_reflect::Partial<'facet, false>,
206 facet_reflect::Partial<'_, false>,
207 >(partial)
208 };
209
210 let partial = de.deserialize_into(partial, MetaSource::FromEvents)?;
211
212 #[allow(unsafe_code)]
213 let partial: facet_reflect::Partial<'facet, false> = unsafe {
214 core::mem::transmute::<
215 facet_reflect::Partial<'_, false>,
216 facet_reflect::Partial<'facet, false>,
217 >(partial)
218 };
219
220 Ok(partial)
221 }
222
223 /// Deserialize into an existing borrowed `Partial`.
224 pub fn deserialize_into_borrowed<'facet>(
225 self,
226 partial: facet_reflect::Partial<'facet, true>,
227 ) -> Result<facet_reflect::Partial<'facet, true>, DeserializeError>
228 where
229 'input: 'facet,
230 {
231 use facet_format::{FormatDeserializer, MetaSource};
232 let mut parser = self.parser();
233 let mut de = FormatDeserializer::new(&mut parser);
234 de.deserialize_into(partial, MetaSource::FromEvents)
235 }
236}
237
238/// Deserialize a value from postcard bytes into an owned type.
239///
240/// This is the recommended default for most use cases. The input does not need
241/// to outlive the result, making it suitable for deserializing from temporary
242/// buffers (e.g., HTTP request bodies).
243///
244/// Types containing `&str` or `&[u8]` fields cannot be deserialized with this
245/// function; use `String`/`Vec<u8>` or `Cow<str>`/`Cow<[u8]>` instead. For
246/// zero-copy deserialization into borrowed types, use [`from_slice_borrowed`].
247///
248/// # Example
249///
250/// ```
251/// use facet::Facet;
252/// use facet_postcard::from_slice;
253///
254/// #[derive(Facet, Debug, PartialEq)]
255/// struct Point {
256/// x: i32,
257/// y: i32,
258/// }
259///
260/// // Postcard encoding: [x=10 (zigzag), y=20 (zigzag)]
261/// let bytes = &[0x14, 0x28];
262/// let point: Point = from_slice(bytes).unwrap();
263/// assert_eq!(point.x, 10);
264/// assert_eq!(point.y, 20);
265/// ```
266pub fn from_slice<T>(input: &[u8]) -> Result<T, DeserializeError>
267where
268 T: facet_core::Facet<'static>,
269{
270 Deserializer::new(input).deserialize()
271}
272
273/// Deserialize a value from postcard bytes, allowing zero-copy borrowing.
274///
275/// This variant requires the input to outlive the result (`'input: 'facet`),
276/// enabling zero-copy deserialization of byte slices as `&[u8]` or `Cow<[u8]>`.
277///
278/// Use this when you need maximum performance and can guarantee the input
279/// buffer outlives the deserialized value. For most use cases, prefer
280/// [`from_slice`] which doesn't have lifetime requirements.
281///
282/// # Example
283///
284/// ```
285/// use facet::Facet;
286/// use facet_postcard::from_slice_borrowed;
287///
288/// #[derive(Facet, Debug, PartialEq)]
289/// struct Message<'a> {
290/// id: u32,
291/// data: &'a [u8],
292/// }
293///
294/// // Postcard encoding: [id=1, data_len=3, 0xAB, 0xCD, 0xEF]
295/// let bytes = &[0x01, 0x03, 0xAB, 0xCD, 0xEF];
296/// let msg: Message = from_slice_borrowed(bytes).unwrap();
297/// assert_eq!(msg.id, 1);
298/// assert_eq!(msg.data, &[0xAB, 0xCD, 0xEF]);
299/// ```
300pub fn from_slice_borrowed<'input, 'facet, T>(input: &'input [u8]) -> Result<T, DeserializeError>
301where
302 T: facet_core::Facet<'facet>,
303 'input: 'facet,
304{
305 Deserializer::new(input).deserialize_borrowed()
306}
307
308/// Deserialize postcard bytes into an existing Partial.
309///
310/// This is useful for reflection-based deserialization where you don't have
311/// a concrete type `T` at compile time, only its Shape metadata. The Partial
312/// must already be allocated for the target type.
313///
314/// This version produces owned strings (no borrowing from input).
315///
316/// # Example
317///
318/// ```
319/// use facet::Facet;
320/// use facet_postcard::from_slice_into;
321/// use facet_reflect::Partial;
322///
323/// #[derive(Facet, Debug, PartialEq)]
324/// struct Point {
325/// x: i32,
326/// y: i32,
327/// }
328///
329/// // Postcard encoding: [x=10 (zigzag), y=20 (zigzag)]
330/// let bytes = &[0x14, 0x28];
331/// let partial = Partial::alloc_owned::<Point>().unwrap();
332/// let partial = from_slice_into(bytes, partial).unwrap();
333/// let value = partial.build().unwrap();
334/// let point: Point = value.materialize().unwrap();
335/// assert_eq!(point.x, 10);
336/// assert_eq!(point.y, 20);
337/// ```
338pub fn from_slice_into<'facet>(
339 input: &[u8],
340 partial: facet_reflect::Partial<'facet, false>,
341) -> Result<facet_reflect::Partial<'facet, false>, DeserializeError> {
342 Deserializer::new(input).deserialize_into(partial)
343}
344
345/// Deserialize postcard bytes into an existing Partial, allowing zero-copy borrowing.
346///
347/// This variant requires the input to outlive the Partial's lifetime (`'input: 'facet`),
348/// enabling zero-copy deserialization of byte slices as `&[u8]` or `Cow<[u8]>`.
349///
350/// This is useful for reflection-based deserialization where you don't have
351/// a concrete type `T` at compile time, only its Shape metadata.
352///
353/// # Example
354///
355/// ```
356/// use facet::Facet;
357/// use facet_postcard::from_slice_into_borrowed;
358/// use facet_reflect::Partial;
359///
360/// #[derive(Facet, Debug, PartialEq)]
361/// struct Message<'a> {
362/// id: u32,
363/// data: &'a [u8],
364/// }
365///
366/// // Postcard encoding: [id=1, data_len=3, 0xAB, 0xCD, 0xEF]
367/// let bytes = &[0x01, 0x03, 0xAB, 0xCD, 0xEF];
368/// let partial = Partial::alloc::<Message>().unwrap();
369/// let partial = from_slice_into_borrowed(bytes, partial).unwrap();
370/// let value = partial.build().unwrap();
371/// let msg: Message = value.materialize().unwrap();
372/// assert_eq!(msg.id, 1);
373/// assert_eq!(msg.data, &[0xAB, 0xCD, 0xEF]);
374/// ```
375pub fn from_slice_into_borrowed<'input, 'facet>(
376 input: &'input [u8],
377 partial: facet_reflect::Partial<'facet, true>,
378) -> Result<facet_reflect::Partial<'facet, true>, DeserializeError>
379where
380 'input: 'facet,
381{
382 Deserializer::new(input).deserialize_into_borrowed(partial)
383}