From b0e24e1342851ffe31a3bc7d07a24fe46c320216 Mon Sep 17 00:00:00 2001 From: Yun-Jhong Wu Date: Sun, 3 Dec 2023 12:38:12 -0600 Subject: [PATCH] Rename crate (#10) --- Cargo.toml | 4 +- README.md | 36 +++++++-------- src/lib.rs | 16 +++---- src/{named_type.rs => strong_type.rs} | 2 +- tests/{named_type.rs => strong_type.rs} | 58 ++++++++++++------------- tests/tests.rs | 2 +- 6 files changed, 59 insertions(+), 59 deletions(-) rename src/{named_type.rs => strong_type.rs} (95%) rename tests/{named_type.rs => strong_type.rs} (82%) diff --git a/Cargo.toml b/Cargo.toml index d97a106..d4df463 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] -name = "named-type" +name = "strong-type" description = "Procedural macros for naming and strong-typing pritimives and strings" version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" -repository = "https://github.com/yunjhongwu/named-type" +repository = "https://github.com/yunjhongwu/strong-type" readme = "README.md" [lib] diff --git a/README.md b/README.md index 173a35c..894c30d 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# named-type -`named-type` is a Rust crate providing macros to create strongly typed and named values. It simplifies the process of defining types that are distinct at the type level but share underlying data structures. +# strong-type +`strong-type` is a Rust crate providing macros to create strongly typed and named values. It simplifies the process of defining types that are distinct at the type level but share underlying data structures. ```rust -use named_type::NamedType; +use strong_type::StrongType; -#[derive(NamedType)] +#[derive(StrongType)] struct Timestamp(i64); let timestamp = Timestamp(1701620628123456789); @@ -11,26 +11,26 @@ println!("{}", timestamp); // Timestamp(1701620628123456789) ``` ## Features -- `NamedType`: Create a named strong type. -- `NamedNumeric`: Extend `NamedType` with arithmetic operations. +- `StrongType`: Create a named strong type. +- `StrongNumericType`: Extend `StrongType` with arithmetic operations. - `custom_display`: Allow users to manually implement `Display` instead of using the default display format. ## Supported underlying types: - - Both `NamedType` and `NamedNumeric`: + - Both `StrongType` and `StrongNumericType`: - `i8`, `i16`, `i32`, `i64`, `i128`, `isize` - `u8`, `u16`, `u32`, `u64`, `u128`, `usize` - `f32`, `f64` - - Only `NamedType`: + - Only `StrongType`: - `bool` - `char` - `String` ## Examples -#### Create a named type: +#### Create a named strong type: ```rust -use named_type::NamedType; +use strong_type::StrongType; -#[derive(NamedType)] +#[derive(StrongType)] struct Tag(String); let _ = Tag("dev".to_string()); @@ -41,13 +41,13 @@ let tag: Tag = "dev".into(); #### Strong type: ```rust -use named_type::NamedType; +use strong_type::StrongType; use std::any::Any; -#[derive(NamedType)] +#[derive(StrongType)] struct Second(i32); -#[derive(NamedType)] +#[derive(StrongType)] struct Minute(i32); let x = Second(2); @@ -61,9 +61,9 @@ assert_ne!(y.type_id(), z.type_id()); // Different type: Second versus Minute #### Named type with arithmetic operations: ```rust -use named_type::NamedNumeric; +use strong_type::StrongNumericType; -#[derive(NamedNumeric)] +#[derive(StrongNumericType)] struct Second(i32); let x = Second(2); @@ -79,9 +79,9 @@ assert_eq!(x + y, Second(5)); #### Named type with `custom_display`: ```rust -use named_type::NamedNumeric; +use strong_type::StrongNumericType; -#[derive(NamedNumeric)] +#[derive(StrongNumericType)] #[custom_display] struct Second(f64); diff --git a/src/lib.rs b/src/lib.rs index 3360508..2a1ed38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,19 +1,19 @@ mod detail; -mod named_type; +mod strong_type; use proc_macro::TokenStream; use syn::{parse_macro_input, DeriveInput}; -use crate::named_type::expand_named_type; +use crate::strong_type::expand_strong_type; -#[proc_macro_derive(NamedType, attributes(custom_display))] -pub fn named_type(input: TokenStream) -> TokenStream { +#[proc_macro_derive(StrongType, attributes(custom_display))] +pub fn strong_type(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); - expand_named_type(input, false).into() + expand_strong_type(input, false).into() } -#[proc_macro_derive(NamedNumeric, attributes(custom_display))] -pub fn named_numeric(input: TokenStream) -> TokenStream { +#[proc_macro_derive(StrongNumericType, attributes(custom_display))] +pub fn strong_numeric_type(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); - expand_named_type(input, true).into() + expand_strong_type(input, true).into() } diff --git a/src/named_type.rs b/src/strong_type.rs similarity index 95% rename from src/named_type.rs rename to src/strong_type.rs index a8e4649..3435328 100644 --- a/src/named_type.rs +++ b/src/strong_type.rs @@ -7,7 +7,7 @@ use proc_macro2::TokenStream; use quote::quote; use syn::DeriveInput; -pub(super) fn expand_named_type(input: DeriveInput, impl_arithmetic: bool) -> TokenStream { +pub(super) fn expand_strong_type(input: DeriveInput, impl_arithmetic: bool) -> TokenStream { let name = &input.ident; let value_type = get_type_ident(&input); let group = get_type(value_type); diff --git a/tests/named_type.rs b/tests/strong_type.rs similarity index 82% rename from tests/named_type.rs rename to tests/strong_type.rs index 8deaa71..afc6168 100644 --- a/tests/named_type.rs +++ b/tests/strong_type.rs @@ -1,84 +1,84 @@ #[cfg(test)] mod tests { - use named_type::{NamedNumeric, NamedType}; use std::any::Any; use std::collections::HashSet; use std::fmt::Display; + use strong_type::{StrongNumericType, StrongType}; #[test] fn test_basic() { - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedI8(i8); let _ = NamedI8(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedI16(i16); let _ = NamedI16(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedI32(i32); let _ = NamedI32(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedI64(i64); let _ = NamedI64(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedI128(i128); let _ = NamedI128(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedISize(isize); let _ = NamedISize(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedU8(u8); let _ = NamedU8(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedU16(u16); let _ = NamedU16(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedU32(u32); let _ = NamedU32(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedU64(u64); let _ = NamedU64(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedU128(u128); let _ = NamedU128(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedUSize(usize); let _ = NamedUSize(1); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedF32(f32); let _ = NamedF32(1.0); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct NamedF64(f64); let _ = NamedF64(1.0); - #[derive(NamedType)] + #[derive(StrongType)] struct NamedBool(bool); let _ = NamedBool(true); - #[derive(NamedType)] + #[derive(StrongType)] struct NamedChar(char); let _ = NamedChar('a'); - #[derive(NamedType)] + #[derive(StrongType)] struct NamedString(String); let _ = NamedString("string".to_string()); } #[test] fn test_int_arithmetic() { - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct Second(i32); let x = Second(2); @@ -109,7 +109,7 @@ mod tests { #[test] fn test_float_arithmetic() { - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct Second(f64); let x = Second(2.5); @@ -140,10 +140,10 @@ mod tests { #[test] fn test_strong_type() { - #[derive(NamedType)] + #[derive(StrongType)] struct Second(i32); - #[derive(NamedType)] + #[derive(StrongType)] struct Minute(i32); let x = Second(2); @@ -156,7 +156,7 @@ mod tests { #[test] fn test_float_nan() { - #[derive(NamedType)] + #[derive(StrongType)] struct Meter(f64); let y = Meter::nan(); @@ -170,7 +170,7 @@ mod tests { #[test] fn test_bool_negate() { - #[derive(NamedType)] + #[derive(StrongType)] struct IsTrue(bool); let is_true = IsTrue(true); @@ -181,7 +181,7 @@ mod tests { #[test] fn test_string_ctor() { - #[derive(NamedType)] + #[derive(StrongType)] struct Meter(String); let _: Meter = "String".to_string().into(); @@ -192,12 +192,12 @@ mod tests { #[test] fn test_display() { - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] struct Meter(i32); assert_eq!(format!("{}", Meter(-2)), "Meter(-2)"); assert_eq!(format!("{:?}", Meter(-2)), "Meter { value: -2 }"); - #[derive(NamedNumeric)] + #[derive(StrongNumericType)] #[custom_display] struct Mile(f64); @@ -215,7 +215,7 @@ mod tests { #[test] fn test_hash() { - #[derive(NamedType)] + #[derive(StrongType)] struct Sign(bool); let mut map = HashSet::::new(); @@ -224,7 +224,7 @@ mod tests { map.insert(Sign(false)); assert_eq!(map.len(), 2); - #[derive(NamedType)] + #[derive(StrongType)] struct Tag(String); let mut map = HashSet::::new(); diff --git a/tests/tests.rs b/tests/tests.rs index 7a19dc8..3af017c 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1 +1 @@ -mod named_type; +mod strong_type;