Skip to content

Commit

Permalink
Add is_nan, new, From (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 3, 2023
1 parent 00e5c4f commit 4f6059b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 45 deletions.
8 changes: 7 additions & 1 deletion src/detail/basic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
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 #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
34 changes: 18 additions & 16 deletions src/detail/basic_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@ 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)
}
pub fn value(&self) -> #value_type {
self.0
}
}

pub fn value(&self) -> #value_type {
self.0
}
impl From<#value_type> for #name {
fn from(value: #value_type) -> Self {
Self(value)
}
}

impl Copy for #name {}
impl Copy for #name {}

impl Clone for #name {
fn clone(&self) -> Self {
*self
}
impl Clone for #name {
fn clone(&self) -> Self {
*self
}
}

#[allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
impl std::cmp::PartialOrd for #name {
fn partial_cmp(&self, rhs: &Self) -> Option<std::cmp::Ordering> {
self.value().partial_cmp(&rhs.value())
}
#[allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
impl std::cmp::PartialOrd for #name {
fn partial_cmp(&self, rhs: &Self) -> Option<std::cmp::Ordering> {
self.value().partial_cmp(&rhs.value())
}
}
}
}
14 changes: 11 additions & 3 deletions src/detail/basic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ use quote::quote;
pub(crate) fn implement_basic_string(name: &syn::Ident) -> TokenStream {
quote! {
impl #name {
pub fn new(value : &str) -> Self {
pub fn value(&self) -> &str {
self.0.as_str()
}
}

impl From<&str> for #name {
fn from(value: &str) -> Self {
Self(String::from(value))
}
}

pub fn value(&self) -> &str {
self.0.as_str()
impl From<String> for #name {
fn from(value: String) -> Self {
Self(value)
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/detail/nan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ pub(crate) fn implement_nan(name: &syn::Ident, value_type: &syn::Ident) -> Token
fn nan() -> Self {
Self(#value_type::NAN)
}

fn is_nan(&self) -> bool {
self.0.is_nan()
}
}
}
}
2 changes: 1 addition & 1 deletion src/named_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(super) fn expand_named_type(input: DeriveInput, impl_arithmetic: bool) -> To
let group = get_type(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
64 changes: 40 additions & 24 deletions tests/named_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,44 +124,40 @@ mod tests {
assert_ne!(y.type_id(), z.type_id());
}

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

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

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

let mut map = HashSet::<Tag>::new();
map.insert(Tag("dev".to_string()));
map.insert(Tag("prod".to_string()));
assert_eq!(map.len(), 2);
}

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

let y = Meter::nan();
assert!(y.is_nan());
assert!(y.value().is_nan());

let z = Meter(0.0);
assert!(!z.is_nan());
assert!(!z.value().is_nan());
}

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

assert!(is_true.value());
assert!(!(!is_true).value());
assert!((!!is_true).value());
}

assert!(sign.value());
assert!(!(!sign).value());
assert!((!!sign).value());
#[test]
fn test_string_ctor() {
#[derive(NamedType)]
struct Meter(String);

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

#[test]
Expand All @@ -186,4 +182,24 @@ mod tests {
format!("Mile {{ value: {} }}", std::f64::consts::E)
);
}

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

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

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

let mut map = HashSet::<Tag>::new();
map.insert(Tag::new("dev"));
map.insert(Tag::new("prod"));
assert_eq!(map.len(), 2);
}
}

0 comments on commit 4f6059b

Please sign in to comment.