facet_msgpack/lib.rs
1//! MsgPack binary format for facet.
2//!
3//! This crate provides serialization and deserialization for the MessagePack binary format.
4//!
5//! # Serialization
6//!
7//! ```
8//! use facet::Facet;
9//! use facet_msgpack::to_vec;
10//!
11//! #[derive(Facet)]
12//! struct Point { x: i32, y: i32 }
13//!
14//! let point = Point { x: 10, y: 20 };
15//! let bytes = to_vec(&point).unwrap();
16//! ```
17//!
18//! # Deserialization
19//!
20//! There are two deserialization functions:
21//!
22//! - [`from_slice`]: Deserializes into owned types (`T: Facet<'static>`)
23//! - [`from_slice_borrowed`]: Deserializes with zero-copy borrowing from the input buffer
24//! - [`from_slice_into`]: Deserializes into an existing `Partial` (type-erased, owned)
25//! - [`from_slice_into_borrowed`]: Deserializes into an existing `Partial` (type-erased, zero-copy)
26//!
27//! ```
28//! use facet::Facet;
29//! use facet_msgpack::from_slice;
30//!
31//! #[derive(Facet, Debug, PartialEq)]
32//! struct Point { x: i32, y: i32 }
33//!
34//! // MsgPack encoding of {"x": 10, "y": 20}
35//! let bytes = &[0x82, 0xa1, b'x', 0x0a, 0xa1, b'y', 0x14];
36//! let point: Point = from_slice(bytes).unwrap();
37//! assert_eq!(point.x, 10);
38//! assert_eq!(point.y, 20);
39//! ```
40//!
41//! Both functions use Tier-2 JIT for compatible types (when the `jit` feature is enabled),
42//! with automatic fallback to Tier-0 reflection for all other types.
43
44// Note: unsafe code is used for lifetime transmutes in from_slice_into
45// when BORROW=false, mirroring the approach used in facet-json.
46
47extern crate alloc;
48
49mod error;
50mod parser;
51mod serializer;
52
53#[cfg(feature = "jit")]
54pub mod jit;
55
56#[cfg(feature = "axum")]
57mod axum;
58
59pub use error::MsgPackError;
60
61#[cfg(feature = "axum")]
62pub use axum::{MsgPack, MsgPackRejection, MsgPackSerializeRejection};
63#[cfg(feature = "jit")]
64pub use jit::MsgPackJitFormat;
65pub use parser::MsgPackParser;
66pub use serializer::{MsgPackSerializeError, MsgPackSerializer, to_vec, to_writer};
67
68// Re-export DeserializeError for convenience
69pub use facet_format::DeserializeError;
70
71/// Deserialize a value from MsgPack bytes into an owned type.
72///
73/// This is the recommended default for most use cases. The input does not need
74/// to outlive the result, making it suitable for deserializing from temporary
75/// buffers (e.g., HTTP request bodies).
76///
77/// Types containing `&str` or `&[u8]` fields cannot be deserialized with this
78/// function; use `String`/`Vec<u8>` or `Cow<str>`/`Cow<[u8]>` instead. For
79/// zero-copy deserialization into borrowed types, use [`from_slice_borrowed`].
80///
81/// # Example
82///
83/// ```
84/// use facet::Facet;
85/// use facet_msgpack::from_slice;
86///
87/// #[derive(Facet, Debug, PartialEq)]
88/// struct Point {
89/// x: i32,
90/// y: i32,
91/// }
92///
93/// // MsgPack encoding of {"x": 10, "y": 20}
94/// let bytes = &[0x82, 0xa1, b'x', 0x0a, 0xa1, b'y', 0x14];
95/// let point: Point = from_slice(bytes).unwrap();
96/// assert_eq!(point.x, 10);
97/// assert_eq!(point.y, 20);
98/// ```
99pub fn from_slice<T>(input: &[u8]) -> Result<T, DeserializeError>
100where
101 T: facet_core::Facet<'static>,
102{
103 use facet_format::FormatDeserializer;
104 let mut parser = MsgPackParser::new(input);
105 let mut de = FormatDeserializer::new_owned(&mut parser);
106 de.deserialize()
107}
108
109/// Deserialize a value from MsgPack bytes, allowing zero-copy borrowing.
110///
111/// This variant requires the input to outlive the result (`'input: 'facet`),
112/// enabling zero-copy deserialization of byte slices as `&[u8]` or `Cow<[u8]>`.
113///
114/// Use this when you need maximum performance and can guarantee the input
115/// buffer outlives the deserialized value. For most use cases, prefer
116/// [`from_slice`] which doesn't have lifetime requirements.
117///
118/// # Example
119///
120/// ```
121/// use facet::Facet;
122/// use facet_msgpack::from_slice_borrowed;
123///
124/// #[derive(Facet, Debug, PartialEq)]
125/// struct Message<'a> {
126/// id: u32,
127/// data: &'a [u8],
128/// }
129///
130/// // MsgPack encoding of {"id": 1, "data": <bin8 with 3 bytes>}
131/// let bytes = &[0x82, 0xa2, b'i', b'd', 0x01, 0xa4, b'd', b'a', b't', b'a', 0xc4, 0x03, 0xAB, 0xCD, 0xEF];
132/// let msg: Message = from_slice_borrowed(bytes).unwrap();
133/// assert_eq!(msg.id, 1);
134/// assert_eq!(msg.data, &[0xAB, 0xCD, 0xEF]);
135/// ```
136pub fn from_slice_borrowed<'input, 'facet, T>(input: &'input [u8]) -> Result<T, DeserializeError>
137where
138 T: facet_core::Facet<'facet>,
139 'input: 'facet,
140{
141 use facet_format::FormatDeserializer;
142 let mut parser = MsgPackParser::new(input);
143 let mut de = FormatDeserializer::new(&mut parser);
144 de.deserialize()
145}
146
147/// Deserialize MsgPack bytes into an existing Partial.
148///
149/// This is useful for reflection-based deserialization where you don't have
150/// a concrete type `T` at compile time, only its Shape metadata. The Partial
151/// must already be allocated for the target type.
152///
153/// This version produces owned strings (no borrowing from input).
154///
155/// # Example
156///
157/// ```
158/// use facet::Facet;
159/// use facet_msgpack::from_slice_into;
160/// use facet_reflect::Partial;
161///
162/// #[derive(Facet, Debug, PartialEq)]
163/// struct Point {
164/// x: i32,
165/// y: i32,
166/// }
167///
168/// // MsgPack encoding of {"x": 10, "y": 20}
169/// let bytes = &[0x82, 0xa1, b'x', 0x0a, 0xa1, b'y', 0x14];
170/// let partial = Partial::alloc_owned::<Point>().unwrap();
171/// let partial = from_slice_into(bytes, partial).unwrap();
172/// let value = partial.build().unwrap();
173/// let point: Point = value.materialize().unwrap();
174/// assert_eq!(point.x, 10);
175/// assert_eq!(point.y, 20);
176/// ```
177pub fn from_slice_into<'facet>(
178 input: &[u8],
179 partial: facet_reflect::Partial<'facet, false>,
180) -> Result<facet_reflect::Partial<'facet, false>, DeserializeError> {
181 use facet_format::{FormatDeserializer, MetaSource};
182 let mut parser = MsgPackParser::new(input);
183 let mut de = FormatDeserializer::new_owned(&mut parser);
184
185 // SAFETY: The deserializer expects Partial<'input, false> where 'input is the
186 // lifetime of the MsgPack bytes. Since BORROW=false, no data is borrowed from the
187 // input, so the actual 'facet lifetime of the Partial is independent of 'input.
188 // We transmute to satisfy the type system, then transmute back after deserialization.
189 #[allow(unsafe_code)]
190 let partial: facet_reflect::Partial<'_, false> = unsafe {
191 core::mem::transmute::<
192 facet_reflect::Partial<'facet, false>,
193 facet_reflect::Partial<'_, false>,
194 >(partial)
195 };
196
197 let partial = de.deserialize_into(partial, MetaSource::FromEvents)?;
198
199 // SAFETY: Same reasoning - no borrowed data since BORROW=false.
200 #[allow(unsafe_code)]
201 let partial: facet_reflect::Partial<'facet, false> = unsafe {
202 core::mem::transmute::<
203 facet_reflect::Partial<'_, false>,
204 facet_reflect::Partial<'facet, false>,
205 >(partial)
206 };
207
208 Ok(partial)
209}
210
211/// Deserialize MsgPack bytes into an existing Partial, allowing zero-copy borrowing.
212///
213/// This variant requires the input to outlive the Partial's lifetime (`'input: 'facet`),
214/// enabling zero-copy deserialization of byte slices as `&[u8]` or `Cow<[u8]>`.
215///
216/// This is useful for reflection-based deserialization where you don't have
217/// a concrete type `T` at compile time, only its Shape metadata.
218///
219/// # Example
220///
221/// ```
222/// use facet::Facet;
223/// use facet_msgpack::from_slice_into_borrowed;
224/// use facet_reflect::Partial;
225///
226/// #[derive(Facet, Debug, PartialEq)]
227/// struct Message<'a> {
228/// id: u32,
229/// data: &'a [u8],
230/// }
231///
232/// // MsgPack encoding of {"id": 1, "data": <bin8 with 3 bytes>}
233/// let bytes = &[0x82, 0xa2, b'i', b'd', 0x01, 0xa4, b'd', b'a', b't', b'a', 0xc4, 0x03, 0xAB, 0xCD, 0xEF];
234/// let partial = Partial::alloc::<Message>().unwrap();
235/// let partial = from_slice_into_borrowed(bytes, partial).unwrap();
236/// let value = partial.build().unwrap();
237/// let msg: Message = value.materialize().unwrap();
238/// assert_eq!(msg.id, 1);
239/// assert_eq!(msg.data, &[0xAB, 0xCD, 0xEF]);
240/// ```
241pub fn from_slice_into_borrowed<'input, 'facet>(
242 input: &'input [u8],
243 partial: facet_reflect::Partial<'facet, true>,
244) -> Result<facet_reflect::Partial<'facet, true>, DeserializeError>
245where
246 'input: 'facet,
247{
248 use facet_format::{FormatDeserializer, MetaSource};
249 let mut parser = MsgPackParser::new(input);
250 let mut de = FormatDeserializer::new(&mut parser);
251 de.deserialize_into(partial, MetaSource::FromEvents)
252}