Skip to content

Commit

Permalink
Add NonZero* synonyms
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Sas-Szymański committed Jun 23, 2024
1 parent 1c65430 commit 546e702
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 142 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl PartialEq for MyInt {
| Kind | Traits / Methods Implemented |
| ------- | ---------------------------- |
| Integer<br>`u8`, `u16`, `u32`, `u64`, `u128`, `usize`,<br>`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<br>`NonZeroU8`, `NonZeroU16`, `NonZeroU32`, `NonZeroU64`, `NonZeroU128`, `NonZeroUsize`,<br>`NonZeroI8`, `NonZeroI16`, `NonZeroI32`, `NonZeroI64`, `NonZeroI128`, `NonZeroIsize` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Copy`, `Hash`, `Debug`, `FromStr`, `From`, `AsRef`, `Deref` |
| Float<br>`f32`, `f64` | `PartialEq`, `PartialOrd`, `Clone`, `Default`, `Debug`, `Add`, `Sub`, `Mul`, `Div`, `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign`, `FromStr`, `From`, `AsRef`, `Deref` |
| String<br>`String`, `Box<str>` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Hash`, `Default`, `Debug`, `FromStr`, `From`, `AsRef`, `Deref`, `Borrow<str>`, `as_str()` |
| String<br>`&'static str` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Hash`, `Default`, `Debug`, `From`, `AsRef`, `Deref`, `Borrow<str>`, `as_str()` |
Expand Down
4 changes: 4 additions & 0 deletions src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ pub fn analyze(input: &DeriveInput) -> Option<Info> {
.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,
Expand Down
61 changes: 51 additions & 10 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Info {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Integer,
NonZeroInteger,
Float,
String,
BoxStr,
Expand All @@ -24,6 +25,7 @@ impl Kind {
pub fn is_clone(&self) -> bool {
match self {
Kind::Integer
| Kind::NonZeroInteger
| Kind::Float
| Kind::String
| Kind::BoxStr
Expand All @@ -35,14 +37,15 @@ 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,
}
}

pub fn is_debug(&self) -> bool {
match self {
Kind::Integer
| Kind::NonZeroInteger
| Kind::Float
| Kind::String
| Kind::BoxStr
Expand All @@ -60,41 +63,62 @@ 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,
}
}

pub fn is_partial_eq(&self) -> bool {
match self {
Kind::Integer
| Kind::NonZeroInteger
| Kind::Float
| Kind::String
| Kind::BoxStr
Expand All @@ -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
Expand All @@ -118,14 +143,20 @@ 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,
}
}

pub fn is_display(&self) -> bool {
match self {
Kind::Integer
| Kind::NonZeroInteger
| Kind::Float
| Kind::String
| Kind::BoxStr
Expand All @@ -137,22 +168,32 @@ 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,
}
}

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,
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
//! | Kind | Traits / Methods Implemented |
//! | ------- | ---------------------------- |
//! | Integer<br>`u8`, `u16`, `u32`, `u64`, `u128`, `usize`,<br>`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<br>`NonZeroU8`, `NonZeroU16`, `NonZeroU32`, `NonZeroU64`, `NonZeroU128`, `NonZeroUsize`,<br>`NonZeroI8`, `NonZeroI16`, `NonZeroI32`, `NonZeroI64`, `NonZeroI128`, `NonZeroIsize` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Copy`, `Hash`, `Debug`, `FromStr`, `From`, `AsRef`, `Deref` |
//! | Float<br>`f32`, `f64` | `PartialEq`, `PartialOrd`, `Clone`, `Default`, `Debug`, `Add`, `Sub`, `Mul`, `Div`, `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign`, `FromStr`, `From`, `AsRef`, `Deref` |
//! | String<br>`String`, `Box<str>` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Hash`, `Default`, `Debug`, `FromStr`, `From`, `AsRef`, `Deref`, `Borrow<str>`, `as_str()` |
//! | String<br>`&'static str` | `Eq`, `PartialEq`, `Ord`, `PartialOrd`, `Clone`, `Hash`, `Default`, `Debug`, `From`, `AsRef`, `Deref`, `Borrow<str>`, `as_str()` |
Expand Down
1 change: 1 addition & 0 deletions tests/cases/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Add `Box<str>` and `&'static str` synonyms
* Add `NonZero*` synonyms

## 0.1.3 (2024-06-05)

Expand Down
96 changes: 96 additions & 0 deletions tests/cases/pass-int.rs
Original file line number Diff line number Diff line change
@@ -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<Foo>) {}

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: core::ops::Add<T>>(_: T) {}
fn check_sub<T: core::ops::Add<T>>(_: T) {}
fn check_mul<T: core::ops::Add<T>>(_: T) {}
fn check_div<T: core::ops::Add<T>>(_: 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) {}
66 changes: 0 additions & 66 deletions tests/cases/pass-signed-int.rs

This file was deleted.

Loading

0 comments on commit 546e702

Please sign in to comment.