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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#![forbid(unsafe_code)]
#![deny(missing_docs, unstable_features)]
//! # Nimble
//!
//! Async friendly, simple and fast binary encoding/decoding in Rust.
//!
//! ## Binary encoding scheme
//!
//! This crate uses a minimal binary encoding scheme. For example, consider the following `struct`:
//!
//! ```
//! struct MyStruct {
//!     a: u8,
//!     b: u16,
//! }
//! ```
//!
//! `encode()` will serialize this into `Vec` of size `3` (which is the sum of sizes of `u8` and `u16`).
//!
//! Similarly, for types which can have dynamic size (`Vec`, `String`, etc.), `encode()` prepends the size of encoded value
//! as `u64`.
//!
//! ## Usage
//!
//! Add `nimble` in your `Cargo.toml`'s `dependencies` section:
//!
//! ```toml
//! [dependencies]
//! nimble = { version = "0.2", features = ["derive"] }
//! ```
//!
//! Or, if you are in an environment based on `tokio`, use:
//!
//! ```toml
//! [dependencies]
//! nimble = { version = "0.2", default-features = false, features = ["derive", "tokio"] }
//! ```
//!
//! For encoding and decoding, any type must implement two traits provided by this crate, i.e., `Encode` and `Decode`. For
//! convenience, `nimble` provides `derive` macros (only when `"derive"` feature is enabled) to implement these traits.
//!
//! ```rust,ignore
//! use nimble::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! struct MyStruct {
//!     a: u8,
//!     b: u16,
//! }
//! ```
//!
//! Now you can use `encode()` and `decode()` functions to encode and decode values of `MyStruct`. In addition to this, you
//! can also use `MyStruct::encode_to()` function to encode values directly to a type implementing `AsyncWrite` and
//! `MyStruct::decode_from()` function to decode values directly from a type implementing `AsyncRead`.
//!
//! > Note: Most of the functions exposed by this crate are `async` functions and returns `Future` values. So, you'll need
//! an executor to drive the `Future` returned from these functions. `async-std` and `tokio` are two popular options.
//!
//! ### Features
//!
//! - `futures`: Select this feature when you want to implement `Encode` and `Decode` using `futures`'
//!   `AsyncRead`/`AsyncWrite` traits.
//!   - **Enabled** by default.
//! - `tokio`: Select this feature when you want to implement `Encode` and `Decode` using `tokio`'s `AsyncRead`/`AsyncWrite`
//!   traits.
//!   - **Disabled** by default.
//! - `derive`: Enables derive macros for implementing `Encode` and `Decode` traits.
//!   - **Disabled** by default.
//!
//! > Note: Features `futures` and `tokio` are mutually exclusive, i.e., only one of them can be enabled at a time.
//! > Compilation will fail if either both of them are enabled or none of them are enabled.
#[cfg(all(feature = "futures", feature = "tokio"))]
compile_error!("Features `futures` and `tokio` are mutually exclusive");

#[cfg(not(any(feature = "futures", feature = "tokio")))]
compile_error!("Either feature `futures` or `tokio` must be enabled for this crate");

mod config;
mod decode;
mod encode;
mod error;

pub mod io;

#[cfg(feature = "derive")]
pub use nimble_derive::{Decode, Encode};

/// Utility macro for implementing [`Encode`](trait.Encode.html) and [`Decode`](trait.Decode.html) traits.
pub use async_trait::async_trait;

pub use self::{
    config::{Config, Endianness},
    decode::Decode,
    encode::Encode,
    error::{Error, Result},
};

use self::io::{Read, Write};

const DEFAULT_CONFIG: Config = Config::new_default();

/// Returns default `Config`
pub fn config<'a>() -> &'a Config {
    &DEFAULT_CONFIG
}

/// Encodes a value in a `Vec` using default configuration
#[inline]
pub async fn encode<E: Encode + ?Sized>(value: &E) -> Vec<u8> {
    DEFAULT_CONFIG.encode(value).await
}

/// Writes encoded byte array to writer and returns the number of bytes written
#[inline]
pub async fn encode_to<E: Encode + ?Sized, W: Write + Unpin + Send>(
    value: &E,
    writer: W,
) -> Result<usize> {
    DEFAULT_CONFIG.encode_to(value, writer).await
}

/// Decodes a value from bytes using default configuration
#[inline]
pub async fn decode<D: Decode, T: AsRef<[u8]>>(bytes: T) -> Result<D> {
    DEFAULT_CONFIG.decode(bytes).await
}

/// Decodes values from reader
#[inline]
pub async fn decode_from<D: Decode, R: Read + Unpin + Send>(reader: R) -> Result<D> {
    DEFAULT_CONFIG.decode_from(reader).await
}

