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
// When building on docs.rs with `--cfg docsrs`, annotate feature-gated items.
// Enable `alloc` types (Vec/String) for optional text helpers in this crate.
extern crate alloc;
/// Many byte-order-handling libraries focus on providing code to convert to and from big- or little-endian. However,
/// this requires users of those libraries to use a lot of explicit logic. This library uses the Rust type system to
/// enforce conversions invisibly, and also ensure that they are done consistently. A struct member can be read and written
/// simply using the standard From and Into trait methods (from() and into()). No explicit endian checks are required.
///
/// # Example 1:
///
/// ```rust
/// use simple_endian::*;
///
/// fn init() {
/// #[repr(C)]
/// struct BinPacket {
/// a: u64be,
/// b: u32be,
/// }
/// let mut bp = BinPacket{a: 0xfe.into(), b: 10.into()};
/// let new_a = bp.a.to_native() * 1234;
///
/// bp.a = new_a.into();
/// bp.b = 1234.into();
/// }
/// ```
///
/// Trying to write `bp.a = new_a;` causes an error because the type u64 can't be directly stored.
///
/// # Example 2: Writing a portable struct to a file.
///
/// Of course, just storing things in memory isn't that useful unless you write somewhere.
///
/// ```rust,no_run
/// use simple_endian::*;
/// use std::fs::File;
/// use std::io::prelude::*;
/// use std::mem::{transmute, size_of};
///
/// // We have to specify a representation in order to define the layout.
/// #[repr(C)]
/// struct BinBEStruct {
/// pub a: u64be,
/// b: u64be,
/// c: f64be,
/// }
///
/// fn main() -> std::io::Result<()> {
/// let bin_struct = BinBEStruct{a: 345.into(), b: 0xfee.into(), c: 9.345.into()};
///
/// let mut pos = 0;
/// let mut data_file = File::create(".test.bin")?;
/// let buffer = unsafe { transmute::<&BinBEStruct, &[u8; size_of::<BinBEStruct>()]>(&bin_struct) };
///
/// while pos < buffer.len() {
/// let bytes_written = data_file.write(&buffer[pos..])?;
/// pos += bytes_written;
/// }
/// Ok(())
/// }
/// ```
///
/// # Example 3: Mmapping a portable struct with the memmap crate.
///
/// You'll need to add memmap to your Cargo.toml to get this to actually work:
///
/// ```rust,no_run
/// extern crate memmap;
///
/// use std::{
/// io::Error,
/// fs::OpenOptions,
/// mem::size_of,
/// };
///
/// use memmap::MmapOptions;
/// use simple_endian::*;
///
/// #[repr(C)]
/// struct MyBEStruct {
/// header: u64be,
/// label: [u8; 8],
/// count: u128be,
/// }
///
/// fn main() -> Result<(), Error> {
/// let file = OpenOptions::new()
/// .read(true).write(true).create(true)
/// .open(".test.bin")?;
///
/// // Truncate the file to the size of the header.
/// file.set_len(size_of::<MyBEStruct>() as u64)?;
/// let mut mmap = unsafe { MmapOptions::new().map_mut(&file)? };
///
/// let mut ptr = mmap.as_mut_ptr() as *mut MyBEStruct;
///
/// unsafe {
/// // Set the magic number
/// (*ptr).header = 0xfeedface.into();
///
/// // Increment the counter each time we run.
/// (*ptr).count += 1.into();
///
/// (*ptr).label = *b"Iamhere!";
/// }
///
/// println!("done.");
/// Ok(())
/// }
/// ```
// (docs continue above)
/// The main part of the library. Contains the trait SpecificEndian<T> and BigEndian<T> and LittleEndian<T> structs, as well as the
/// implementation of those on the primitive types.
pub use *;
/// Types that do not change based on endianness and their implementations.
/// These types include the unit type, booleans, single-byte integers, and strings.
pub use SimpleEndian;
/// Text/code-unit conversion helpers (UTF-8/UTF-16/UTF-32 and fixed-size strings), behind feature flags.
pub use *;
/// Bitwise operations. These should be equally fast in any endian.
/// Ops for comparisons and ordering.
/// Shift operations.
/// General math operations.
/// Negations.
/// Formatter impls.
/// The shorthand types (e.g u64be, f32le, etc)
pub use *;
// Additional shorthand types (feature-gated).
pub use *;
// Optional proc-macro derives.
pub use Endianize;
/// Optional IO helpers gated by the `io` feature.
pub use *;
pub use *;