Skip to main content

tampon/
buffer.rs

1/* 
2Copyright (c) 2026  NickelAnge.Studio 
3Email               mathieu.grenier@nickelange.studio
4Git                 https://github.com/NickelAngeStudio/tampon
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25/// ##### Variadic macro used to create a [`buffer`](https://en.wikipedia.org/wiki/Data_buffer) and [`serialize`](https://en.wikipedia.org/wiki/Serialization) [`compatible variables`](macro.buffer.html#compatible-variabless). 
26/// 
27/// # Description
28/// Variadic macro used to create a [`buffer`](https://en.wikipedia.org/wiki/Data_buffer) and [`serialize`](https://en.wikipedia.org/wiki/Serialization) [`bool`], [`Numeric types`](https://doc.rust-lang.org/reference/types/numeric.html) (except usize, isize), [`String`] and implementors of trait [`Tampon`](trait.Tampon.html).
29/// Also work with [`slice`] by using brackets `[]` instead of parenthesis `()`.
30/// 
31/// # Usage
32/// `let buffer = buffer!([0..n](v1, ..., vn):type, [0..n][optional_len_type : s1, ..., sn]:type);`
33/// * One-to-many `(v1, ..., vn):type` where elements in `parenthesis()` are the variables to be copied into created buffer.
34/// * One-to-many `[s1, ..., sn]:type` where elements in `brackets[]` are the slices to be copied into created buffer.
35///     * `optional_len_type` u8, u16, u32 default, u64 or u128 for the size of bytes used to encode length. 
36/// 
37/// # Return
38/// New buffer created with argument(s) serialized with the size needed to contain them all.
39/// 
40/// # Example(s)
41/// ```
42/// // Import macro buffer
43/// use tampon::buffer;
44/// 
45/// // Declare multiple variables (numerics don't need to be same type)
46/// let a:u8 = 55;
47/// let b:u8 = 255;
48/// let c:u32 = 12545566;
49/// let d:String = String::from("Example string");
50/// let e:Vec<i32> = vec![i32::MAX; 50];
51/// let f:Vec<f64> = vec![f64::MAX; 50];
52/// let g:Vec<f64> = vec![f64::MAX; 50];
53/// 
54/// // Create a buffer and serialize data with buffer! macro.
55/// let buffer:Vec<u8> = buffer!((a,b):u8, (c):u32, (d):String, [e]:i32, [f,g]:f64);
56/// 
57/// // Print result
58/// println!("Resulted buffer={:?}", buffer);
59/// ```
60/// 
61/// # Compatible variables(s)
62/// * [`bool`]
63/// * All [`Numeric types`](https://doc.rust-lang.org/reference/types/numeric.html) except [`usize`] and [`isize`]
64/// * [`String`] 
65/// * Implementors of trait [`Tampon`](trait.Tampon.html)
66/// * [`slice`] of the above types
67/// 
68/// # Endianness
69/// * [`Numeric types`](https://doc.rust-lang.org/reference/types/numeric.html) bytes are written as [`little endian`](https://en.wikipedia.org/wiki/Endianness).
70#[macro_export]
71macro_rules! buffer {
72
73    ($( $tokens:tt : $tokens_type:ident ),+ ) => {{
74        // Get size needed for variable serialization
75        let buffer_size = $crate::bytes_size!( $($tokens : $tokens_type),+);
76
77        // Create mutable buffer of needed size
78        let mut buffer:Vec<u8> = vec![0;buffer_size];
79
80        // Serialize variable into vector
81        $crate::serialize!(buffer, $($tokens : $tokens_type),+);
82
83
84        // Return buffer
85        buffer
86
87    } as Vec<u8> };
88
89    () => {{
90        // Create mutable buffer of needed size
91        let mut buffer:Vec<u8> = vec![0;0];
92
93        // Return buffer
94        buffer
95
96    } as Vec<u8> };
97
98}