#[cfg(test)]
#[cfg(not(feature = "tokio"))]
mod tests {
    use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
    use std::{
        collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque},
        ffi::CString,
    };

    use futures_executor as executor;
    use rand::random;

    use crate::{decode, encode, Encode};

    macro_rules! primitive_test {
        ($type: ty, $name: ident) => {
            #[test]
            fn $name() {
                executor::block_on(async {
                    let original = random::<$type>();
                    let encoded = encode(&original).await;
                    assert_eq!(original.size(), encoded.len());
                    let decoded: $type = decode(&encoded).await.unwrap();
                    assert_eq!(original, decoded, "Invalid encoding/decoding");
                });
            }
        };
    }

    primitive_test!(u8, u8_test);
    primitive_test!(u16, u16_test);
    primitive_test!(u32, u32_test);
    primitive_test!(u64, u64_test);
    primitive_test!(u128, u128_test);

    primitive_test!(i8, i8_test);
    primitive_test!(i16, i16_test);
    primitive_test!(i32, i32_test);
    primitive_test!(i64, i64_test);
    primitive_test!(i128, i128_test);

    primitive_test!(usize, usize_test);
    primitive_test!(isize, isize_test);
    primitive_test!(bool, bool_test);
    primitive_test!(char, char_test);

    primitive_test!(f32, f32_test);
    primitive_test!(f64, f64_test);

    primitive_test!(NonZeroU8, non_zero_u8_test);
    primitive_test!(NonZeroU16, non_zero_u16_test);
    primitive_test!(NonZeroU32, non_zero_u32_test);
    primitive_test!(NonZeroU64, non_zero_u64_test);
    primitive_test!(NonZeroU128, non_zero_u128_test);
    primitive_test!(NonZeroUsize, non_zero_usize_test);

    primitive_test!([u8; 32], u8_arr_test);
    primitive_test!([u16; 32], u16_arr_test);
    primitive_test!([u32; 32], u32_arr_test);
    primitive_test!([u64; 32], u64_arr_test);
    primitive_test!([u128; 32], u128_arr_test);

    primitive_test!([i8; 32], i8_arr_test);
    primitive_test!([i16; 32], i16_arr_test);
    primitive_test!([i32; 32], i32_arr_test);
    primitive_test!([i64; 32], i64_arr_test);
    primitive_test!([i128; 32], i128_arr_test);

    primitive_test!([usize; 32], usize_arr_test);
    primitive_test!([isize; 32], isize_arr_test);
    primitive_test!([bool; 32], bool_arr_test);
    primitive_test!([char; 32], char_arr_test);

    primitive_test!([f32; 32], f32_arr_test);
    primitive_test!([f64; 32], f64_arr_test);

    #[test]
    fn option_none_test() {
        executor::block_on(async {
            let original: Option<u8> = None;
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: Option<u8> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn option_some_test() {
        executor::block_on(async {
            let original: Option<u8> = Some(random());
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: Option<u8> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn result_ok_test() {
        executor::block_on(async {
            let original: Result<u8, u8> = Ok(random());
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: Result<u8, u8> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn result_err_test() {
        executor::block_on(async {
            let original: Result<u8, u8> = Err(random());
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: Result<u8, u8> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn fixed_arr_test() {
        executor::block_on(async {
            let original = [1i32, 2i32, 3i32];
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: [i32; 3] = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn vec_test() {
        executor::block_on(async {
            let original = vec![1, 2, 3];
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: Vec<i32> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn slice_test() {
        executor::block_on(async {
            let original = [1i32, 2i32, 3i32];
            let encoded = encode(&original[..]).await;
            assert_eq!(original[..].size(), encoded.len());
            let decoded: Vec<i32> = decode(&encoded).await.unwrap();
            assert_eq!(original.to_vec(), decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn string_test() {
        executor::block_on(async {
            let original = "hello";
            let encoded = encode(original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: String = decode(&encoded).await.unwrap();
            assert_eq!(original.to_string(), decoded, "Invalid encoding/decoding");
        })
    }

    #[test]
    fn c_string_test() {
        executor::block_on(async {
            let original = CString::new("hello").unwrap();
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: CString = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        })
    }

    #[test]
    fn vec_string_test() {
        executor::block_on(async {
            let original = vec!["hello".to_string(), "world".to_string()];
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: Vec<String> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        })
    }

    #[test]
    fn box_test() {
        executor::block_on(async {
            let original = Box::new("10".to_string());
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: Box<String> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn tuple_test() {
        executor::block_on(async {
            let original = ("hello".to_string(), 25u8, 100i32);
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: (String, u8, i32) = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn vec_deque_test() {
        executor::block_on(async {
            let mut original = VecDeque::with_capacity(3);
            original.push_back(1);
            original.push_back(2);
            original.push_back(3);
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: VecDeque<i32> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn linked_list_test() {
        executor::block_on(async {
            let mut original = LinkedList::new();
            original.push_back(1);
            original.push_back(2);
            original.push_back(3);
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: LinkedList<i32> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn hash_set_test() {
        executor::block_on(async {
            let mut original = HashSet::with_capacity(3);
            original.insert(1);
            original.insert(2);
            original.insert(3);
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: HashSet<i32> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn btree_set_test() {
        executor::block_on(async {
            let mut original = BTreeSet::new();
            original.insert(3);
            original.insert(1);
            original.insert(2);
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: BTreeSet<i32> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn binary_heap_test() {
        executor::block_on(async {
            let mut original: BinaryHeap<i32> = BinaryHeap::new();
            original.push(3);
            original.push(1);
            original.push(2);
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: BinaryHeap<i32> = decode(&encoded).await.unwrap();
            let new_encoded = encode(&decoded).await;
            assert_eq!(encoded, new_encoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn hash_map_test() {
        executor::block_on(async {
            let mut original = HashMap::with_capacity(3);
            original.insert(1, "Hello".to_owned());
            original.insert(2, "World".to_owned());
            original.insert(3, "!".to_owned());
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: HashMap<i32, String> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }

    #[test]
    fn btree_map_test() {
        executor::block_on(async {
            let mut original = BTreeMap::new();
            original.insert(1, "Hello".to_owned());
            original.insert(2, "World".to_owned());
            original.insert(3, "!".to_owned());
            let encoded = encode(&original).await;
            assert_eq!(original.size(), encoded.len());
            let decoded: BTreeMap<i32, String> = decode(&encoded).await.unwrap();
            assert_eq!(original, decoded, "Invalid encoding/decoding");
        });
    }
}