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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! # npsd (Network Payload Serializer / Deserializer)
//!
//! The `npsd` crate provides a flexible and efficient way to serialize and deserialize network payloads.
//! It supports converting Rust types into byte streams suitable for network transmission and reconstructing
//! those types from byte streams received over the network. This is particularly useful for networked
//! applications that require efficient and reliable data exchange.
//!
//! ## Features
//! - Serialize and deserialize complex Rust types to and from byte streams.
//! - Support for custom serialization contexts.
//! - Middleware support for extensible processing during serialization/deserialization.
//!
//! ## Examples
//! ### Sync Schema
//! Requires the `sync` feature to be enabled.
//! ```rust
//! # #[cfg(feature = "sync")]
//! use npsd::{Payload, Schema, Next, Info};
//!
//! # #[cfg(feature = "sync")]
//! #[derive(Schema, Info, PartialEq, Debug)]
//! enum Animal {
//! Dog,
//! Frog(String, Vec<isize>),
//! Cat { age: usize, name: String },
//! AntHive(Vec<String>),
//! }
//!
//! # #[cfg(feature = "sync")]
//! #[test]
//! fn test_schema() {
//! // Create Middleware
//! let mut next = Next::default();
//!
//! // Create an instance of `Animal`.
//! let animal = Animal::Frog("Frog".to_string(), vec![12393818, -19383812, 11111, -1093838482]);
//!
//! // Serialize the `animal` instance into a packet.
//! animal.into_packet(&mut (), &mut next).unwrap();
//!
//! // Create a copy of serialized data if needed
//! let _serialized = next.serialized();
//!
//! // Deserialize the packet back into an `Animal` instance.
//! let deserialized = Animal::from_packet(&mut (), &mut next).unwrap();
//!
//! // Ensure the deserialized instance matches the original.
//! assert_eq!(deserialized, animal);
//! }
//! ```
//!
//! ### Async Schema
//! Requires the `async` feature to be enabled.
//! ```rust
//! # #[cfg(feature = "async")]
//! use npsd::{AsyncPayload, AsyncSchema, Next, Info};
//!
//! # #[cfg(feature = "async")]
//! #[derive(AsyncSchema, Info, PartialEq, Debug)]
//! enum Animal {
//! Dog,
//! Frog(String, Vec<isize>),
//! Cat { age: usize, name: String },
//! AntHive(Vec<String>),
//! }
//!
//! # #[cfg(feature = "async")]
//! #[tokio::test]
//! async fn test_schema() {
//! // Create Middleware
//! let mut next = Next::default();
//!
//! // Create an instance of `Animal`.
//! let animal = Animal::Frog("Frog".to_string(), vec![12393818, -19383812, 11111, -1093838482]);
//!
//! // Serialize the `animal` instance into a packet.
//! animal.poll_into_packet(&mut (), &mut next).await.unwrap();
//!
//! // Create a copy of serialized data if needed
//! let _serialized = next.serialized();
//!
//! // Deserialize the packet back into an `Animal` instance.
//! let deserialized = Animal::poll_from_packet(&mut (), &mut next).await.unwrap();
//!
//! // Ensure the deserialized instance matches the original.
//! assert_eq!(deserialized, animal);
//! }
//! ```
//!
//! ### Sync Bitmap
//! Requires the `sync` feature to be enabled.
//! ```rust
//! # #[cfg(feature = "sync")]
//! use npsd::{Payload, Bitmap, Next, Info};
//!
//! # #[cfg(feature = "sync")]
//! #[derive(Bitmap, Info, PartialEq, Debug)]
//! struct Flags {
//! a: bool,
//! b: bool,
//! c: bool,
//! }
//!
//! # #[cfg(feature = "sync")]
//! #[test]
//! fn test_bitmap() {
//! // Create Middleware
//! let mut next = Next::default();
//!
//! // Create an u8 bitmap of `Flags`.
//! let flags = Flags { a: true, b: false, c: true };
//!
//! // Serialize the `Flags` into a packet.
//! flags.into_packet(&mut (), &mut next).unwrap();
//!
//! // Create a copy of serialized data if needed
//! let _serialized = next.serialized();
//!
//! // Deserialize the packet back into an `Flags`.
//! let deserialized = Flags::from_packet(&mut (), &mut next).unwrap();
//!
//! // Ensure the deserialized matches the original.
//! assert_eq!(deserialized, flags);
//! }
//! ```
//!
//! ### Async Bitmap
//! Requires the `async` feature to be enabled.
//! ```rust
//! # #[cfg(feature = "async")]
//! use npsd::{AsyncPayload, AsyncBitmap, Next, Info};
//!
//! # #[cfg(feature = "async")]
//! #[derive(AsyncBitmap, Info, PartialEq, Debug)]
//! struct Flags {
//! a: bool,
//! b: bool,
//! c: bool,
//! }
//!
//! # #[cfg(feature = "async")]
//! #[tokio::test]
//! async fn test_async_bitmap() {
//! // Create Middleware
//! let mut next = Next::default();
//!
//! // Create an u8 bitmap of `Flags`.
//! let flags = Flags { a: true, b: false, c: true };
//!
//! // Serialize the `Flags` into a packet.
//! flags.poll_into_packet(&mut (), &mut next).await.unwrap();
//!
//! // Create a copy of serialized data if needed
//! let _serialized = next.serialized();
//!
//! // Deserialize the packet back into an `Flags`.
//! let deserialized = Flags::poll_from_packet(&mut (), &mut next).await.unwrap();
//!
//! // Ensure the deserialized matches the original.
//! assert_eq!(deserialized, flags);
//! }
//! ```
/// The `Middleware` trait defines methods for converting types to and from payloads of bytes.
///
/// ### Methods
///
/// - `fn into_payload<C, T: IntoPayload<C>>(&mut self, value: &T, ctx: &mut C) -> Result<(), Error>`:
/// - Converts a value into a payload of bytes. This method takes a value and a context, serializes the value into a byte stream, and writes the resulting bytes into the handler.
/// - `fn from_payload<'a, C, T: FromPayload<'a, C>>(&mut self, ctx: &mut C) -> Result<T, Error>`:
/// - Converts a payload of bytes back into a value. This method reads bytes from the handler, uses the context to interpret them, and reconstructs the original value.
/// - `fn write<T>(&mut self, data: &[T]) -> Result<(), Error>`:
/// - Writes raw data into the handler. This method takes a slice of data and appends it to the handler after ensuring that the size of the data elements is 1 byte.
/// - `fn read<'a, T>(&'a mut self, nbytes: usize) -> Result<&'a [T], Error>`:
/// - Reads raw data from the handler. This method reads a specified number of bytes from the handler, splits the handler's data accordingly, and returns a slice of the read data.
/// - `fn read_mut<'a, T>(&'a mut self, nbytes: usize) -> Result<&'a mut [T], Error>`:
/// - Reads raw data from the handler. This method reads a specified number of bytes from the handler, splits the handler's data accordingly, and returns a mutable slice of the read data.
/// - `fn push<T: AnyBox<'a>>(&mut self, value: Box<T>) -> Result<&'a T, Error>`:
/// - Pushes a boxed value into the handler, returning a reference to the stored value.
/// - `fn push_mut<T: AnyBox<'a>>(&mut self, value: Box<T>) -> Result<&'a mut T, Error>`:
/// - Pushes a boxed value into the handler, returning a mutable reference to the stored value.
/// - `fn push_array<T: AnyBox<'a>>(&mut self, values: Box<[T]>) -> Result<&'a [T], Error>`:
/// - Pushes a boxed array of values into the handler, returning a reference to the stored array.
/// - `fn push_array_mut<T: AnyBox<'a>>(&mut self, values: Box<[T]>) -> Result<&'a mut [T], Error>`:
/// - Pushes a boxed array of values into the handler, returning a mutable reference to the stored array.
/// The `AsyncMiddleware` trait defines asynchronous methods for converting types to and from payloads of bytes.
///
/// ### Methods
/// - `fn poll_into_payload<'a, C, T: AsyncIntoPayload<C>>(&mut self, value: &T, ctx: &mut C) -> impl Future<Output = Result<(), Error>>`:
/// - Polls the conversion of a value into a payload of bytes asynchronously.
/// - `fn poll_from_payload<'a, C, T: AsyncFromPayload<'a, C>>(&mut self, ctx: &mut C) -> impl Future<Output = Result<T, Error>>`:
/// - Polls the conversion of a payload of bytes back into a value asynchronously.
/// - `fn poll_write<T>(&mut self, data: &[T]) -> impl Future<Output = Result<(), Error>>`:
/// - Polls the asynchronous writing of raw data into the handler.
/// - `fn poll_read<'a, T>(&'a mut self, nbytes: usize) -> impl Future<Output = Result<&'a [T], Error>>`:
/// - Polls the asynchronous reading of raw data from the handler.
/// - `fn poll_read_mut<'a, T>(&'a mut self, nbytes: usize) -> impl Future<Output = Result<&'a mut [T], Error>>`:
/// - Polls the asynchronous reading of raw data from the handler, returning a mutable slice of the read data.
/// - `fn poll_push<T: AnyBox<'a>>(&mut self, value: Box<T>) -> impl Future<Output = Result<&'a T, Error>>`:
/// - Polls the asynchronous pushing of a boxed value into the handler, returning a reference to the stored value.
/// - `fn poll_push_mut<T: AnyBox<'a>>(&mut self, value: Box<T>) -> impl Future<Output = Result<&'a mut T, Error>>`:
/// - Polls the asynchronous pushing of a boxed value into the handler, returning a mutable reference to the stored value.
/// - `fn poll_push_array<T: AnyBox<'a>>(&mut self, values: Box<[T]>) -> impl Future<Output = Result<&'a [T], Error>>`:
/// - Polls the asynchronous pushing of a boxed array of values into the handler, returning a reference to the stored array.
/// - `fn poll_push_array_mut<T: AnyBox<'a>>(&mut self, values: Box<[T]>) -> impl Future<Output = Result<&'a mut [T], Error>>`:
/// - Polls the asynchronous pushing of a boxed array of values into the handler, returning a mutable reference to the stored array.
/// The `IntoPayload` trait is used to convert a type into a payload of bytes.
///
/// ### Methods
/// - `fn into_payload<'m, M: Middleware<'m>>(&self, ctx: &mut C, next: &mut M) -> Result<(), Error>`:
/// - Converts a value into a payload of bytes. This method takes the value, context, and middleware, serializes the value into a byte stream, and writes it into the handler.
/// The `AsyncIntoPayload` trait is used for asynchronous methods for converting types into payloads of bytes.
///
/// ### Methods
/// - `fn poll_into_payload<'m, M: AsyncMiddleware<'m>>(&self, ctx: &mut C, next: &mut M) -> impl Future<Output = Result<(), Error>>`:
/// - Polls the conversion of a value into a payload of bytes asynchronously.
/// The `FromPayload` trait is used to convert a payload of bytes back into a type.
///
/// ### Methods
/// - `fn from_payload<M: Middleware<'a>>(ctx: &mut C, next: &mut M) -> Result<Self, Error>`:
/// - Converts a payload of bytes back into a value. This method reads bytes from the handler, uses the context and middleware to interpret them, and reconstructs the original value.
/// The `AsyncFromPayload` trait is used for asynchronous methods for converting payloads of bytes back into types.
///
/// ### Methods
/// - `fn poll_from_payload<'m, M: AsyncMiddleware<'a>>(ctx: &mut C, next: &mut M) -> impl Future<Output = Result<Self, Error>>`:
/// - Polls the conversion of a payload of bytes back into a value asynchronously.
/// The `Payload` trait combines `IntoPayload` and `FromPayload` to facilitate complete serialization and deserialization of types.
///
/// ### Methods
/// - `fn into_packet<'b, M: Middleware<'b>>(&self, ctx: &mut C, next: &mut M) -> Result<(), Error>`:
/// - Serializes a value into a buffer. This method takes the value, context, and an initial buffer capacity, serializes the value, and returns the resulting byte buffer.
/// - `fn from_packet<'m, M: Middleware<'m>>(ctx: &mut C, next: &mut M) -> Result<Self, Error>`:
/// - Deserializes a buffer into a value. This method takes a context and a buffer containing the serialized data, and returns the deserialized value.
/// The `AsyncPayload` trait combines `AsyncIntoPayload` and `AsyncFromPayload` to asynchronous methods for complete serialization and deserialization of types.
///
/// ### Methods
/// - `fn poll_into_packet<'m, M: AsyncMiddleware<'m>>(&self, ctx: &mut C, next: &mut M) -> impl Future<Output = Result<(), Error>>`:
/// - Initiates the asynchronous conversion of a value into a packet.
/// - `fn poll_from_packet<'m, M: AsyncMiddleware<'m>>(ctx: &mut C, next: &mut M) -> impl Future<Output = Result<Self, Error>>`:
/// - Initiates the asynchronous deserialization of a packet into a value.
/// The `PayloadInfo` trait provides metadata about the payload.
///
/// ### Associated Constants
/// - `const HASH: u64`: A constant hash value associated with the type.
/// - `const TYPE: &'static str`: A string representing the type of the payload.
/// - `const SIZE: Option<usize>`: An optional constant representing the size of the payload.
pub use *;
use Future;
pub use xxh3_64 as PayloadConstHash;
pub use xxh3_64 as PayloadHash;
pub use *;
pub use *;
pub use *;