1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use Bytes;
use ;
/// Efficient serialization using Bytes buffers for zero-copy operations.
///
/// This module provides serialization utilities that leverage the `bytes` crate
/// for efficient memory management and zero-copy operations. Particularly useful
/// for network protocols, streaming data, and high-performance applications.
///
/// ## Features
///
/// - **Zero-Copy Operations**: Efficient sharing and cloning of serialized data
/// - **Memory Efficiency**: Reduced allocations through buffer reuse
/// - **Thread Safety**: Safe concurrent access to serialized data
/// - **Streaming Compatible**: Works well with async streaming patterns
/// - **Reference Counting**: Automatic memory management with Arc-like semantics
///
/// ## Examples
///
/// ### Efficient Data Sharing
/// ```rust
/// use trash_utilities::serde::{serialize_to_bytes, deserialize_from_bytes};
/// use serde::{Serialize, Deserialize};
/// use bytes::Bytes;
///
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// struct Message {
/// id: u64,
/// payload: Vec<u8>,
/// }
///
/// let message = Message {
/// id: 12345,
/// payload: vec![1, 2, 3, 4, 5],
/// };
///
/// // Serialize to Bytes buffer
/// let bytes: Bytes = serialize_to_bytes(&message).unwrap();
///
/// // Clone cheaply (no data copying)
/// let bytes_clone = bytes.clone();
///
/// // Deserialize from either buffer
/// let msg1: Message = deserialize_from_bytes(&bytes).unwrap();
/// let msg2: Message = deserialize_from_bytes(&bytes_clone).unwrap();
///
/// assert_eq!(msg1, msg2);
/// assert_eq!(msg1, message);
/// ```
///
/// ### Network Protocol Efficiency
/// ```rust,no_run
/// use trash_utilities::serde::serialize_to_bytes;
/// use serde::Serialize;
/// use bytes::Bytes;
///
/// #[derive(Serialize)]
/// struct Packet {
/// sequence: u32,
/// data: Vec<u8>,
/// }
///
/// async fn send_packet(packet: Packet) -> Result<(), Box<dyn std::error::Error>> {
/// let bytes: Bytes = serialize_to_bytes(&packet)?;
///
/// // Send bytes over network without copying
/// // network_send(bytes).await?;
///
/// Ok(())
/// }
/// ```
///
/// ### Memory Pool Integration
/// ```rust
/// use trash_utilities::serde::{serialize_to_bytes, deserialize_from_bytes};
/// use bytes::Bytes;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize, Debug)]
/// struct CacheEntry {
/// key: String,
/// value: Vec<u8>,
/// }
///
/// // Simulate a memory cache
/// fn cache_operation() -> Result<(), Box<dyn std::error::Error>> {
/// let entry = CacheEntry {
/// key: "user_123".to_string(),
/// value: vec![0; 1024], // 1KB of data
/// };
///
/// // Serialize once, reuse buffer
/// let serialized: Bytes = serialize_to_bytes(&entry)?;
///
/// // Store buffer in cache (efficient)
/// // cache.insert("user_123", serialized.clone());
///
/// // Deserialize when needed
/// let recovered: CacheEntry = deserialize_from_bytes(&serialized)?;
///
/// Ok(())
/// }
/// ```
/// Work with Bytes buffers for efficient memory management.
/// This function serializes to JSON and wraps the result in a Bytes buffer,
/// which is more efficient for sharing and zero-copy operations.
///
/// # Type Parameters
/// - `T`: The type to serialize, must implement `Serialize`.
///
/// # Parameters
/// - `value`: The value to serialize.
///
/// # Returns
/// - `Ok(Bytes)` containing the JSON data in a Bytes buffer.
/// - `Err(serde_json::Error)` if serialization fails.
///
/// # Errors
/// Returns `serde_json::Error` if the value cannot be serialized to JSON.
///
/// # Examples
/// ```rust
/// use trash_analyzer::serde::serialize_to_bytes;
/// use serde::Serialize;
/// use bytes::Bytes;
///
/// #[derive(Serialize)]
/// struct Message { text: String, id: u64 }
///
/// let msg = Message { text: "Hello".to_string(), id: 123 };
/// let bytes: Bytes = serialize_to_bytes(&msg).unwrap();
/// // Can be cloned cheaply and shared across threads
/// let cloned = bytes.clone();
/// ```
/// Deserialize from a Bytes buffer.
///
/// This function deserializes JSON data from a Bytes buffer, which is
/// efficient for zero-copy operations.
///
/// # Type Parameters
/// - `T`: The type to deserialize into, must implement `DeserializeOwned`.
///
/// # Parameters
/// - `bytes`: The Bytes buffer containing JSON data.
///
/// # Returns
/// - `Ok(T)` containing the deserialized value.
/// - `Err(serde_json::Error)` if deserialization fails.
///
/// # Errors
/// Returns `serde_json::Error` if the JSON is malformed or doesn't match the expected type.
///
/// # Examples
/// ```rust
/// use trash_analyzer::serde::{serialize_to_bytes, deserialize_from_bytes};
/// use serde::{Serialize, Deserialize};
/// use bytes::Bytes;
///
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// struct Message { text: String, id: u64 }
///
/// let msg = Message { text: "Hello".to_string(), id: 123 };
/// let bytes = serialize_to_bytes(&msg).unwrap();
/// let deserialized: Message = deserialize_from_bytes(&bytes).unwrap();
/// assert_eq!(deserialized, msg);
/// ```