Skip to content

Commit

Permalink
Add float INFINITY/NEG_INFINITY (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Feb 11, 2024
1 parent 7508a89 commit 116d5ee
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
23 changes: 19 additions & 4 deletions strong-type-derive/src/detail/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@ use quote::quote;
pub(crate) fn implement_constants(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream {
quote! {
impl #name {
pub const MIN: Self = Self(#value_type::MIN);
pub const MAX: Self = Self(#value_type::MAX);
pub const ZERO: Self = Self(0 as #value_type);
pub const ONE: Self = Self(1 as #value_type);
}
}
}

pub(crate) fn implement_constants_derived(
name: &syn::Ident,
value_type: &syn::Ident,
) -> TokenStream {
quote! {
impl #name {
pub const MIN: Self = Self(#value_type::MIN);
pub const MAX: Self = Self(#value_type::MAX);
pub const ZERO: Self = Self(#value_type::ZERO);
pub const ONE: Self = Self(#value_type::ONE);
}
}
}

pub(crate) fn implement_infinity(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream {
quote! {
impl #name {
pub const INFINITY: Self = Self(#value_type::INFINITY);
pub const NEG_INFINITY: Self = Self(#value_type::NEG_INFINITY);
}
}
}

pub(crate) fn implement_limit(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream {
quote! {
impl #name {
pub const MIN: Self = Self(#value_type::MIN);
pub const MAX: Self = Self(#value_type::MAX);
}
}
}
4 changes: 3 additions & 1 deletion strong-type-derive/src/detail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub(crate) use basic_string::{
};
pub(crate) use bit_ops::implement_bit_shift;
pub(crate) use bool_ops::implement_bool_ops;
pub(crate) use constants::{implement_constants, implement_constants_derived};
pub(crate) use constants::{
implement_constants, implement_constants_derived, implement_infinity, implement_limit,
};
pub(crate) use display::implement_display;
pub(crate) use hash::implement_hash;
pub(crate) use nan::implement_nan;
Expand Down
19 changes: 12 additions & 7 deletions strong-type-derive/src/strong_type.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::detail::{
get_attributes, get_type, implement_arithmetic, implement_basic, implement_basic_primitive,
implement_basic_string, implement_bit_shift, implement_bool_ops, implement_constants,
implement_constants_derived, implement_display, implement_hash, implement_nan,
implement_negate, implement_primitive_accessor, implement_primitive_accessor_derived,
implement_primitive_str_accessor, implement_primitive_str_accessor_derived, is_struct_valid,
StrongTypeAttributes, TypeInfo, UnderlyingType, ValueTypeGroup,
implement_constants_derived, implement_display, implement_hash, implement_infinity,
implement_limit, implement_nan, implement_negate, implement_primitive_accessor,
implement_primitive_accessor_derived, implement_primitive_str_accessor,
implement_primitive_str_accessor_derived, is_struct_valid, StrongTypeAttributes, TypeInfo,
UnderlyingType, ValueTypeGroup,
};
use proc_macro2::TokenStream;
use quote::quote;
Expand Down Expand Up @@ -58,7 +59,7 @@ pub(super) fn expand_strong_type(input: DeriveInput) -> TokenStream {
ValueTypeGroup::Int(underlying_type) | ValueTypeGroup::UInt(underlying_type) => {
ast.extend(implement_basic_primitive(name, &value_type));
ast.extend(implement_hash(name));

ast.extend(implement_limit(name, &value_type));
match underlying_type {
UnderlyingType::Primitive => ast.extend(implement_constants(name, &value_type)),
UnderlyingType::Derived => {
Expand All @@ -69,10 +70,14 @@ pub(super) fn expand_strong_type(input: DeriveInput) -> TokenStream {
ValueTypeGroup::Float(underlying_type) => {
ast.extend(implement_basic_primitive(name, &value_type));
ast.extend(implement_nan(name, &value_type));
ast.extend(implement_limit(name, &value_type));
ast.extend(implement_infinity(name, &value_type));
match underlying_type {
UnderlyingType::Primitive => ast.extend(implement_constants(name, &value_type)),
UnderlyingType::Primitive => {
ast.extend(implement_constants(name, &value_type));
}
UnderlyingType::Derived => {
ast.extend(implement_constants_derived(name, &value_type))
ast.extend(implement_constants_derived(name, &value_type));
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions strong-type-tests/tests/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,25 @@ mod tests {
assert_eq!(Second::MAX.value(), i32::MAX);
assert_eq!(Second::MIN.value(), i32::MIN);
assert_eq!(Second::ZERO.value(), 0i32);
assert_eq!(Second::ONE.value(), 1i32);

#[derive(StrongType)]
struct State(u8);

assert_eq!(State::MAX.value(), u8::MAX);
assert_eq!(State::MIN.value(), u8::MIN);
assert_eq!(State::ZERO.value(), 0u8);
assert_eq!(State::ONE.value(), 1u8);

#[derive(StrongType)]
struct Meter(f32);

assert_eq!(Meter::MAX.value(), f32::MAX);
assert_eq!(Meter::MIN.value(), f32::MIN);
assert_eq!(Meter::INFINITY.value(), f32::INFINITY);
assert_eq!(Meter::NEG_INFINITY.value(), f32::NEG_INFINITY);
assert_eq!(Meter::ZERO.value(), 0f32);
assert_eq!(Meter::ONE.value(), 1f32);
}

#[test]
Expand Down

0 comments on commit 116d5ee

Please sign in to comment.