Skip to content

Commit

Permalink
Add crate info (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 3, 2023
1 parent 4f6059b commit c1683ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
[package]
name = "named-type"
description = "Named type macro"
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"
readme = "README.md"

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.50"
quote = "1.0.23"
syn = "2.0.10"
proc-macro2 = "1"
quote = "1"
syn = "2"

[[test]]
name = "integration"
Expand Down
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# named-type
```rust
use named_type::NamedType;

#[derive(NamedType)]
struct Timestamp(i64);
let timestamp = Timestamp(1701620628123456789);
```

Macros to create strong typed and named values in Rust.
- `NamedType` creates a strong type with a name.
- `NamedNumeric` creates a strong type with a name and implements traits for arithmetic operations.
- `NamedType` creates a named strong type.
- `NamedNumeric` creates a named strong type and implements traits for arithmetic operations.
- `custom_display` suppresses the default `Display` trait implementation and allows users to implement it manually.

## Supported underlying types:
Expand All @@ -15,15 +23,16 @@ Macros to create strong typed and named values in Rust.
- `String`

## Examples
#### Create a named type:
```rust
use named_type::NamedType;

#[derive(NamedType)]
struct Tag(String);

let tag = Tag("dev".to_string());
println!("{:?}", tag); // Tag { value: "dev" }
println!("{}", tag); // Tag("dev")
let _ = Tag("dev".to_string());
let tag = Tag::new("dev");
let tag: Tag = "dev".into();
```

#### Strong type:
Expand All @@ -42,8 +51,8 @@ let x = Second(2);
let y = Second(3);
let z = Minute(3);

assert_eq!(x.type_id(), y.type_id());
assert_ne!(y.type_id(), z.type_id());
assert_eq!(x.type_id(), y.type_id()); // Same type: Second
assert_ne!(y.type_id(), z.type_id()); // Different type: Second versus Minute
```

#### Named type with arithmetic operations:
Expand All @@ -59,6 +68,7 @@ assert_eq!(x.value(), 2);
let mut y = Second(3);
assert!(x < y);
assert!(y >= x);
asssrt_eq!(x + y, Second(5));
```

#### Named type with `custom_display`:
Expand All @@ -76,7 +86,7 @@ impl Display for Mile {
}
}

println!("{}", Second(std::f64::consts::E)); // "Mile(2.72)"
println!("{}", Second(std::f64::consts::E)); // "Second(2.72)"
println!("{:?}", Second(std::f64::consts::E)); // "Second { value: 2.718281828459045 }"

```

0 comments on commit c1683ec

Please sign in to comment.