Skip to content

Commit

Permalink
Fixed a field visibility bug when deriving.
Browse files Browse the repository at this point in the history
As reported in #24. Also added a test when deriving a tuple type.
  • Loading branch information
jtempest committed Oct 11, 2022
1 parent 6f4fdca commit 615975c
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Fixed a bug where field visibility was not respected when deriving the Ulps
type (https://github.com/jtempest/float_eq-rs/issues/24).

## [1.0.0] - 2022-06-02
Added some resource links to the documentation and promoted the crate to
Expand Down
6 changes: 3 additions & 3 deletions float_eq/crates-io.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ The extension traits may be derived for non-generic structs and tuple structs:
all_tol = "f64"
)]
#[derive(Debug, PartialEq, Clone, Copy)]
struct Point {
x: f64,
y: f64,
pub struct Point {
pub x: f64,
pub y: f64,
}

let a = Point { x: 1.0, y: -2.0 };
Expand Down
1 change: 1 addition & 0 deletions float_eq/tests/derive_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn tests() {

// #[derive_float_eq(...)]
t.pass("tests/derive_tests/derive_float_eq/derive_float_eq.rs");
t.pass("tests/derive_tests/derive_float_eq/derive_float_eq_tuple_struct.rs");
t.pass("tests/derive_tests/derive_float_eq/derive_float_eq_all.rs");
t.pass("tests/derive_tests/derive_float_eq/derive_float_eq_all_custom_debug.rs");
t.compile_fail("tests/derive_tests/derive_float_eq/derive_float_eq_missing_ulps_tol.rs");
Expand Down
45 changes: 25 additions & 20 deletions float_eq/tests/derive_tests/derive_float_eq/derive_float_eq.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
use float_eq::{assert_float_eq, assert_float_ne, derive_float_eq, UlpsTol};

#[derive_float_eq(
ulps_tol = "MyComplex32Ulps",
ulps_tol_derive = "Clone, Copy, Debug, PartialEq",
debug_ulps_diff = "MyComplex32UlpsDiff",
debug_ulps_diff_derive = "Clone, Copy, Debug, PartialEq"
)]
#[derive(Debug, Clone, Copy, PartialEq)]
struct MyComplex32 {
re: f32,
im: f32,
}
mod my_module {
use float_eq::{derive_float_eq, UlpsTol};

#[derive_float_eq(
ulps_tol = "MyComplex32Ulps",
ulps_tol_derive = "Clone, Copy, Debug, PartialEq",
debug_ulps_diff = "MyComplex32UlpsDiff",
debug_ulps_diff_derive = "Clone, Copy, Debug, PartialEq"
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MyComplex32 {
pub re: f32,
pub im: f32,
}

impl MyComplex32 {
fn new(re: f32, im: f32) -> MyComplex32 {
MyComplex32 { re, im }
impl MyComplex32 {
pub fn new(re: f32, im: f32) -> MyComplex32 {
MyComplex32 { re, im }
}
}
}

impl MyComplex32Ulps {
fn new(re: UlpsTol<f32>, im: UlpsTol<f32>) -> MyComplex32Ulps {
MyComplex32Ulps { re, im }
impl MyComplex32Ulps {
pub fn new(re: UlpsTol<f32>, im: UlpsTol<f32>) -> MyComplex32Ulps {
MyComplex32Ulps { re, im }
}
}
}

fn main() {
use float_eq::{assert_float_eq, assert_float_ne};
use my_module::{MyComplex32, MyComplex32Ulps};

let a = MyComplex32 { re: 1.0, im: -2.0 };
assert_float_eq!(a, a, abs <= MyComplex32 { re: 0.0, im: 0.0 });
assert_float_eq!(a, a, rel <= MyComplex32 { re: 0.0, im: 0.0 });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mod my_module {
use float_eq::{derive_float_eq, UlpsTol};

#[derive_float_eq(
ulps_tol = "MyComplex32Ulps",
ulps_tol_derive = "Clone, Copy, Debug, PartialEq",
debug_ulps_diff = "MyComplex32UlpsDiff",
debug_ulps_diff_derive = "Clone, Copy, Debug, PartialEq"
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MyComplex32(pub f32, pub f32);
}

fn main() {
use float_eq::{assert_float_eq, assert_float_ne};
use my_module::{MyComplex32, MyComplex32Ulps};

let a = MyComplex32(1.0, -2.0);
assert_float_eq!(a, a, abs <= MyComplex32(0.0, 0.0));
assert_float_eq!(a, a, rel <= MyComplex32(0.0, 0.0));
assert_float_eq!(a, a, ulps <= MyComplex32Ulps(0, 0));

let b = MyComplex32(1.000_000_1, -2.000_000_5);

assert_float_eq!(a, b, abs <= MyComplex32(0.000_000_15, 0.000_000_55));
assert_float_ne!(a, b, abs <= MyComplex32(0.000_000_05, 0.000_000_55));
assert_float_ne!(a, b, abs <= MyComplex32(0.000_000_15, 0.000_000_45));

assert_float_eq!(a, b, rel <= MyComplex32(0.000_000_15, 0.000_000_25));
assert_float_ne!(a, b, rel <= MyComplex32(0.000_000_05, 0.000_000_25));
assert_float_ne!(a, b, rel <= MyComplex32(0.000_000_15, 0.000_000_15));

assert_float_eq!(a, b, ulps <= MyComplex32Ulps(1, 2));
assert_float_ne!(a, b, ulps <= MyComplex32Ulps(0, 2));
assert_float_ne!(a, b, ulps <= MyComplex32Ulps(1, 1));
}
10 changes: 2 additions & 8 deletions float_eq/tests/unit_tests/num_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,9 @@ fn debug_all_tol() {

assert_eq!(a.debug_abs_all_tol(&b, &0.2), Complex32::new(0.2, 0.2));
assert_eq!(a.debug_rel_all_tol(&b, &0.2), Complex32::new(0.5, 0.85));
assert_eq!(
a.debug_rmax_all_tol(&b, &0.2),
Complex32::new(0.5, 0.85)
);
assert_eq!(a.debug_rmax_all_tol(&b, &0.2), Complex32::new(0.5, 0.85));
assert_eq!(a.debug_rmin_all_tol(&b, &0.2), Complex32::new(0.4, 0.8));
assert_eq!(
a.debug_r1st_all_tol(&b, &0.2),
Complex32::new(0.4, 0.85)
);
assert_eq!(a.debug_r1st_all_tol(&b, &0.2), Complex32::new(0.4, 0.85));
assert_eq!(a.debug_r2nd_all_tol(&b, &0.2), Complex32::new(0.5, 0.8));
assert_eq!(a.debug_ulps_all_tol(&b, &2), ComplexUlps32::new(2, 2));
}
Expand Down
4 changes: 2 additions & 2 deletions float_eq_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn expand_float_eq_ulps_tol(input: DeriveInput) -> Result<TokenStream, syn::Erro
let ulps_fields = fields.expand(|field| {
let name = &field.name;
let ty = &field.ty;
quote! { #name: float_eq::UlpsTol<#ty> }
quote! { #vis #name: float_eq::UlpsTol<#ty> }
});
quote! {
#vis struct #ulps_name {
Expand All @@ -137,7 +137,7 @@ fn expand_float_eq_ulps_tol(input: DeriveInput) -> Result<TokenStream, syn::Erro
read::FieldListType::Tuple => {
let ulps_fields = fields.expand(|field| {
let ty = &field.ty;
quote! { float_eq::UlpsTol<#ty> }
quote! { #vis float_eq::UlpsTol<#ty> }
});
quote! {
#vis struct #ulps_name( #(#ulps_fields,)* );
Expand Down
5 changes: 4 additions & 1 deletion float_eq_derive/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::ToTokens;
use syn::{
spanned::Spanned, Attribute, Data, DeriveInput, Fields, FieldsNamed, FieldsUnnamed, Lit,
LitInt, LitStr, Meta, NestedMeta, Type,
LitInt, LitStr, Meta, NestedMeta, Type, Visibility,
};

pub enum FieldName<'a> {
Expand All @@ -22,6 +22,7 @@ impl ToTokens for FieldName<'_> {
pub struct FieldInfo<'a> {
pub name: FieldName<'a>,
pub ty: &'a Type,
pub vis: &'a Visibility,
}

pub enum FieldListType {
Expand Down Expand Up @@ -78,13 +79,15 @@ fn named_field_info(field: &syn::Field) -> FieldInfo {
FieldInfo {
name: FieldName::Ident(field.ident.as_ref().expect("Expected named field")),
ty: &field.ty,
vis: &field.vis,
}
}

fn unnamed_field_info((n, field): (usize, &syn::Field)) -> FieldInfo {
FieldInfo {
name: FieldName::Num(Lit::Int(LitInt::new(&format!("{}", n), Span::call_site()))),
ty: &field.ty,
vis: &field.vis,
}
}

Expand Down

0 comments on commit 615975c

Please sign in to comment.