From 546e70228c8fb8cfc8dc8adc87ba5e6df15feec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Sas-Szyma=C5=84ski?= Date: Sun, 23 Jun 2024 19:02:08 +0200 Subject: [PATCH] Add NonZero* synonyms --- README.md | 1 + src/analyze.rs | 4 ++ src/info.rs | 61 ++++++++++++++++---- src/lib.rs | 1 + tests/cases/CHANGELOG.md | 1 + tests/cases/pass-int.rs | 96 ++++++++++++++++++++++++++++++++ tests/cases/pass-signed-int.rs | 66 ---------------------- tests/cases/pass-unsigned-int.rs | 66 ---------------------- 8 files changed, 154 insertions(+), 142 deletions(-) create mode 100644 tests/cases/pass-int.rs delete mode 100644 tests/cases/pass-signed-int.rs delete mode 100644 tests/cases/pass-unsigned-int.rs diff --git a/README.md b/README.md index 6d143c4..b467869 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ impl PartialEq for MyInt { | Kind | Traits / Methods Implemented | | ------- | ---------------------------- | | Integer
`u8`, `u16`, `u32`, `u64`, `u128`, `usize`,
`i8`, `i16`, `i32`, `i64`, `i128`, `isize` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Copy`, `Hash`, `Default`, `Debug`, `Add`, `Sub`, `Mul`, `Div`, `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign`, `FromStr`, `From`, `AsRef`, `Deref` | +| Integer
`NonZeroU8`, `NonZeroU16`, `NonZeroU32`, `NonZeroU64`, `NonZeroU128`, `NonZeroUsize`,
`NonZeroI8`, `NonZeroI16`, `NonZeroI32`, `NonZeroI64`, `NonZeroI128`, `NonZeroIsize` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Copy`, `Hash`, `Debug`, `FromStr`, `From`, `AsRef`, `Deref` | | Float
`f32`, `f64` | `PartialEq`, `PartialOrd`, `Clone`, `Default`, `Debug`, `Add`, `Sub`, `Mul`, `Div`, `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign`, `FromStr`, `From`, `AsRef`, `Deref` | | String
`String`, `Box` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Hash`, `Default`, `Debug`, `FromStr`, `From`, `AsRef`, `Deref`, `Borrow`, `as_str()` | | String
`&'static str` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Hash`, `Default`, `Debug`, `From`, `AsRef`, `Deref`, `Borrow`, `as_str()` | diff --git a/src/analyze.rs b/src/analyze.rs index 07591c6..d03e2dc 100644 --- a/src/analyze.rs +++ b/src/analyze.rs @@ -35,12 +35,16 @@ pub fn analyze(input: &DeriveInput) -> Option { .to_token_stream() .to_string() .replace("std :: string :: ", "") + .replace("std :: num :: ", "") .replace("std :: primitive :: ", "") .replace("core :: primitive :: ", "") .as_str() { "u8" | "u16" | "u32" | "u64" | "u128" | "usize" | "i8" | "i16" | "i32" | "i64" | "i128" | "isize" => Kind::Integer, + "NonZeroU8" | "NonZeroU16" | "NonZeroU32" | "NonZeroU64" | "NonZeroU128" + | "NonZeroUsize" | "NonZeroI8" | "NonZeroI16" | "NonZeroI32" | "NonZeroI64" + | "NonZeroI128" | "NonZeroIsize" => Kind::NonZeroInteger, "f32" | "f64" => Kind::Float, "String" => Kind::String, "Box < str >" => Kind::BoxStr, diff --git a/src/info.rs b/src/info.rs index c5c7ec5..0b860f8 100644 --- a/src/info.rs +++ b/src/info.rs @@ -12,6 +12,7 @@ pub struct Info { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Kind { Integer, + NonZeroInteger, Float, String, BoxStr, @@ -24,6 +25,7 @@ impl Kind { pub fn is_clone(&self) -> bool { match self { Kind::Integer + | Kind::NonZeroInteger | Kind::Float | Kind::String | Kind::BoxStr @@ -35,7 +37,7 @@ impl Kind { pub fn is_copy(&self) -> bool { match self { - Kind::Integer | Kind::Float | Kind::Char => true, + Kind::Integer | Kind::NonZeroInteger | Kind::Float | Kind::Char => true, Kind::String | Kind::BoxStr | Kind::StaticStr | Kind::Other => false, } } @@ -43,6 +45,7 @@ impl Kind { pub fn is_debug(&self) -> bool { match self { Kind::Integer + | Kind::NonZeroInteger | Kind::Float | Kind::String | Kind::BoxStr @@ -60,34 +63,54 @@ impl Kind { | Kind::BoxStr | Kind::StaticStr | Kind::Char => true, - Kind::Other => false, + Kind::NonZeroInteger | Kind::Other => false, } } pub fn is_deserialize(&self) -> bool { match self { - Kind::Integer | Kind::Float | Kind::String | Kind::BoxStr | Kind::Char => true, + Kind::Integer + | Kind::NonZeroInteger + | Kind::Float + | Kind::String + | Kind::BoxStr + | Kind::Char => true, Kind::Other | Kind::StaticStr => false, } } pub fn is_eq(&self) -> bool { match self { - Kind::Integer | Kind::String | Kind::BoxStr | Kind::StaticStr | Kind::Char => true, + Kind::Integer + | Kind::NonZeroInteger + | Kind::String + | Kind::BoxStr + | Kind::StaticStr + | Kind::Char => true, Kind::Float | Kind::Other => false, } } pub fn is_hash(&self) -> bool { match self { - Kind::Integer | Kind::String | Kind::BoxStr | Kind::StaticStr | Kind::Char => true, + Kind::Integer + | Kind::NonZeroInteger + | Kind::String + | Kind::BoxStr + | Kind::StaticStr + | Kind::Char => true, Kind::Float | Kind::Other => false, } } pub fn is_ord(&self) -> bool { match self { - Kind::Integer | Kind::String | Kind::BoxStr | Kind::StaticStr | Kind::Char => true, + Kind::Integer + | Kind::NonZeroInteger + | Kind::String + | Kind::BoxStr + | Kind::StaticStr + | Kind::Char => true, Kind::Float | Kind::Other => false, } } @@ -95,6 +118,7 @@ impl Kind { pub fn is_partial_eq(&self) -> bool { match self { Kind::Integer + | Kind::NonZeroInteger | Kind::Float | Kind::String | Kind::BoxStr @@ -107,6 +131,7 @@ impl Kind { pub fn is_partial_ord(&self) -> bool { match self { Kind::Integer + | Kind::NonZeroInteger | Kind::Float | Kind::String | Kind::BoxStr @@ -118,7 +143,12 @@ impl Kind { pub fn is_serialize(&self) -> bool { match self { - Kind::Integer | Kind::Float | Kind::String | Kind::BoxStr | Kind::Char => true, + Kind::Integer + | Kind::NonZeroInteger + | Kind::Float + | Kind::String + | Kind::BoxStr + | Kind::Char => true, Kind::Other | Kind::StaticStr => false, } } @@ -126,6 +156,7 @@ impl Kind { pub fn is_display(&self) -> bool { match self { Kind::Integer + | Kind::NonZeroInteger | Kind::Float | Kind::String | Kind::BoxStr @@ -137,7 +168,12 @@ impl Kind { pub fn is_from_str(&self) -> bool { match self { - Kind::Integer | Kind::Float | Kind::String | Kind::BoxStr | Kind::Char => true, + Kind::Integer + | Kind::NonZeroInteger + | Kind::Float + | Kind::String + | Kind::BoxStr + | Kind::Char => true, Kind::Other | Kind::StaticStr => false, } } @@ -145,14 +181,19 @@ impl Kind { pub fn is_number(&self) -> bool { match self { Kind::Integer | Kind::Float => true, - Kind::String | Kind::BoxStr | Kind::StaticStr | Kind::Char | Kind::Other => false, + Kind::NonZeroInteger + | Kind::String + | Kind::BoxStr + | Kind::StaticStr + | Kind::Char + | Kind::Other => false, } } pub fn is_string(&self) -> bool { match self { Kind::String | Kind::BoxStr | Kind::StaticStr => true, - Kind::Integer | Kind::Float | Kind::Char | Kind::Other => false, + Kind::Integer | Kind::NonZeroInteger | Kind::Float | Kind::Char | Kind::Other => false, } } } diff --git a/src/lib.rs b/src/lib.rs index 5ec63d2..0e1e560 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,7 @@ //! | Kind | Traits / Methods Implemented | //! | ------- | ---------------------------- | //! | Integer
`u8`, `u16`, `u32`, `u64`, `u128`, `usize`,
`i8`, `i16`, `i32`, `i64`, `i128`, `isize` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Copy`, `Hash`, `Default`, `Debug`, `Add`, `Sub`, `Mul`, `Div`, `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign`, `FromStr`, `From`, `AsRef`, `Deref` | +//! | Integer
`NonZeroU8`, `NonZeroU16`, `NonZeroU32`, `NonZeroU64`, `NonZeroU128`, `NonZeroUsize`,
`NonZeroI8`, `NonZeroI16`, `NonZeroI32`, `NonZeroI64`, `NonZeroI128`, `NonZeroIsize` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Copy`, `Hash`, `Debug`, `FromStr`, `From`, `AsRef`, `Deref` | //! | Float
`f32`, `f64` | `PartialEq`, `PartialOrd`, `Clone`, `Default`, `Debug`, `Add`, `Sub`, `Mul`, `Div`, `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign`, `FromStr`, `From`, `AsRef`, `Deref` | //! | String
`String`, `Box` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Hash`, `Default`, `Debug`, `FromStr`, `From`, `AsRef`, `Deref`, `Borrow`, `as_str()` | //! | String
`&'static str` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Hash`, `Default`, `Debug`, `From`, `AsRef`, `Deref`, `Borrow`, `as_str()` | diff --git a/tests/cases/CHANGELOG.md b/tests/cases/CHANGELOG.md index 76c4609..f072508 100644 --- a/tests/cases/CHANGELOG.md +++ b/tests/cases/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased * Add `Box` and `&'static str` synonyms +* Add `NonZero*` synonyms ## 0.1.3 (2024-06-05) diff --git a/tests/cases/pass-int.rs b/tests/cases/pass-int.rs new file mode 100644 index 0000000..0169169 --- /dev/null +++ b/tests/cases/pass-int.rs @@ -0,0 +1,96 @@ +use synonym::Synonym; +use std::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize}; + +macro_rules! check { + ($t:ty, $v:expr) => { + #[derive(Synonym)] + struct Foo($t); + + fn check_as_ref(_: impl AsRef<$t>) {} + fn check_from(_: impl From<$t>) {} + fn check_from_inner(_: impl From) {} + + check_partial_eq(Foo($v)); + check_eq(Foo($v)); + check_partial_ord(Foo($v)); + check_ord(Foo($v)); + check_clone(Foo($v)); + check_copy(Foo($v)); + check_hash(Foo($v)); + check_debug(Foo($v)); + check_display(Foo($v)); + check_as_ref(Foo($v)); + check_from(Foo($v)); + check_from_inner($v); + check_from_str(Foo($v)); + }; + (builtin; $t:ty, $v:expr) => { + { + check!($t, $v); + check_default(Foo($v)); + check_add(Foo($v)); + check_sub(Foo($v)); + check_mul(Foo($v)); + check_div(Foo($v)); + check_add_assign(Foo($v)); + check_sub_assign(Foo($v)); + check_mul_assign(Foo($v)); + check_div_assign(Foo($v)); + } + }; + (nonzero; $t:ty, $v:expr) => { + { + check!($t, $v); + } + } +} + +fn main() { + check!(builtin; u8, 1u8); + check!(builtin; u16, 1u16); + check!(builtin; u32, 1u32); + check!(builtin; u64, 1u64); + check!(builtin; u128, 1u128); + check!(builtin; usize, 1usize); + + check!(builtin; i8, 1i8); + check!(builtin; i16, 1i16); + check!(builtin; i32, 1i32); + check!(builtin; i64, 1i64); + check!(builtin; i128, 1i128); + check!(builtin; isize, 1isize); + + check!(nonzero; NonZeroU8, NonZeroU8::new(1).unwrap()); + check!(nonzero; NonZeroU16, NonZeroU16::new(1).unwrap()); + check!(nonzero; NonZeroU32, NonZeroU32::new(1).unwrap()); + check!(nonzero; NonZeroU64, NonZeroU64::new(1).unwrap()); + check!(nonzero; NonZeroU128, NonZeroU128::new(1).unwrap()); + check!(nonzero; NonZeroUsize, NonZeroUsize::new(1).unwrap()); + + check!(nonzero; NonZeroI8, NonZeroI8::new(1).unwrap()); + check!(nonzero; NonZeroI16, NonZeroI16::new(1).unwrap()); + check!(nonzero; NonZeroI32, NonZeroI32::new(1).unwrap()); + check!(nonzero; NonZeroI64, NonZeroI64::new(1).unwrap()); + check!(nonzero; NonZeroI128, NonZeroI128::new(1).unwrap()); + check!(nonzero; NonZeroIsize, NonZeroIsize::new(1).unwrap()); +} + +fn check_partial_eq(_: impl PartialEq) {} +fn check_eq(_: impl Eq) {} +fn check_partial_ord(_: impl PartialOrd) {} +fn check_ord(_: impl Ord) {} +fn check_clone(_: impl Clone) {} +fn check_copy(_: impl Copy) {} +fn check_hash(_: impl core::hash::Hash) {} +fn check_default(_: impl Default) {} +fn check_debug(_: impl core::fmt::Debug) {} +fn check_display(_: impl core::fmt::Display) {} +fn check_from_str(_: impl core::str::FromStr) {} +fn check_add>(_: T) {} +fn check_sub>(_: T) {} +fn check_mul>(_: T) {} +fn check_div>(_: T) {} +fn check_add_assign(_: impl core::ops::AddAssign) {} +fn check_sub_assign(_: impl core::ops::SubAssign) {} +fn check_mul_assign(_: impl core::ops::MulAssign) {} +fn check_div_assign(_: impl core::ops::DivAssign) {} \ No newline at end of file diff --git a/tests/cases/pass-signed-int.rs b/tests/cases/pass-signed-int.rs deleted file mode 100644 index 7020c32..0000000 --- a/tests/cases/pass-signed-int.rs +++ /dev/null @@ -1,66 +0,0 @@ -use synonym::Synonym; - -macro_rules! check { - ($t:ty, $v:expr) => { - { - #[derive(Synonym)] - struct Foo($t); - - fn check_as_ref(_: impl AsRef<$t>) {} - fn check_from(_: impl From<$t>) {} - fn check_from_inner(_: impl From) {} - - check_partial_eq(Foo($v)); - check_eq(Foo($v)); - check_partial_ord(Foo($v)); - check_ord(Foo($v)); - check_clone(Foo($v)); - check_copy(Foo($v)); - check_hash(Foo($v)); - check_default(Foo($v)); - check_debug(Foo($v)); - check_display(Foo($v)); - check_as_ref(Foo($v)); - check_from(Foo($v)); - check_from_inner($v); - check_from_str(Foo($v)); - check_add(Foo($v)); - check_sub(Foo($v)); - check_mul(Foo($v)); - check_div(Foo($v)); - check_add_assign(Foo($v)); - check_sub_assign(Foo($v)); - check_mul_assign(Foo($v)); - check_div_assign(Foo($v)); - } - } -} - -fn main() { - check!(i8, 1i8); - check!(i16, 1i16); - check!(i32, 1i32); - check!(i64, 1i64); - check!(i128, 1i128); - check!(isize, 1isize); -} - -fn check_partial_eq(_: impl PartialEq) {} -fn check_eq(_: impl Eq) {} -fn check_partial_ord(_: impl PartialOrd) {} -fn check_ord(_: impl Ord) {} -fn check_clone(_: impl Clone) {} -fn check_copy(_: impl Copy) {} -fn check_hash(_: impl core::hash::Hash) {} -fn check_default(_: impl Default) {} -fn check_debug(_: impl core::fmt::Debug) {} -fn check_display(_: impl core::fmt::Display) {} -fn check_from_str(_: impl core::str::FromStr) {} -fn check_add>(_: T) {} -fn check_sub>(_: T) {} -fn check_mul>(_: T) {} -fn check_div>(_: T) {} -fn check_add_assign(_: impl core::ops::AddAssign) {} -fn check_sub_assign(_: impl core::ops::SubAssign) {} -fn check_mul_assign(_: impl core::ops::MulAssign) {} -fn check_div_assign(_: impl core::ops::DivAssign) {} diff --git a/tests/cases/pass-unsigned-int.rs b/tests/cases/pass-unsigned-int.rs deleted file mode 100644 index a5e0107..0000000 --- a/tests/cases/pass-unsigned-int.rs +++ /dev/null @@ -1,66 +0,0 @@ -use synonym::Synonym; - -macro_rules! check { - ($t:ty, $v:expr) => { - { - #[derive(Synonym)] - struct Foo($t); - - fn check_as_ref(_: impl AsRef<$t>) {} - fn check_from(_: impl From<$t>) {} - fn check_from_inner(_: impl From) {} - - check_partial_eq(Foo($v)); - check_eq(Foo($v)); - check_partial_ord(Foo($v)); - check_ord(Foo($v)); - check_clone(Foo($v)); - check_copy(Foo($v)); - check_hash(Foo($v)); - check_default(Foo($v)); - check_debug(Foo($v)); - check_display(Foo($v)); - check_as_ref(Foo($v)); - check_from(Foo($v)); - check_from_inner($v); - check_from_str(Foo($v)); - check_add(Foo($v)); - check_sub(Foo($v)); - check_mul(Foo($v)); - check_div(Foo($v)); - check_add_assign(Foo($v)); - check_sub_assign(Foo($v)); - check_mul_assign(Foo($v)); - check_div_assign(Foo($v)); - } - } -} - -fn main() { - check!(u8, 1u8); - check!(u16, 1u16); - check!(u32, 1u32); - check!(u64, 1u64); - check!(u128, 1u128); - check!(usize, 1usize); -} - -fn check_partial_eq(_: impl PartialEq) {} -fn check_eq(_: impl Eq) {} -fn check_partial_ord(_: impl PartialOrd) {} -fn check_ord(_: impl Ord) {} -fn check_clone(_: impl Clone) {} -fn check_copy(_: impl Copy) {} -fn check_hash(_: impl core::hash::Hash) {} -fn check_default(_: impl Default) {} -fn check_debug(_: impl core::fmt::Debug) {} -fn check_display(_: impl core::fmt::Display) {} -fn check_from_str(_: impl core::str::FromStr) {} -fn check_add>(_: T) {} -fn check_sub>(_: T) {} -fn check_mul>(_: T) {} -fn check_div>(_: T) {} -fn check_add_assign(_: impl core::ops::AddAssign) {} -fn check_sub_assign(_: impl core::ops::SubAssign) {} -fn check_mul_assign(_: impl core::ops::MulAssign) {} -fn check_div_assign(_: impl core::ops::DivAssign) {} \ No newline at end of file