Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

u8_tuple #14

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Cargo.lock
**/*.rs.bk
# build artifacts
*/target/*
**/.DS_Store
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
readme = "readme.md"
license-file = "LICENSE"
Expand All @@ -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"
Expand Down
Binary file removed byteserde_examples/.DS_Store
Binary file not shown.
82 changes: 82 additions & 0 deletions byteserde_types/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>` - will take the `u8` and return tuple struct with type of `NAME` argument.
/// * `From<Name>` - 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
Expand Down