Skip to content

Commit

Permalink
Rename numeric attribute to auto_operators; doc cleanups (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu committed Dec 20, 2023
1 parent dff40b2 commit 7e6ca67
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ println!("{}", timestamp); // Timestamp(1701620628123456789)

## Features

- Derive trait:
- `StrongType`: Create a named strong type. The macro automatically implement common traits like `Clone`, `Debug`, `Default`, `PartialEq`, `PartialOrd`, `Send`, `Sync`, and `Display` (unless the `custom_display` attribute is used to override it). Additionally, depending on the underlying data type, strong-typed structs may also implement `Copy`, `Eq`, `Ord`, `Hash`. For example, if the underlying type is a primitive data type like `i32` or `bool`, these additional traits will be implemented. This allows the strong types to inherit useful behaviors from their underlying types, while still maintaining their distinct identity at the type level.
- **Derive trait:**
- `StrongType`: Create a named strong type.
- The macro automatically implement common traits like `Clone`, `Debug`, `Default`, `PartialEq`, `PartialOrd`, `Send`, and `Sync`. It also implements `Display` by default, unless overridden by the custom_display attribute.
- Conditionally, based on the underlying data type, traits like `Copy`, `Eq`, `Ord`, `Hash` may also be implemented. For primitive data types like `i32` or `bool`, these additional traits will be automatically included.
- Numeric types (integer and floating-point) additionally implement methods like `min()`, `max()`, and, for floating-point types, `nan()`.

- Attributes:
- `numeric`: Extend `StrongType` with arithmetic/logical operations.
- `custom_display`: Provides flexibility for users to manually implement `Display` instead of using the default display format.
- **Attributes:**
- `auto_operators`: Automatically implements relevant arithmetic (for numeric types) or logical (for boolean types) operators.
- `custom_display`: Allows users to manually implement the `Display` trait, providing an alternative to the default display format.

## Installation
Add `strong-type` to your `Cargo.toml`:
Expand Down Expand Up @@ -87,7 +90,7 @@ assert_eq!(map.len(), 2);
use strong_type::StrongType;

#[derive(StrongType)]
#[numeric]
#[auto_operators]
struct Second(i32);

let x = Second::new(2);
Expand All @@ -108,7 +111,7 @@ assert_eq!(x + y, Second(5));
use strong_type::StrongType;

#[derive(StrongType)]
#[numeric]
#[auto_operators]
struct IsTrue(bool);

let x = IsTrue::new(true);
Expand Down
2 changes: 1 addition & 1 deletion strong-type-derive/src/detail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ pub(crate) use min_max::implement_min_max;
pub(crate) use nan::implement_nan;
pub(crate) use negate::implement_negate;
pub(crate) use underlying_type::{get_type_group, get_type_ident, UnderlyingTypeGroup};
pub(crate) use utils::{has_custom_display, has_numeric};
pub(crate) use utils::{has_auto_operators, has_custom_display};
4 changes: 2 additions & 2 deletions strong-type-derive/src/detail/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use syn::DeriveInput;

pub(crate) fn has_numeric(input: &DeriveInput) -> bool {
pub(crate) fn has_auto_operators(input: &DeriveInput) -> bool {
input
.attrs
.iter()
.any(|attr| attr.path().is_ident("numeric"))
.any(|attr| attr.path().is_ident("auto_operators"))
}

pub(crate) fn has_custom_display(input: &DeriveInput) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion strong-type-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use syn::{parse_macro_input, DeriveInput};

use crate::strong_type::expand_strong_type;

#[proc_macro_derive(StrongType, attributes(numeric, custom_display))]
#[proc_macro_derive(StrongType, attributes(auto_operators, custom_display))]
pub fn strong_type(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
expand_strong_type(input).into()
Expand Down
6 changes: 3 additions & 3 deletions strong-type-derive/src/strong_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::detail::{
get_type_group, get_type_ident, has_custom_display, has_numeric, implement_arithmetic,
get_type_group, get_type_ident, has_auto_operators, has_custom_display, implement_arithmetic,
implement_basic, implement_basic_primitive, implement_basic_string, implement_bool_ops,
implement_display, implement_hash, implement_min_max, implement_nan, implement_negate,
UnderlyingTypeGroup,
Expand Down Expand Up @@ -61,7 +61,7 @@ pub(super) fn expand_strong_type(input: DeriveInput) -> TokenStream {
}
}

if has_numeric(&input) {
if has_auto_operators(&input) {
match &group {
UnderlyingTypeGroup::Int | UnderlyingTypeGroup::Float => {
ast.extend(implement_arithmetic(name));
Expand All @@ -73,7 +73,7 @@ pub(super) fn expand_strong_type(input: DeriveInput) -> TokenStream {
UnderlyingTypeGroup::Bool => {
ast.extend(implement_bool_ops(name));
}
_ => panic!("Non-numeric type: {value_type}"),
_ => {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod tests {
#[test]
fn test_int_arithmetic() {
#[derive(StrongType)]
#[numeric]
#[auto_operators]
struct Second(i32);

let x = Second::new(2);
Expand Down Expand Up @@ -104,7 +104,7 @@ mod tests {
#[test]
fn test_float_arithmetic() {
#[derive(StrongType)]
#[numeric]
#[auto_operators]
struct Second(f64);

let x = Second::new(2.0);
Expand Down Expand Up @@ -168,7 +168,7 @@ mod tests {
#[test]
fn test_bool_ops() {
#[derive(StrongType)]
#[numeric]
#[auto_operators]
struct IsTrue(bool);

let x = IsTrue::new(true);
Expand Down
2 changes: 1 addition & 1 deletion strong-type-tests/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod auto_operators;
mod display;
mod numeric;
mod strong_type;

0 comments on commit 7e6ca67

Please sign in to comment.