Skip to content

Commit

Permalink
Rename crate (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 3, 2023
1 parent 9a02e30 commit b0e24e1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 59 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# 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);
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());
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion src/named_type.rs → src/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
58 changes: 29 additions & 29 deletions tests/named_type.rs → tests/strong_type.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -109,7 +109,7 @@ mod tests {

#[test]
fn test_float_arithmetic() {
#[derive(NamedNumeric)]
#[derive(StrongNumericType)]
struct Second(f64);

let x = Second(2.5);
Expand Down Expand Up @@ -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);
Expand All @@ -156,7 +156,7 @@ mod tests {

#[test]
fn test_float_nan() {
#[derive(NamedType)]
#[derive(StrongType)]
struct Meter(f64);

let y = Meter::nan();
Expand All @@ -170,7 +170,7 @@ mod tests {

#[test]
fn test_bool_negate() {
#[derive(NamedType)]
#[derive(StrongType)]
struct IsTrue(bool);
let is_true = IsTrue(true);

Expand All @@ -181,7 +181,7 @@ mod tests {

#[test]
fn test_string_ctor() {
#[derive(NamedType)]
#[derive(StrongType)]
struct Meter(String);

let _: Meter = "String".to_string().into();
Expand All @@ -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);

Expand All @@ -215,7 +215,7 @@ mod tests {

#[test]
fn test_hash() {
#[derive(NamedType)]
#[derive(StrongType)]
struct Sign(bool);

let mut map = HashSet::<Sign>::new();
Expand All @@ -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::<Tag>::new();
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod named_type;
mod strong_type;

0 comments on commit b0e24e1

Please sign in to comment.