|
| 1 | +# endian-num |
| 2 | + |
| 3 | +[](https://crates.io/crates/endian-num) |
| 4 | +[](https://docs.rs/endian-num) |
| 5 | +[](https://github.com/rust-osdev/endian-num/actions/workflows/ci.yml) |
| 6 | + |
| 7 | +This crate provides the [`Be`] (big-endian) and [`Le`] (little-endian) byte-order-aware numeric types. |
| 8 | + |
| 9 | +[`Be`]: https://docs.rs/endian-num/latest/endian_num/struct.Be.html |
| 10 | +[`Le`]: https://docs.rs/endian-num/latest/endian_num/struct.Le.html |
| 11 | + |
| 12 | +The core API looks _roughly_ like this (correspondingly for `Be`): |
| 13 | + |
| 14 | +```rust |
| 15 | +#[repr(transparent)] |
| 16 | +pub struct<T> Le(pub T); |
| 17 | + |
| 18 | +impl Le<T: Integer> { |
| 19 | + pub const fn from_ne(n: T) -> Self; |
| 20 | + pub const fn from_be(n: Be<T>) -> Self; |
| 21 | + |
| 22 | + pub const fn to_ne(self) -> T; |
| 23 | + pub const fn to_be(self) -> Be<T>; |
| 24 | + |
| 25 | + pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()]; |
| 26 | + pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()]; |
| 27 | + pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()]; |
| 28 | + |
| 29 | + pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self; |
| 30 | + pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self; |
| 31 | + pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The types also implement appropriate traits from [`core::cmp`], [`core::convert`], [`core::fmt`], and [`core::ops`] and provide additional helper methods for computations. |
| 36 | + |
| 37 | +For API documentation, see the [docs]. |
| 38 | + |
| 39 | +[docs]: https://docs.rs/endian-num |
| 40 | +[`core::cmp`]: https://doc.rust-lang.org/stable/core/cmp/index.html |
| 41 | +[`core::convert`]: https://doc.rust-lang.org/stable/core/convert/index.html |
| 42 | +[`core::fmt`]: https://doc.rust-lang.org/stable/core/fmt/index.html |
| 43 | +[`core::ops`]: https://doc.rust-lang.org/stable/core/ops/index.html |
| 44 | + |
| 45 | + |
| 46 | +## License |
| 47 | + |
| 48 | +Licensed under either of |
| 49 | + |
| 50 | + * Apache License, Version 2.0 |
| 51 | + ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 52 | + * MIT license |
| 53 | + ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 54 | + |
| 55 | +at your option. |
| 56 | + |
| 57 | +### Contribution |
| 58 | + |
| 59 | +Unless you explicitly state otherwise, any contribution intentionally submitted |
| 60 | +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 61 | +dual licensed as above, without any additional terms or conditions. |
0 commit comments