Skip to content

Commit

Permalink
Derive Default, Sync, and Send (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 12, 2023
1 parent b88163a commit b3f2cf1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[package]
name = "strong-type"
description = "Procedural macros for naming and strong-typing pritimives and strings"
version = "0.2.0"
version = "0.3.0"
edition = "2021"

license = "MIT OR Apache-2.0"
keywords = ["primitive", "string", "strongly-typed"]
repository = "https://github.com/yunjhongwu/strong-type"
readme = "README.md"

Expand Down
12 changes: 11 additions & 1 deletion src/detail/basic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;
use quote::quote;

pub(crate) fn implement_basic(name: &syn::Ident) -> TokenStream {
pub(crate) fn implement_basic(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream {
quote! {
impl std::fmt::Debug for #name {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
Expand All @@ -11,10 +11,20 @@ pub(crate) fn implement_basic(name: &syn::Ident) -> TokenStream {
}
}

impl std::default::Default for #name {
fn default() -> Self {
Self::new(#value_type::default())
}
}

impl std::cmp::PartialEq for #name {
fn eq(&self, rhs: &Self) -> bool {
self.value() == rhs.value()
}
}

unsafe impl Send for #name {}

unsafe impl Sync for #name {}
}
}
2 changes: 1 addition & 1 deletion src/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(super) fn expand_strong_type(input: DeriveInput, impl_arithmetic: bool) -> T
let group = get_type_group(value_type);

let mut ast = quote!();
ast.extend(implement_basic(name));
ast.extend(implement_basic(name, value_type));
if !custom_display(&input) {
ast.extend(implement_display(name));
};
Expand Down
40 changes: 22 additions & 18 deletions tests/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,83 @@
mod tests {
use std::any::Any;
use std::collections::HashSet;
use std::fmt::Display;
use std::fmt::{Debug, Display};
use std::ops::Neg;
use strong_type::{StrongNumericType, StrongType};

fn test_type<T: Debug + Clone + Send + Sync + Default + PartialEq>(default_value: T) {
assert_eq!(T::default(), default_value);
}

#[test]
fn test_basic() {
#[derive(StrongNumericType)]
struct NamedI8(i8);
let _ = NamedI8::new(1);
test_type::<NamedI8>(NamedI8::new(0));

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

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

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

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

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

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

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

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

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

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

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

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

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

#[derive(StrongNumericType)]
struct NamedBool(bool);
let _ = NamedBool::new(true);
test_type::<NamedBool>(NamedBool::new(false));

#[derive(StrongType)]
struct NamedChar(char);
let _ = NamedChar::new('a');
test_type::<NamedChar>(NamedChar::new('\0'));

#[derive(StrongType)]
struct NamedString(String);
let _ = NamedString::new("string");
test_type::<NamedString>(NamedString::new(""));
}

#[test]
Expand Down

0 comments on commit b3f2cf1

Please sign in to comment.