| Crates.io | generic-interval |
| lib.rs | generic-interval |
| version | 1.0.0 |
| created_at | 2025-02-02 13:59:23.647121+00 |
| updated_at | 2025-03-03 15:29:46.269437+00 |
| description | A generic closed interval library. |
| homepage | https://github.com/kenba/interval-rs |
| repository | https://github.com/kenba/interval-rs |
| max_upload_size | |
| id | 1539601 |
| size | 37,455 |
A generic closed interval library.
An interval is a pair of numbers which represents all the numbers between them.
Intervals are considered closed so the bounds are included.
Intervals are written [a, b] to represent all the numbers between a and b inclusive, a ≤ b.
The library is designed to be used with any types that implement the
Copy and PartialOrd traits including the floating point types:
f64 and f32 and arithmetic types in
new types.
The library is declared no_std so it can be used in embedded applications.
use generic_interval::{Interval, hull, intersection};
// An example new-type based on f64
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Metres(pub f64);
let a = Interval::try_from((Metres(1.0), Metres(4.0))).unwrap();
let b = Interval::try_from((Metres(6.0), Metres(9.0))).unwrap();
// Note: the hull does not include 4.0-6.0
let result = hull(a, b);
assert_eq!(Metres(1.0), result.lower());
assert_eq!(Metres(9.0), result.upper());
let result = intersection(a, b);
assert!(result.is_none());
let c = Interval::try_from((Metres(4.0), Metres(9.0))).unwrap();
let result = intersection(a, c).unwrap();
assert_eq!(Metres(4.0), result.lower());
assert_eq!(Metres(4.0), result.upper());
generic-interval is provided under a MIT license, see LICENSE.