deku_string/vec/mod.rs
1//! Wrapper around `Vec<T>` to read and write with various layouts.
2mod deku_impl;
3mod lib_impl;
4#[cfg(feature = "serde")]
5mod serde_impl;
6mod std_impl;
7
8use alloc::vec::Vec;
9
10use crate::Size;
11
12/// Thin wrapper around [`alloc::vec::Vec<T>`] to read and write in various layouts.
13///
14/// It designed to make it easy to create complicated fields like this:
15/// `encrypted_compressed_items: Encrypted<Compressed<FixedLengthVec<PascalString>>>`
16///
17/// Example usage of [`VecDeku`]:
18///
19/// ```rust
20/// use deku_string::{Encoding, Size, StringDeku, StringLayout, VecDeku, VecLayout};
21///
22/// #[derive(Debug, Clone, PartialEq, deku::DekuRead, deku::DekuWrite)]
23/// #[deku(endian = "little")]
24/// struct SampleModel {
25/// #[deku(ctx = "VecLayout::FixedLength(10)")]
26/// fixed_length_vec: VecDeku<u8>,
27///
28/// #[deku(ctx = "VecLayout::LengthPrefix(Size::U32), (Encoding::Utf8, \
29/// StringLayout::LengthPrefix(Size::U8))")]
30/// vec_of_strings: VecDeku<StringDeku>,
31/// }
32/// ```
33#[derive(Clone, Default, Eq, Ord, PartialEq, PartialOrd)]
34pub struct VecDeku<T>(Vec<T>)
35where
36 T: Sized + Clone;
37
38/// Supported vec binary layouts.
39#[derive(Debug, Clone, Copy)]
40#[non_exhaustive]
41pub enum VecLayout {
42 /// Fixed length data.
43 ///
44 /// If a user writes less items, [`Default::default()`] value will be used.
45 ///
46 /// While it's possible to embed custom value here, it would have serious
47 /// limitations.
48 FixedLength(
49 /// How many (exact) items should be in vector.
50 usize,
51 ),
52
53 /// String is prefixed by length value
54 LengthPrefix(Size),
55
56 /// Read until the reader end
57 End,
58}