byte_order/order.rs
1/// An enumeration used in the creation of [`NumberReader`] and [`NumberWriter`]
2/// structures to specify what endianness their operations should be performed
3/// with.
4///
5/// # Examples
6///
7/// Constructing a [`NumberWriter`] which writes numbers with big-endian byte
8/// order:
9///
10/// ```
11/// use byte_order::{ByteOrder, NumberWriter};
12///
13/// let be_writer = NumberWriter::with_order(ByteOrder::BE, vec![]);
14/// ```
15///
16/// Likewise, constructing another [`NumberWriter`] which now writes numbers
17/// with little-endian byte order:
18///
19/// ```
20/// # use byte_order::{ByteOrder, NumberWriter};
21/// let le_writer = NumberWriter::with_order(ByteOrder::LE, vec![]);
22/// ```
23///
24/// [`NumberReader`]: crate::NumberReader
25/// [`NumberWriter`]: crate::NumberWriter
26#[derive(Debug, Eq, PartialEq)]
27pub enum ByteOrder {
28 BE,
29 LE,
30}
31
32impl ByteOrder {
33 /// The native-endian serialization of the target platform. This value will
34 /// be equal to [`ByteOrder::BE`] or [`ByteOrder::LE`].
35 ///
36 /// # Examples
37 ///
38 /// ```
39 /// use byte_order::ByteOrder;
40 ///
41 /// assert_eq!(
42 /// ByteOrder::NE,
43 /// if cfg!(target_endian = "big") { ByteOrder::BE } else { ByteOrder::LE }
44 /// );
45 /// ```
46 ///
47 /// [`ByteOrder::BE`]: ByteOrder::BE
48 /// [`ByteOrder::LE`]: ByteOrder::LE
49 pub const NE: ByteOrder = if cfg!(target_endian = "big") {
50 ByteOrder::BE
51 } else {
52 ByteOrder::LE
53 };
54}