Skip to content

Commit

Permalink
Require Into<UnderlyingType> in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu committed Dec 23, 2023
1 parent 72e157f commit 0c42895
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 50 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ use strong_type::StrongType;
struct Tag(String);

let tag = Tag::new("dev");
let tag: Tag = "dev".into(); // Alternative instantiation
```

#### Demonstrating type distinctiveness:
Expand Down Expand Up @@ -91,18 +90,18 @@ use strong_type::StrongType;

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

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

assert_eq!(x.value(), 2);
assert_eq!(y.value(), 3);
assert_eq!(z.value(), 0);
assert!(x < y);
assert!(y >= x);
assert_eq!(x + y, Second(5));
assert_eq!(x + y, Nanosecond(5));
```

#### Named bool type with logical operations:
Expand Down
6 changes: 6 additions & 0 deletions strong-type-derive/src/detail/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use quote::quote;

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

impl StrongType for #name {
type UnderlyingType = #value_type;
}
Expand Down
4 changes: 0 additions & 4 deletions strong-type-derive/src/detail/basic_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ 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: 0 additions & 4 deletions strong-type-derive/src/detail/basic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ 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
70 changes: 35 additions & 35 deletions strong-type-tests/tests/auto_operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,62 @@ mod tests {
#[auto_operators]
struct Second(i32);

let x = Second::new(2);
let mut y = Second::new(3);
let x = Second(2);
let mut y = Second(3);
let x_ref = &x;
let y_ref = &y;

assert_eq!(y + x, Second::new(5));
assert_eq!(y + x_ref, Second::new(5));
assert_eq!(y_ref + x, Second::new(5));
assert_eq!(y_ref + x_ref, Second::new(5));
assert_eq!(y + x, Second(5));
assert_eq!(y + x_ref, Second(5));
assert_eq!(y_ref + x, Second(5));
assert_eq!(y_ref + x_ref, Second(5));

assert_eq!(y - x, Second::new(1));
assert_eq!(y - x_ref, Second::new(1));
assert_eq!(y_ref - x, Second::new(1));
assert_eq!(y_ref - x_ref, Second::new(1));
assert_eq!(y - x, Second(1));
assert_eq!(y - x_ref, Second(1));
assert_eq!(y_ref - x, Second(1));
assert_eq!(y_ref - x_ref, Second(1));

assert_eq!(y * x, Second::new(6));
assert_eq!(y * x_ref, Second::new(6));
assert_eq!(y_ref * x, Second::new(6));
assert_eq!(y_ref * x_ref, Second::new(6));
assert_eq!(y * x, Second(6));
assert_eq!(y * x_ref, Second(6));
assert_eq!(y_ref * x, Second(6));
assert_eq!(y_ref * x_ref, Second(6));

assert_eq!(y / x, Second::new(1));
assert_eq!(y / x_ref, Second::new(1));
assert_eq!(y_ref / x, Second::new(1));
assert_eq!(y_ref / x_ref, Second::new(1));
assert_eq!(y / x, Second(1));
assert_eq!(y / x_ref, Second(1));
assert_eq!(y_ref / x, Second(1));
assert_eq!(y_ref / x_ref, Second(1));

assert_eq!(y % x, Second::new(1));
assert_eq!(y % x_ref, Second::new(1));
assert_eq!(y_ref % x, Second::new(1));
assert_eq!(y_ref % x_ref, Second::new(1));
assert_eq!(y % x, Second(1));
assert_eq!(y % x_ref, Second(1));
assert_eq!(y_ref % x, Second(1));
assert_eq!(y_ref % x_ref, Second(1));

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

let mut y = Second::new(3);
let mut y = Second(3);
y += x_ref;
assert_eq!(y, Second::new(5));
assert_eq!(y, Second(5));
y -= x_ref;
assert_eq!(y, Second::new(3));
assert_eq!(y, Second(3));
y *= x_ref;
assert_eq!(y, Second::new(6));
assert_eq!(y, Second(6));
y /= x_ref;
assert_eq!(y, Second::new(3));
assert_eq!(y, Second(3));
y %= x_ref;
assert_eq!(y, Second::new(1));
assert_eq!(y, Second(1));

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

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

#[test]
Expand Down
2 changes: 1 addition & 1 deletion strong-type-tests/tests/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod tests {
#[derive(StrongType)]
struct Days(u32);
let _ = Days(1);
let _ = Days::new(1);
let _ = Days::new(1u32);

#[derive(StrongType)]
struct Value(f64);
Expand Down

0 comments on commit 0c42895

Please sign in to comment.