Skip to content

Commit

Permalink
Make ::new the standard constructor (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 3, 2023
1 parent b0e24e1 commit 04a6e90
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "strong-type"
description = "Procedural macros for naming and strong-typing pritimives and strings"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/yunjhongwu/strong-type"
Expand Down
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use strong_type::StrongType;
#[derive(StrongType)]
struct Timestamp(i64);

let timestamp = Timestamp(1701620628123456789);
let timestamp = Timestamp::new(1701620628123456789);
println!("{}", timestamp); // Timestamp(1701620628123456789)
```

Expand All @@ -33,7 +33,6 @@ use strong_type::StrongType;
#[derive(StrongType)]
struct Tag(String);

let _ = Tag("dev".to_string());
let tag = Tag::new("dev");
let tag: Tag = "dev".into();
```
Expand All @@ -50,9 +49,9 @@ struct Second(i32);
#[derive(StrongType)]
struct Minute(i32);

let x = Second(2);
let y = Second(3);
let z = Minute(3);
let x = Second::new(2);
let y = Second::new(3);
let z = Minute::new(3);

assert_eq!(x.type_id(), y.type_id()); // Same type: Second
assert_ne!(y.type_id(), z.type_id()); // Different type: Second versus Minute
Expand All @@ -66,8 +65,8 @@ use strong_type::StrongNumericType;
#[derive(StrongNumericType)]
struct Second(i32);

let x = Second(2);
let y = Second(3);
let x = Second::new(2);
let y = Second::new(3);

assert_eq!(x.value(), 2);
assert_eq!(y.value(), 3);
Expand All @@ -91,6 +90,6 @@ impl Display for Second {
}
}

