Rust intervals
This package provides various functions to deal with intervals (aka ranges) of values.
Features
It provides the following features:
- Generic over the interval type. Support out of the box for u8, u16, u32, u64, i8, i16, i32, i64, char, and optionally for chrono::DateTime
- Extensive testing, with coverage of nearly 100% of the code and basic fuzzing.
- Supports any variation on open-closed, open-open, closed-open, closed-closed, unbounded-open, unbounded-closed, open-unbounded, and close-unbounded intervals. These intervals can be freely combined.
- Usable with types that do not even provide comparison, though with reduce feature set. The more traits your type has, the more functions the interval provides.
- Standard queries like
contains()
: whether the interval contains a specific valuecontains_interval()
: whether the interval fully includes another interval.is_empty()
: does the interval contain any value ?equivalent()
: do two intervals contain the same set of values ?strictly_left_of()
,left_of()
,right_of()
,strictly_right_of()
: relative positions of two intervalsconvex_hull()
: smallest interval that contains two othersdifference()
: all values in one interval but not in anothersymmetric_difference()
: all values in either interval, but not in bothintersection()
: all values in both intervalsbetween()
: all values between two intervalscontiguous()
: whether two intervals are contiguous, with no values between themunion()
: union of two contiguous intervalsDisplay
andDebug
support
- Operator overloads for the queries above
- Support for
serde
- Support for
no_std
- Support for standard traits like
Default
,Clone
,Display
,Debug
,Eq
,PartialEq
,Ord
,PartialOrd
,Hash
,From<String>
,From<Interval> -> String
andFromStr
depending on what your type provides. See examples below on how to convert to and from a string.
Example
use ;
let intv1 = new_closed_open;
let value = 5;
assert!;
let intv2 = interval!;
assert!;
let _ = intv1.convex_hull;
An interval can be converted to a string using the various standard Rust approaches:
use ;
let intv1 = new_closed_open;
let s = format!; // using the Display trait
let s = intv1.to_string; // using the ToString trait (via Display)
let s = String from; // using From<Interval<T>>->String trait
let s: String = intv1.into; // using the Into<String> trait
let s = "[1, 4]";
let intv = from; // using From<String> trait
let intv: = s.into; // using Into<Interval<T>> trait
let intv: = s.parse?; // using FromStr trait (may fail)
Authors
- Emmanuel Briot (Rust version)
- Duncan Sands (Ada version it is based on)