endify/lib.rs
1//! # endify
2//!
3//! Effortlessly convert structs between endianess-formats.
4//!
5//! ## Usage
6//! ```rust
7//! use endify::Endify;
8//!
9//! #[repr(C)]
10//! #[derive(Debug, Endify)]
11//! struct MyStruct {
12//! a: u32,
13//! b: u16,
14//! c: u8,
15//! }
16//!
17//! # fn read_from_disk() -> MyStruct {
18//! # MyStruct { a: 30u32.to_be(), b: 10u16.to_be(), c: 3u8.to_be() }
19//! # }
20//!
21//! fn main() {
22//! // stored on disk as `little-endian` format.
23//! let my_struct = read_from_disk();
24//!
25//! // convert all fields to `native-endian` format.
26//! let my_struct_native = my_struct.from_le();
27//! }
28//! ```
29
30#![no_std]
31#![allow(clippy::wrong_self_convention)]
32pub use endify_derive::Endify;
33
34/// A trait for converting between endianess-formts effortlessly.
35pub trait Endify {
36 /// Converts a struct to little-endian format.
37 fn to_le(self) -> Self;
38 /// Converts a struct to big-endian format.
39 fn to_be(self) -> Self;
40 /// Converts a struct stored in little-endian format to native-endian format.
41 fn from_le(self) -> Self;
42 /// Converts a struct stored in big-endian format to native-endian format.
43 fn from_be(self) -> Self;
44}