nom nom nom
-rw-r--r--.gitignore1
-rw-r--r--Cargo.toml9
-rw-r--r--LICENSE21
-rw-r--r--src/lib.rs72
4 files changed, 103 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ffa3bbd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+Cargo.lock \ No newline at end of file
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..1d61984
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "bites"
+version = "0.0.0"
+edition = "2021"
+authors = ["bend-n <[email protected]>"]
+license = "MIT"
+description = "bites of tasty numbers"
+repository = "https://github.com/bend-n/bites"
+exclude = [".gitignore"]
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..2f002a4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 bendn
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..f30a192
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,72 @@
+//! tired of writing endless UNDERSCORES?
+//! i have the crate for you!
+//!
+//! [`to_le_bytes`](u32::to_le_bytes) no more! now, with one simple function, you can [`le`]`(n)` to easily eat all of those bites.
+//! ```
+//! bites::le(5);
+//! bites::be(25);
+//! bites::ne(421);
+//! ```
+#![forbid(unsafe_code)]
+#![allow(incomplete_features)]
+#![feature(generic_const_exprs)]
+
+/// Convert a number to its little endian bites.
+/// ```
+/// # assert_eq!(
+/// bites::le(5u32)
+/// # , 5u32.to_le_bytes()
+/// # );
+/// ```
+pub fn le<T: Bites>(n: T) -> [u8; <T as Bites>::SIZE] {
+ n.le()
+}
+
+/// Convert a number to its big endian bites.
+/// ```
+/// # assert_eq!(
+/// bites::be(42u16)
+/// # , 42u16.to_be_bytes()
+/// # );
+/// ```
+pub fn be<T: Bites>(n: T) -> [u8; <T as Bites>::SIZE] {
+ n.be()
+}
+
+/// Convert a number to its native endian bites.
+/// ```
+/// # assert_eq!(
+/// bites::ne(7236284524343093608u64)
+/// # , 7236284524343093608u64.to_ne_bytes()
+/// # );
+/// ```
+pub fn ne<T: Bites>(n: T) -> [u8; <T as Bites>::SIZE] {
+ n.ne()
+}
+
+mod seal {
+ #[diagnostic::on_unimplemented(message = "not a number", label = "this is not a number")]
+ pub trait Seal {}
+}
+
+use seal::Seal;
+/// Convert a number to its bites.
+pub trait Bites: Seal
+where
+ Self: Sized,
+{
+ const SIZE: usize = std::mem::size_of::<Self>();
+ fn le(self) -> [u8; Self::SIZE];
+ fn be(self) -> [u8; Self::SIZE];
+ fn ne(self) -> [u8; Self::SIZE];
+}
+
+macro_rules! le { ($($x:ident)+) => { $(
+ impl Seal for $x {}
+ impl Bites for $x {
+ #[inline] fn le(self) -> [u8; Self::SIZE] { self.to_le_bytes() }
+ #[inline] fn be(self) -> [u8; Self::SIZE] { self.to_be_bytes() }
+ #[inline] fn ne(self) -> [u8; Self::SIZE] { self.to_ne_bytes() }
+ }
+)+ }; }
+le!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128);