From 8e19d6746afc2325316cf50982d337b4bd96c6fc Mon Sep 17 00:00:00 2001 From: softstream-link Date: Sun, 14 Jan 2024 12:52:56 -0500 Subject: [PATCH 1/2] u8_tuple --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 1 + byteserde_examples/.DS_Store | Bin 6148 -> 0 bytes byteserde_types/src/macros/mod.rs | 82 ++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) delete mode 100644 .DS_Store delete mode 100644 byteserde_examples/.DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 912c2048d33be4a96a3035c78412e1c5c3ccc666..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKJ5B>p3>-s>NHh@@<+}nmSViFkIRFqgR6#)`NYHJSb8$4rpMq$ii9{2PC41iU z^Ssqgv0ekP`Fnp0%m7U3jyQN2o1eRn?4mM8r1Op))_6nDct5KCJ>lGItni6FSwHz} zeBco;c*Vv~p8MU?YP;PyK_&&HfE17dQa}ovtU$HW>D9?Mh$sc5z_}{m--kwb># pIw%a$j)~EZx$$;<6Gd6qe9iN&a7YX~^Fb%-XTWulNrC@X;2Sk~7~}u| diff --git a/.gitignore b/.gitignore index 69876ed..cb939bf 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ Cargo.lock **/*.rs.bk # build artifacts */target/* +**/.DS_Store \ No newline at end of file diff --git a/byteserde_examples/.DS_Store b/byteserde_examples/.DS_Store deleted file mode 100644 index 049052f4bcfba88b0f03326087ac29364e18fc29..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKJ8l9o5S>X7LZaD{(pSh0ECeUW1qfNFh>$?(x8htJEpI*rD~FC0L2sn-v&Zvn z`4u}mBBIOh{!OG4kr~`jE;h7g`{s?kWJG~*obe^QeSiMk9*>76`}crxr*e{=d`N%G zw+$Ki0p4uOckG^oI!YPJ{}bi_;M)x;q%=%U$tXx^;Zp{U=E^NXj8)R_j diff --git a/byteserde_types/src/macros/mod.rs b/byteserde_types/src/macros/mod.rs index dc810af..b42aa19 100644 --- a/byteserde_types/src/macros/mod.rs +++ b/byteserde_types/src/macros/mod.rs @@ -598,6 +598,88 @@ macro_rules! numeric_tuple { }; } +#[macro_export] +macro_rules! byte_tuple { + ($NAME:ident, $TYPE:ty, $(#[$STRUCT_META:meta]),* ) => { + $(#[$STRUCT_META])* + pub struct $NAME($TYPE); + impl $NAME { + pub fn value(&self) -> $TYPE { + self.0 + } + pub fn new(value: $TYPE) -> Self { + $NAME(value) + } + } + impl From<$TYPE> for $NAME { + #[inline(always)] + fn from(v: $TYPE) -> Self { + $NAME(v) + } + } + impl From<$NAME> for $TYPE { + #[inline(always)] + fn from(v: $NAME) -> Self { + v.0 + } + } + impl std::fmt::Display for $NAME { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", &self.0) + } + } + }; +} + +/// Generates a `tuple` `struct` with a given name for managing a Byte type `u8` allocated on `stack`. +/// +/// # Arguments +/// * `NAME` - name of the struct to be generated +/// * `#[derive(...)]` -- list of additional valid rust derive traits +/// +/// # Derives +/// Note that provided implementation already includes several traits which `SHOULD NOT` be included in the derive list. +/// * `Display` - provides a human readable sting view of the `u8` value +/// +/// # From +/// Note that provided implementation already includes the following `From` implementations. +/// * `From` - will take the `u8` and return tuple struct with type of `NAME` argument. +/// * `From` - will take the `struct` type from the `NAME` argument and return the `u8` value. +/// +/// # Examples +/// ``` +/// # #[macro_use] extern crate byteserde_types; fn main() { +/// use byteserde_derive::ByteSerializeStack; +/// u8_tuple!(Byte, #[derive(ByteSerializeStack, PartialEq, Debug)]); +/// +/// let inp_num: Byte = 1_u8.into(); // from u8 +/// println!("inp_num: {:?}, {}", inp_num, inp_num); +/// assert_eq!(inp_num.value(), 1_u8); +/// +/// let inp_num: Byte = Byte::new(2); // using new +/// println!("inp_num: {:?}, {}", inp_num, inp_num); +/// assert_eq!(inp_num.value(), 2_u8); +/// +/// let inp_num: u8 = inp_num.into(); // to u8 +/// assert_eq!(inp_num, 2_u8); +/// # } +/// ``` +#[macro_export] +macro_rules! u8_tuple { + ($NAME:ident, $(#[$STRUCT_META:meta]),*) => { + $crate::byte_tuple!($NAME, u8, $(#[$STRUCT_META])*); + }; +} + +/// see [u8_tuple] for more details and examples. +#[macro_export] +macro_rules! i8_tuple { + ($NAME:ident, $(#[$STRUCT_META:meta]),*) => { + $crate::byte_tuple!($NAME, i8, $(#[$STRUCT_META])*); + }; +} + + /// Generates a `tuple` `struct` with a given name for managing a Numeric type `u16` allocated on `stack`. /// /// # Arguments From fab57da9e514ae0baa83e3ce20ecac6f0824f2e4 Mon Sep 17 00:00:00 2001 From: softstream-link Date: Sun, 14 Jan 2024 13:06:41 -0500 Subject: [PATCH 2/2] version bump --- Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6c6bea3..85cf5b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ resolver = "2" [workspace.package] # NOTE remember to update this and below [workspace.dependencies] for byteserde and byteserde_derive for the release -version = "0.6.1" +version = "0.6.2" authors = ["Softstream "] readme = "readme.md" license-file = "LICENSE" @@ -21,9 +21,9 @@ categories = ["encoding"] [workspace.dependencies] # workspace members -byteserde = { version = "0.6.1", path = "./byteserde" } -byteserde_derive = { version = "0.6.1", path = "./byteserde_derive" } -byteserde_types = { version = "0.6.1", path = "./byteserde_types" } +byteserde = { version = "0.6.2", path = "./byteserde" } +byteserde_derive = { version = "0.6.2", path = "./byteserde_derive" } +byteserde_types = { version = "0.6.2", path = "./byteserde_types" } bytes = "1.4"