Skip to main content

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