https://github.com/akvize/range-cmp
Trait that allows comparing a value to a range of values.
https://github.com/akvize/range-cmp
rust
Last synced: about 1 year ago
JSON representation
Trait that allows comparing a value to a range of values.
- Host: GitHub
- URL: https://github.com/akvize/range-cmp
- Owner: Akvize
- License: other
- Created: 2023-10-24T12:03:30.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-11-08T12:56:24.000Z (over 2 years ago)
- Last Synced: 2025-03-26T21:45:55.390Z (over 1 year ago)
- Topics: rust
- Language: Rust
- Homepage:
- Size: 30.3 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# range_cmp
[![Crates.io][crates-badge]][crates-url]
[![MIT licensed][mit-badge]][mit-url]
[![Apache licensed][apache-badge]][apache-url]
[![Build Status][actions-badge]][actions-url]
[crates-badge]: https://img.shields.io/crates/v/range-cmp.svg
[crates-url]: https://crates.io/crates/range-cmp
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
[mit-url]: https://github.com/Akvize/range-cmp/blob/master/LICENSE-MIT
[apache-badge]: https://img.shields.io/badge/license-APACHE-blue.svg
[apache-url]: https://github.com/Akvize/range-cmp/blob/master/LICENSE-APACHE
[actions-badge]: https://github.com/Akvize/range-cmp/actions/workflows/ci.yml/badge.svg
[actions-url]: https://github.com/Akvize/range-cmp/actions/workflows/ci.yml
[Docs](https://docs.rs/range-cmp/latest/range_cmp/)
This Rust crate provides the `RangeComparable` trait on all types that
implement `Ord`. This traits exposes a `rcmp` associated method
that allows comparing a value with a range of values:
```rust
use range_cmp::{RangeComparable, RangeOrdering};
assert_eq!(15.rcmp(20..30), RangeOrdering::Below);
assert_eq!(25.rcmp(20..30), RangeOrdering::Inside);
assert_eq!(35.rcmp(20..30), RangeOrdering::Above);
```
## Empty ranges handling
This crate _does not_ strictly handle empty ranges,
which are not mathematically comparable. In this case,
range_cmp will show different behavior depending on
the representation of the empty range. For instance:
```rust
assert_eq!(30.range_cmp(45..35), RangeOrdering::Below);
assert_eq!(30.range_cmp(25..15), RangeOrdering::Above);
assert_eq!(0.range_cmp(0..0), RangeOrdering::Above);
```