1
#![deny(missing_docs)]
2

            
3
//! Reading and writing of binary data.
4

            
5
pub mod read;
6
pub mod write;
7

            
8
/// Calculate the length required to 32-bit (long) align data of length `len`
9
///
10
/// Example:
11
///
12
/// ```
13
/// use allsorts::binary::long_align;
14
///
15
/// let length = 123;
16
/// let padded_length = long_align(length);
17
/// assert_eq!(padded_length, 124);
18
/// ```
19
pub const fn long_align(len: usize) -> usize {
20
    len.div_ceil(4) * 4
21
}
22

            
23
/// Calculate the length required to 16-bit (word) align data of length `len`
24
///
25
/// Example:
26
///
27
/// ```
28
/// use allsorts::binary::word_align;
29
///
30
/// let length = 123;
31
/// let padded_length = word_align(length);
32
/// assert_eq!(padded_length, 124);
33
/// ```
34
pub const fn word_align(len: usize) -> usize {
35
    len.div_ceil(2) * 2
36
}
37

            
38
/// Unsigned 8-bit binary type.
39
#[derive(Copy, Clone)]
40
pub enum U8 {}
41

            
42
/// Signed 8-bit binary type.
43
#[derive(Copy, Clone)]
44
pub enum I8 {}
45

            
46
/// Unsigned 16-bit big endian binary type.
47
#[derive(Copy, Clone)]
48
pub enum U16Be {}
49

            
50
/// Signed 16-bit big endian binary type.
51
#[derive(Copy, Clone)]
52
pub enum I16Be {}
53

            
54
/// Unsigned 24-bit (3 bytes) big endian binary type.
55
#[derive(Copy, Clone)]
56
pub enum U24Be {}
57

            
58
/// Unsigned 32-bit big endian binary type.
59
#[derive(Copy, Clone)]
60
pub enum U32Be {}
61

            
62
/// Signed 32-bit big endian binary type.
63
#[derive(Copy, Clone)]
64
pub enum I32Be {}
65

            
66
/// Unsigned 64-bit binary type.
67
#[derive(Copy, Clone)]
68
pub enum U64Be {}
69

            
70
/// Signed 64-bit binary type.
71
#[derive(Copy, Clone)]
72
pub enum I64Be {}