postcard/ser/flavors.rs
1//! # Serialization Flavors
2//!
3//! "Flavors" in `postcard` are used as modifiers to the serialization or deserialization
4//! process. Flavors typically modify one or both of the following:
5//!
6//! 1. The output medium of the serialization, e.g. whether the data is serialized to a `[u8]` slice, or a `heapless::Vec`.
7//! 2. The format of the serialization, such as encoding the serialized output in a COBS format, performing CRC32 checksumming while serializing, etc.
8//!
9//! Flavors are implemented using the [`Flavor`] trait, which acts as a "middleware" for receiving the bytes as serialized by `serde`.
10//! Multiple flavors may be combined to obtain a desired combination of behavior and storage.
11//! When flavors are combined, it is expected that the storage flavor (such as `Slice` or `HVec`) is the innermost flavor.
12//!
13//! Custom flavors may be defined by users of the `postcard` crate, however some commonly useful flavors have been provided in
14//! this module. If you think your custom flavor would be useful to others, PRs adding flavors are very welcome!
15//!
16//! ## Usability
17//!
18//! Flavors may not always be convenient to use directly, as they may expose some implementation details of how the
19//! inner workings of the flavor behaves. It is typical to provide a convenience method for using a flavor, to prevent
20//! the user from having to specify generic parameters, setting correct initialization values, or handling the output of
21//! the flavor correctly. See `postcard::to_vec()` for an example of this.
22//!
23//! It is recommended to use the [`serialize_with_flavor()`](../fn.serialize_with_flavor.html) method for serialization. See it's documentation for information
24//! regarding usage and generic type parameters.
25//!
26//! ## When to use (multiple) flavors
27//!
28//! Combining flavors are nice for convenience, as they perform potentially multiple steps of
29//! serialization at one time.
30//!
31//! This can often be more memory efficient, as intermediate buffers are not typically required.
32//!
33//! ## When NOT to use (multiple) flavors
34//!
35//! The downside of passing serialization through multiple steps is that it is typically slower than
36//! performing each step serially. Said simply, "cobs encoding while serializing" is often slower
37//! than "serialize then cobs encode", due to the ability to handle longer "runs" of data in each
38//! stage. The downside is that if these stages can not be performed in-place on the buffer, you
39//! will need additional buffers for each stage.
40//!
41//! ## Examples
42//!
43//! ### Using a single flavor
44//!
45//! In the first example, we use the `Slice` flavor, to store the serialized output into a mutable `[u8]` slice.
46//! No other modification is made to the serialization process.
47//!
48//! ```rust
49//! use postcard::{
50//! serialize_with_flavor,
51//! ser_flavors::Slice,
52//! };
53//!
54//! let mut buf = [0u8; 32];
55//!
56//! let data: &[u8] = &[0x01, 0x00, 0x20, 0x30];
57//! let buffer = &mut [0u8; 32];
58//! let res = serialize_with_flavor::<[u8], Slice, &mut [u8]>(
59//! data,
60//! Slice::new(buffer)
61//! ).unwrap();
62//!
63//! assert_eq!(res, &[0x04, 0x01, 0x00, 0x20, 0x30]);
64//! ```
65//!
66//! ### Using combined flavors
67//!
68//! In the second example, we mix `Slice` with `Cobs`, to cobs encode the output while
69//! the data is serialized. Notice how `Slice` (the storage flavor) is the innermost flavor used.
70//!
71//! ```rust
72//! use postcard::{
73//! serialize_with_flavor,
74//! ser_flavors::{Cobs, Slice},
75//! };
76//!
77//! let mut buf = [0u8; 32];
78//!
79//! let data: &[u8] = &[0x01, 0x00, 0x20, 0x30];
80//! let buffer = &mut [0u8; 32];
81//! let res = serialize_with_flavor::<[u8], Cobs<Slice>, &mut [u8]>(
82//! data,
83//! Cobs::try_new(Slice::new(buffer)).unwrap(),
84//! ).unwrap();
85//!
86//! assert_eq!(res, &[0x03, 0x04, 0x01, 0x03, 0x20, 0x30, 0x00]);
87//! ```
88
89use crate::error::{Error, Result};
90use cobs::{EncoderState, PushResult};
91use core::marker::PhantomData;
92use core::ops::Index;
93use core::ops::IndexMut;
94
95#[cfg(feature = "heapless")]
96pub use heapless_vec::*;
97
98#[cfg(feature = "use-std")]
99pub use std_vec::*;
100
101#[cfg(feature = "alloc")]
102pub use alloc_vec::*;
103
104#[cfg(feature = "alloc")]
105extern crate alloc;
106
107/// The serialization Flavor trait
108///
109/// This is used as the primary way to encode serialized data into some kind of buffer,
110/// or modify that data in a middleware style pattern.
111///
112/// See the module level docs for an example of how flavors are used.
113pub trait Flavor {
114 /// The `Output` type is what this storage "resolves" to when the serialization is complete,
115 /// such as a slice or a Vec of some sort.
116 type Output;
117
118 /// Override this method when you want to customize processing
119 /// multiple bytes at once, such as copying a slice to the output,
120 /// rather than iterating over one byte at a time.
121 #[inline]
122 fn try_extend(&mut self, data: &[u8]) -> Result<()> {
123 data.iter().try_for_each(|d| self.try_push(*d))
124 }
125
126 /// Push a single byte to be modified and/or stored.
127 fn try_push(&mut self, data: u8) -> Result<()>;
128
129 /// Finalize the serialization process.
130 fn finalize(self) -> Result<Self::Output>;
131}
132
133////////////////////////////////////////
134// Slice
135////////////////////////////////////////
136
137/// The `Slice` flavor is a storage flavor, storing the serialized (or otherwise modified) bytes into a plain
138/// `[u8]` slice. The `Slice` flavor resolves into a sub-slice of the original slice buffer.
139pub struct Slice<'a> {
140 start: *mut u8,
141 cursor: *mut u8,
142 end: *mut u8,
143 _pl: PhantomData<&'a [u8]>,
144}
145
146impl<'a> Slice<'a> {
147 /// Create a new `Slice` flavor from a given backing buffer
148 pub fn new(buf: &'a mut [u8]) -> Self {
149 let ptr = buf.as_mut_ptr_range();
150 Slice {
151 start: ptr.start,
152 cursor: ptr.start,
153 end: ptr.end,
154 _pl: PhantomData,
155 }
156 }
157}
158
159impl<'a> Flavor for Slice<'a> {
160 type Output = &'a mut [u8];
161
162 #[inline(always)]
163 fn try_push(&mut self, b: u8) -> Result<()> {
164 if self.cursor == self.end {
165 Err(Error::SerializeBufferFull)
166 } else {
167 // SAFETY: `self.cursor` is in-bounds and won't be incremented past `self.end` as we
168 // have checked above.
169 unsafe {
170 self.cursor.write(b);
171 self.cursor = self.cursor.add(1);
172 }
173 Ok(())
174 }
175 }
176
177 #[inline(always)]
178 fn try_extend(&mut self, b: &[u8]) -> Result<()> {
179 let remain = (self.end as usize) - (self.cursor as usize);
180 let blen = b.len();
181 if blen > remain {
182 Err(Error::SerializeBufferFull)
183 } else {
184 // SAFETY: `self.cursor` is in-bounds for `blen` elements and won't be incremented past
185 // `self.end` as we have checked above.
186 unsafe {
187 core::ptr::copy_nonoverlapping(b.as_ptr(), self.cursor, blen);
188 self.cursor = self.cursor.add(blen);
189 }
190 Ok(())
191 }
192 }
193
194 fn finalize(self) -> Result<Self::Output> {
195 let used = (self.cursor as usize) - (self.start as usize);
196 // SAFETY: `self.cursor` is in-bounds for `used` elements
197 let sli = unsafe { core::slice::from_raw_parts_mut(self.start, used) };
198 Ok(sli)
199 }
200}
201
202impl Index<usize> for Slice<'_> {
203 type Output = u8;
204
205 fn index(&self, idx: usize) -> &u8 {
206 let len = (self.end as usize) - (self.start as usize);
207 assert!(idx < len);
208 // SAFETY: `self.start` is in-bounds at `idx`
209 unsafe { &*self.start.add(idx) }
210 }
211}
212
213impl IndexMut<usize> for Slice<'_> {
214 fn index_mut(&mut self, idx: usize) -> &mut u8 {
215 let len = (self.end as usize) - (self.start as usize);
216 assert!(idx < len);
217 // SAFETY: `self.start` is in-bounds at `idx`
218 unsafe { &mut *self.start.add(idx) }
219 }
220}
221
222/// Wrapper over a [`core::iter::Extend<u8>`] that implements the flavor trait
223pub struct ExtendFlavor<T> {
224 iter: T,
225}
226
227impl<T> ExtendFlavor<T>
228where
229 T: core::iter::Extend<u8>,
230{
231 /// Create a new [`Self`] flavor from a given [`core::iter::Extend<u8>`]
232 pub fn new(iter: T) -> Self {
233 Self { iter }
234 }
235}
236
237impl<T> Flavor for ExtendFlavor<T>
238where
239 T: core::iter::Extend<u8>,
240{
241 type Output = T;
242
243 #[inline(always)]
244 fn try_push(&mut self, data: u8) -> Result<()> {
245 self.iter.extend([data]);
246 Ok(())
247 }
248
249 #[inline(always)]
250 fn try_extend(&mut self, b: &[u8]) -> Result<()> {
251 self.iter.extend(b.iter().copied());
252 Ok(())
253 }
254
255 fn finalize(self) -> Result<Self::Output> {
256 Ok(self.iter)
257 }
258}
259
260/// Support for the [`embedded-io`](crate::eio::embedded_io) traits
261#[cfg(any(feature = "embedded-io-04", feature = "embedded-io-06"))]
262pub mod eio {
263
264 use super::Flavor;
265 use crate::{Error, Result};
266
267 /// Wrapper over a [`embedded_io Write`](crate::eio::Write) that implements the flavor trait
268 pub struct WriteFlavor<T> {
269 writer: T,
270 }
271
272 impl<T> WriteFlavor<T>
273 where
274 T: crate::eio::Write,
275 {
276 /// Create a new [`Self`] flavor from a given [`embedded_io Write`](crate::eio::Write)
277 pub fn new(writer: T) -> Self {
278 Self { writer }
279 }
280 }
281
282 impl<T> Flavor for WriteFlavor<T>
283 where
284 T: crate::eio::Write,
285 {
286 type Output = T;
287
288 #[inline(always)]
289 fn try_push(&mut self, data: u8) -> Result<()> {
290 self.writer
291 .write_all(&[data])
292 .map_err(|_| Error::SerializeBufferFull)?;
293 Ok(())
294 }
295
296 #[inline(always)]
297 fn try_extend(&mut self, b: &[u8]) -> Result<()> {
298 self.writer
299 .write_all(b)
300 .map_err(|_| Error::SerializeBufferFull)?;
301 Ok(())
302 }
303
304 fn finalize(mut self) -> Result<Self::Output> {
305 self.writer
306 .flush()
307 .map_err(|_| Error::SerializeBufferFull)?;
308 Ok(self.writer)
309 }
310 }
311}
312
313/// Support for the [`std::io`] traits
314#[cfg(feature = "use-std")]
315pub mod io {
316
317 use super::Flavor;
318 use crate::{Error, Result};
319
320 /// Wrapper over a [`std::io::Write`] that implements the flavor trait
321 pub struct WriteFlavor<T> {
322 writer: T,
323 }
324
325 impl<T> WriteFlavor<T>
326 where
327 T: std::io::Write,
328 {
329 /// Create a new [`Self`] flavor from a given [`std::io::Write`]
330 pub fn new(writer: T) -> Self {
331 Self { writer }
332 }
333 }
334
335 impl<T> Flavor for WriteFlavor<T>
336 where
337 T: std::io::Write,
338 {
339 type Output = T;
340
341 #[inline(always)]
342 fn try_push(&mut self, data: u8) -> Result<()> {
343 self.writer
344 .write_all(&[data])
345 .map_err(|_| Error::SerializeBufferFull)?;
346 Ok(())
347 }
348
349 #[inline(always)]
350 fn try_extend(&mut self, b: &[u8]) -> Result<()> {
351 self.writer
352 .write_all(b)
353 .map_err(|_| Error::SerializeBufferFull)?;
354 Ok(())
355 }
356
357 fn finalize(mut self) -> Result<Self::Output> {
358 self.writer
359 .flush()
360 .map_err(|_| Error::SerializeBufferFull)?;
361 Ok(self.writer)
362 }
363 }
364}
365
366#[cfg(feature = "heapless")]
367mod heapless_vec {
368 use super::Flavor;
369 use super::Index;
370 use super::IndexMut;
371 use crate::{Error, Result};
372 use heapless::Vec;
373
374 ////////////////////////////////////////
375 // HVec
376 ////////////////////////////////////////
377
378 /// The `HVec` flavor is a wrapper type around a `heapless::Vec`. This is a stack
379 /// allocated data structure, with a fixed maximum size and variable amount of contents.
380 ///
381 /// This flavor uses `heapless` v0.7, which is affected by [RUSTSEC-2026-0575] and
382 /// will not be fixed. Prefer [`heapless_v0_9::ser::HVec`], enabled by the `heapless-v0_9` feature:
383 ///
384 /// ```rust
385 /// # #[cfg(feature = "heapless-v0_9")] {
386 /// use postcard::heapless_v0_9::{heapless::Vec, ser::HVec};
387 /// use postcard::serialize_with_flavor;
388 ///
389 /// let ser: Vec<u8, 32> = serialize_with_flavor(&true, HVec::<32>::new()).unwrap();
390 /// assert_eq!(&ser[..], &[0x01]);
391 /// # }
392 /// ```
393 ///
394 /// [RUSTSEC-2026-0575]: https://rustsec.org/advisories/RUSTSEC-2026-0575.html
395 /// [`heapless_v0_9::ser::HVec`]: crate::heapless_v0_9::ser::HVec
396 #[derive(Default)]
397 pub struct HVec<const B: usize> {
398 /// the contained data buffer
399 vec: Vec<u8, B>,
400 }
401
402 impl<const B: usize> HVec<B> {
403 /// Create a new, currently empty, [`heapless::Vec`] to be used for storing serialized
404 /// output data.
405 pub fn new() -> Self {
406 Self::default()
407 }
408 }
409
410 impl<const B: usize> Flavor for HVec<B> {
411 type Output = Vec<u8, B>;
412
413 #[inline(always)]
414 fn try_extend(&mut self, data: &[u8]) -> Result<()> {
415 self.vec
416 .extend_from_slice(data)
417 .map_err(|_| Error::SerializeBufferFull)
418 }
419
420 #[inline(always)]
421 fn try_push(&mut self, data: u8) -> Result<()> {
422 self.vec.push(data).map_err(|_| Error::SerializeBufferFull)
423 }
424
425 fn finalize(self) -> Result<Vec<u8, B>> {
426 Ok(self.vec)
427 }
428 }
429
430 impl<const B: usize> Index<usize> for HVec<B> {
431 type Output = u8;
432
433 fn index(&self, idx: usize) -> &u8 {
434 &self.vec[idx]
435 }
436 }
437
438 impl<const B: usize> IndexMut<usize> for HVec<B> {
439 fn index_mut(&mut self, idx: usize) -> &mut u8 {
440 &mut self.vec[idx]
441 }
442 }
443}
444
445#[cfg(feature = "use-std")]
446mod std_vec {
447 /// The `StdVec` flavor is a wrapper type around a `std::vec::Vec`.
448 ///
449 /// This type is only available when the (non-default) `use-std` feature is active
450 pub type StdVec = super::alloc_vec::AllocVec;
451}
452
453#[cfg(feature = "alloc")]
454mod alloc_vec {
455 extern crate alloc;
456 use super::Flavor;
457 use super::Index;
458 use super::IndexMut;
459 use crate::Result;
460 use alloc::vec::Vec;
461
462 /// The `AllocVec` flavor is a wrapper type around an [`alloc::vec::Vec`].
463 ///
464 /// This type is only available when the (non-default) `alloc` feature is active
465 #[derive(Default)]
466 pub struct AllocVec {
467 /// The vec to be used for serialization
468 vec: Vec<u8>,
469 }
470
471 impl AllocVec {
472 /// Create a new, currently empty, [`alloc::vec::Vec`] to be used for storing serialized
473 /// output data.
474 pub fn new() -> Self {
475 Self::default()
476 }
477 }
478
479 impl Flavor for AllocVec {
480 type Output = Vec<u8>;
481
482 #[inline(always)]
483 fn try_extend(&mut self, data: &[u8]) -> Result<()> {
484 self.vec.extend_from_slice(data);
485 Ok(())
486 }
487
488 #[inline(always)]
489 fn try_push(&mut self, data: u8) -> Result<()> {
490 self.vec.push(data);
491 Ok(())
492 }
493
494 fn finalize(self) -> Result<Self::Output> {
495 Ok(self.vec)
496 }
497 }
498
499 impl Index<usize> for AllocVec {
500 type Output = u8;
501
502 #[inline]
503 fn index(&self, idx: usize) -> &u8 {
504 &self.vec[idx]
505 }
506 }
507
508 impl IndexMut<usize> for AllocVec {
509 #[inline]
510 fn index_mut(&mut self, idx: usize) -> &mut u8 {
511 &mut self.vec[idx]
512 }
513 }
514}
515
516////////////////////////////////////////////////////////////////////////////////
517// Modification Flavors
518////////////////////////////////////////////////////////////////////////////////
519
520////////////////////////////////////////
521// COBS
522////////////////////////////////////////
523
524/// The `Cobs` flavor implements [Consistent Overhead Byte Stuffing] on
525/// the serialized data. The output of this flavor includes the termination/sentinel
526/// byte of `0x00`.
527///
528/// This protocol is useful when sending data over a serial interface without framing such as a UART
529///
530/// [Consistent Overhead Byte Stuffing]: https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
531pub struct Cobs<B>
532where
533 B: Flavor + IndexMut<usize, Output = u8>,
534{
535 flav: B,
536 cobs: EncoderState,
537}
538
539impl<B> Cobs<B>
540where
541 B: Flavor + IndexMut<usize, Output = u8>,
542{
543 /// Create a new Cobs modifier Flavor. If there is insufficient space
544 /// to push the leading header byte, the method will return an Error
545 pub fn try_new(mut bee: B) -> Result<Self> {
546 bee.try_push(0).map_err(|_| Error::SerializeBufferFull)?;
547 Ok(Self {
548 flav: bee,
549 cobs: EncoderState::default(),
550 })
551 }
552}
553
554impl<B> Flavor for Cobs<B>
555where
556 B: Flavor + IndexMut<usize, Output = u8>,
557{
558 type Output = <B as Flavor>::Output;
559
560 #[inline(always)]
561 fn try_push(&mut self, data: u8) -> Result<()> {
562 use PushResult::*;
563 match self.cobs.push(data) {
564 AddSingle(n) => self.flav.try_push(n),
565 ModifyFromStartAndSkip((idx, mval)) => {
566 self.flav[idx] = mval;
567 self.flav.try_push(0)
568 }
569 ModifyFromStartAndPushAndSkip((idx, mval, nval)) => {
570 self.flav[idx] = mval;
571 self.flav.try_push(nval)?;
572 self.flav.try_push(0)
573 }
574 }
575 }
576
577 fn finalize(mut self) -> Result<Self::Output> {
578 let (idx, mval) = self.cobs.finalize();
579 self.flav[idx] = mval;
580 self.flav.try_push(0)?;
581 self.flav.finalize()
582 }
583}
584
585////////////////////////////////////////
586// CRC
587////////////////////////////////////////
588
589/// This Cyclic Redundancy Check flavor applies [the CRC crate's `Algorithm`](https://docs.rs/crc/latest/crc/struct.Algorithm.html) struct on
590/// the serialized data.
591///
592/// The output of this flavor receives the CRC appended to the bytes.
593/// CRCs are used for error detection when reading data back.
594/// Requires the `crc` feature.
595///
596/// More on CRCs: <https://en.wikipedia.org/wiki/Cyclic_redundancy_check>.
597#[cfg(feature = "use-crc")]
598#[cfg_attr(docsrs, doc(cfg(feature = "use-crc")))]
599pub mod crc {
600 use crc::Digest;
601 use crc::Width;
602 use serde::Serialize;
603
604 #[cfg(feature = "alloc")]
605 use super::alloc;
606 use super::Flavor;
607 use super::Slice;
608
609 use crate::serialize_with_flavor;
610 use crate::Result;
611
612 /// Manages CRC modifications as a flavor.
613 pub struct CrcModifier<'a, B, W>
614 where
615 B: Flavor,
616 W: Width,
617 {
618 flav: B,
619 digest: Digest<'a, W>,
620 }
621
622 impl<'a, B, W> CrcModifier<'a, B, W>
623 where
624 B: Flavor,
625 W: Width,
626 {
627 /// Create a new CRC modifier Flavor.
628 pub fn new(bee: B, digest: Digest<'a, W>) -> Self {
629 Self { flav: bee, digest }
630 }
631 }
632
633 macro_rules! impl_flavor {
634 ($int:ty, $to_slice:ident, $to_vec:ident, $to_allocvec:ident) => {
635 impl<'a, B> Flavor for CrcModifier<'a, B, $int>
636 where
637 B: Flavor,
638 {
639 type Output = <B as Flavor>::Output;
640
641 #[inline(always)]
642 fn try_push(&mut self, data: u8) -> Result<()> {
643 self.digest.update(&[data]);
644 self.flav.try_push(data)
645 }
646
647 fn finalize(mut self) -> Result<Self::Output> {
648 let crc = self.digest.finalize();
649 for byte in crc.to_le_bytes() {
650 self.flav.try_push(byte)?;
651 }
652 self.flav.finalize()
653 }
654 }
655
656 /// Serialize a `T` to the given slice, with the resulting slice containing
657 /// data followed by a CRC. The CRC bytes are included in the output buffer.
658 ///
659 /// When successful, this function returns the slice containing the
660 /// serialized and encoded message.
661 pub fn $to_slice<'a, T>(
662 value: &T,
663 buf: &'a mut [u8],
664 digest: Digest<'_, $int>,
665 ) -> Result<&'a mut [u8]>
666 where
667 T: Serialize + ?Sized,
668 {
669 serialize_with_flavor(value, CrcModifier::new(Slice::new(buf), digest))
670 }
671
672 /// Serialize a `T` to a `heapless::Vec<u8>`, with the `Vec` containing
673 /// data followed by a CRC. The CRC bytes are included in the output `Vec`.
674 ///
675 /// This function uses `heapless` v0.7, which is affected by [RUSTSEC-2026-0575] and
676 /// will not be fixed. Prefer wrapping [`heapless_v0_9::ser::HVec`] in a [`CrcModifier`],
677 /// enabled by the `heapless-v0_9` feature:
678 ///
679 /// ```rust
680 /// # #[cfg(all(feature = "heapless-v0_9", feature = "use-crc"))] {
681 /// use crc::{Crc, CRC_32_ISCSI};
682 /// use postcard::heapless_v0_9::{heapless::Vec, ser::HVec};
683 /// use postcard::{serialize_with_flavor, ser_flavors::crc::CrcModifier};
684 ///
685 /// let data: &[u8] = &[0x01u8, 0x00, 0x20, 0x30];
686 /// let crc = Crc::<u32>::new(&CRC_32_ISCSI);
687 /// let flavor = CrcModifier::new(HVec::<32>::new(), crc.digest());
688 /// let ser: Vec<u8, 32> = serialize_with_flavor(data, flavor).unwrap();
689 /// assert_eq!(&ser[..], &[0x04, 0x01, 0x00, 0x20, 0x30, 0x8E, 0xC8, 0x1A, 0x37]);
690 /// # }
691 /// ```
692 ///
693 /// [RUSTSEC-2026-0575]: https://rustsec.org/advisories/RUSTSEC-2026-0575.html
694 /// [`heapless_v0_9::ser::HVec`]: crate::heapless_v0_9::ser::HVec
695 #[cfg(feature = "heapless")]
696 #[cfg_attr(docsrs, doc(cfg(feature = "heapless")))]
697 pub fn $to_vec<T, const B: usize>(
698 value: &T,
699 digest: Digest<'_, $int>,
700 ) -> Result<heapless::Vec<u8, B>>
701 where
702 T: Serialize + ?Sized,
703 {
704 use super::HVec;
705 serialize_with_flavor(value, CrcModifier::new(HVec::default(), digest))
706 }
707
708 /// Serialize a `T` to a `heapless::Vec<u8>`, with the `Vec` containing
709 /// data followed by a CRC. The CRC bytes are included in the output `Vec`.
710 #[cfg(feature = "alloc")]
711 #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
712 pub fn $to_allocvec<T>(
713 value: &T,
714 digest: Digest<'_, $int>,
715 ) -> Result<alloc::vec::Vec<u8>>
716 where
717 T: Serialize + ?Sized,
718 {
719 use super::AllocVec;
720 serialize_with_flavor(value, CrcModifier::new(AllocVec::new(), digest))
721 }
722 };
723 }
724
725 impl_flavor!(u8, to_slice_u8, to_vec_u8, to_allocvec_u8);
726 impl_flavor!(u16, to_slice_u16, to_vec_u16, to_allocvec_u16);
727 impl_flavor!(u32, to_slice_u32, to_vec_u32, to_allocvec_u32);
728 impl_flavor!(u64, to_slice_u64, to_vec_u64, to_allocvec_u64);
729 impl_flavor!(u128, to_slice_u128, to_vec_u128, to_allocvec_u128);
730}
731
732/// The `Size` flavor is a measurement flavor, which accumulates the number of bytes needed to
733/// serialize the data.
734///
735/// ```
736/// use postcard::{serialize_with_flavor, ser_flavors};
737///
738/// let value = false;
739/// let size = serialize_with_flavor(&value, ser_flavors::Size::default()).unwrap();
740///
741/// assert_eq!(size, 1);
742/// ```
743#[derive(Default)]
744pub struct Size {
745 size: usize,
746}
747
748impl Flavor for Size {
749 type Output = usize;
750
751 #[inline(always)]
752 fn try_push(&mut self, _b: u8) -> Result<()> {
753 self.size += 1;
754 Ok(())
755 }
756
757 #[inline(always)]
758 fn try_extend(&mut self, b: &[u8]) -> Result<()> {
759 self.size += b.len();
760 Ok(())
761 }
762
763 fn finalize(self) -> Result<Self::Output> {
764 Ok(self.size)
765 }
766}