println!("{}", Second(std::f64::consts::E)); // "Second(2.72)"
println!("{:?}", Second(std::f64::consts::E)); // "Second { value: 2.718281828459045 }"
println!("{}", Second::new(std::f64::consts::E)); // "Second(2.72)"
println!("{:?}", Second::new(std::f64::consts::E)); // "Second { value: 2.718281828459045 }"
```
8 changes: 1 addition & 7 deletions src/detail/basic.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use proc_macro2::TokenStream;
use quote::quote;

pub(crate) fn implement_basic(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream {
pub(crate) fn implement_basic(name: &syn::Ident) -> TokenStream {
quote! {
impl #name {
pub fn new(value: impl Into<#value_type>) -> Self {
Self(value.into())
}
}

impl std::fmt::Debug for #name {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct(stringify!(#name))
Expand Down
4 changes: 4 additions & 0 deletions src/detail/basic_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use quote::quote;
pub(crate) fn implement_basic_primitive(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream {
quote! {
impl #name {
pub fn new(value: #value_type) -> Self {
Self(value.into())
}

pub fn value(&self) -> #value_type {
self.0
}
Expand Down
4 changes: 4 additions & 0 deletions src/detail/basic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use quote::quote;
pub(crate) fn implement_basic_string(name: &syn::Ident) -> TokenStream {
quote! {
impl #name {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}

pub fn value(&self) -> &str {
self.0.as_str()
}
Expand Down
2 changes: 1 addition & 1 deletion src/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(super) fn expand_strong_type(input: DeriveInput, impl_arithmetic: bool) -> T
let group = get_type(value_type);

let mut ast = quote!();
ast.extend(implement_basic(name, value_type));
ast.extend(implement_basic(name));
if !custom_display(&input) {
ast.extend(implement_display(name));
};
Expand Down
95 changes: 47 additions & 48 deletions tests/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,99 +9,99 @@ mod tests {
fn test_basic() {
#[derive(StrongNumericType)]
struct NamedI8(i8);
let _ = NamedI8(1);
let _ = NamedI8::new(1);

#[derive(StrongNumericType)]
struct NamedI16(i16);
let _ = NamedI16(1);
let _ = NamedI16::new(1);

#[derive(StrongNumericType)]
struct NamedI32(i32);
let _ = NamedI32(1);
let _ = NamedI32::new(1);

#[derive(StrongNumericType)]
struct NamedI64(i64);
let _ = NamedI64(1);
let _ = NamedI64::new(1);

#[derive(StrongNumericType)]
struct NamedI128(i128);
let _ = NamedI128(1);
let _ = NamedI128::new(1);

#[derive(StrongNumericType)]
struct NamedISize(isize);
let _ = NamedISize(1);
let _ = NamedISize::new(1);

#[derive(StrongNumericType)]
struct NamedU8(u8);
let _ = NamedU8(1);
let _ = NamedU8::new(1);

#[derive(StrongNumericType)]
struct NamedU16(u16);
let _ = NamedU16(1);
let _ = NamedU16::new(1);

#[derive(StrongNumericType)]
struct NamedU32(u32);
let _ = NamedU32(1);
let _ = NamedU32::new(1);

#[derive(StrongNumericType)]
struct NamedU64(u64);
let _ = NamedU64(1);
let _ = NamedU64::new(1);

#[derive(StrongNumericType)]
struct NamedU128(u128);
let _ = NamedU128(1);
let _ = NamedU128::new(1);

#[derive(StrongNumericType)]
struct NamedUSize(usize);
let _ = NamedUSize(1);
let _ = NamedUSize::new(1);

#[derive(StrongNumericType)]
struct NamedF32(f32);
let _ = NamedF32(1.0);
let _ = NamedF32::new(1.0);

#[derive(StrongNumericType)]
struct NamedF64(f64);
let _ = NamedF64(1.0);
let _ = NamedF64::new(1.0);

#[derive(StrongType)]
struct NamedBool(bool);
let _ = NamedBool(true);
let _ = NamedBool::new(true);

#[derive(StrongType)]
struct NamedChar(char);
let _ = NamedChar('a');
let _ = NamedChar::new('a');

#[derive(StrongType)]
struct NamedString(String);
let _ = NamedString("string".to_string());
let _ = NamedString::new("string");
}

#[test]
fn test_int_arithmetic() {
#[derive(StrongNumericType)]
struct Second(i32);

let x = Second(2);
let mut y = Second(3);
assert_eq!(y + x, Second(5));
assert_eq!(y - x, Second(1));
assert_eq!(y * x, Second(6));
assert_eq!(y / x, Second(1));
let x = Second::new(2);
let mut y = Second::new(3);
assert_eq!(y + x, Second::new(5));
assert_eq!(y - x, Second::new(1));
assert_eq!(y * x, Second::new(6));
assert_eq!(y / x, Second::new(1));
assert!(x < y);
assert!(y >= x);

y += x;
assert_eq!(y, Second(5));
assert_eq!(y, Second::new(5));
y -= x;
assert_eq!(y, Second(3));
assert_eq!(y, Second::new(3));
y *= x;
assert_eq!(y, Second(6));
assert_eq!(y, Second::new(6));
y /= x;
assert_eq!(y, Second(3));
assert_eq!(y, Second::new(3));

let z = Second(2);
let z = Second::new(2);

assert_eq!(-z, Second(-2));
assert_eq!(-z, Second::new(-2));

assert_eq!(Second::max().value(), i32::MAX);
assert_eq!(Second::min().value(), i32::MIN);
Expand All @@ -112,27 +112,27 @@ mod tests {
#[derive(StrongNumericType)]
struct Second(f64);

let x = Second(2.5);
let mut y = Second(3.5);
assert_eq!(y + x, Second(6.0));
assert_eq!(y - x, Second(1.0));
assert_eq!(y * x, Second(8.75));
assert_eq!(y / x, Second(1.4));
let x = Second::new(2.5);
let mut y = Second::new(3.5);
assert_eq!(y + x, Second::new(6.0));
assert_eq!(y - x, Second::new(1.0));
assert_eq!(y * x, Second::new(8.75));
assert_eq!(y / x, Second::new(1.4));
assert!(x < y);
assert!(y >= x);

y += x;
assert_eq!(y, Second(6.0));
assert_eq!(y, Second::new(6.0));
y -= x;
assert_eq!(y, Second(3.5));
assert_eq!(y, Second::new(3.5));
y *= x;
assert_eq!(y, Second(8.75));
assert_eq!(y, Second::new(8.75));
y /= x;
assert_eq!(y, Second(3.5));
assert_eq!(y, Second::new(3.5));

let z = Second(2.0);
let z = Second::new(2.0);

assert_eq!(-z, Second(-2.0));
assert_eq!(-z, Second::new(-2.0));

assert_eq!(Second::max().value(), f64::MAX);
assert_eq!(Second::min().value(), f64::MIN);
Expand All @@ -146,9 +146,9 @@ mod tests {
#[derive(StrongType)]
struct Minute(i32);

let x = Second(2);
let y = Second(3);
let z = Minute(3);
let x = Second::new(2);
let y = Second::new(3);
let z = Minute::new(3);

assert_eq!(x.type_id(), y.type_id());
assert_ne!(y.type_id(), z.type_id());
Expand Down Expand Up @@ -186,7 +186,6 @@ mod tests {

let _: Meter = "String".to_string().into();
let _: Meter = "String".into();
let _ = Meter("String".to_string());
let _ = Meter::new("&str");
}

Expand Down Expand Up @@ -219,9 +218,9 @@ mod tests {
struct Sign(bool);

let mut map = HashSet::<Sign>::new();
map.insert(Sign(true));
map.insert(Sign(false));
map.insert(Sign(false));
map.insert(Sign::new(true));
map.insert(Sign::new(false));
map.insert(Sign::new(false));
assert_eq!(map.len(), 2);

#[derive(StrongType)]
Expand Down

0 comments on commit 04a6e90

Please sign in to comment.