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
/* ------------------------------------------------------------
    ByteArray
    Project.Github: "https://github.com/kerryeon/bytearray"
---------------------------------------------------------------
    Author:
        Name: "kerryeon"
        Email: "besqer996@gnu.ac.kr"
        Github: "https://github.com/kerryeon"
    Generated:
        Date: "3/8/2019"
------------------------------------------------------------ */

use crate::ByteArray;

/// This trait helps you handle user-defined structs via `ByteArray`.
///
/// # Examples
///
/// ```
/// use byte_array::{
///    BinaryBuilder,
///    ByteArray,
/// };
///
/// struct Foo(u64);
///
/// impl BinaryBuilder for Foo {
///    fn new() -> Self {
///        Self(123)
///    }
///
///    fn from_raw(ba: &mut ByteArray) -> Option<Self> {
///        Some(Self(ba.read_safe()?))
///    }
///    fn to_raw(&self, mut ba: &mut ByteArray) {
///        ba <<= &self.0;
///    }
///}
///
/// let mut ba = ByteArray::new();
///
/// // Write
/// ba <<=   &Foo::new();
/// ba <<=   &Foo(321);
/// ba.write(&Foo(222));
///
/// // Read
/// ba.seek_first();
/// assert_eq!(123, ba.read::<u64>());
/// assert_eq!(321, ba.read::<u64>());
/// assert_eq!(222, ba.read::<u64>());
/// ```
///
pub trait BinaryBuilder: Sized {

    /// Creates an empty Data.
    ///
    fn new() -> Self;

    /// Gets the data of the specified type in the `ByteArray`.
    ///
    /// # Panics
    /// Panics when unexpected EOF.
    ///
    fn from_raw(ba: &mut ByteArray) -> Option<Self>;

    /// Adds data to the `ByteArray`.
    ///
    fn to_raw(&self, ba: &mut ByteArray);
}