Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float ord #33

Merged
merged 9 commits into from
Apr 22, 2023
Merged

Float ord #33

merged 9 commits into from
Apr 22, 2023

Conversation

greyblake
Copy link
Owner

@greyblake greyblake commented Apr 22, 2023

This PR addresses #28

It allows to derive Eq and Ord on float based types if they have finite validation:

use nutype::nutype;

#[nutype(validate(finite))]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct Size(f64);


// Comparison
// 
let a: Size = Size::new(2.5).unwrap();
let b: Size = Size::new(3.3).unwrap();

assert_eq!(a.cmp(&b), Ordering::Less);
assert_eq!(b.cmp(&a), Ordering::Greater);


// Sorting
//
let initial_raw_sizes = vec![5.5, 2.0, 15.0, 44.5, 3.5];
let mut sizes: Vec<Size> = initial_raw_sizes
    .into_iter()
    .map(|s| Size::new(s).unwrap())
    .collect();
sizes.sort();
let sorted_raw_sizes: Vec<f64> = sizes.into_iter().map(Size::into_inner).collect();
assert_eq!(sorted_raw_sizes, vec![2.0, 3.5, 5.5, 15.0, 44.5],);

@greyblake greyblake merged commit cd8f0b5 into master Apr 22, 2023
@greyblake greyblake deleted the float-ord branch June 30, 2023 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant