#interval #range #adapter #closed #fundamental #general-purpose #ops

no-std interval_adapter

Interval adapter for both open/closed implementations of intervals ( ranges )

24 breaking releases

0.27.0 Oct 30, 2024
0.25.0 Oct 30, 2024
0.23.0 Jul 13, 2024
0.18.0 Mar 22, 2024
0.3.0 Oct 28, 2023

#239 in Algorithms

Download history 831/week @ 2024-07-29 275/week @ 2024-08-05 1295/week @ 2024-08-12 924/week @ 2024-08-19 2345/week @ 2024-08-26 1637/week @ 2024-09-02 1746/week @ 2024-09-09 427/week @ 2024-09-16 405/week @ 2024-09-23 412/week @ 2024-09-30 291/week @ 2024-10-07 399/week @ 2024-10-14 232/week @ 2024-10-21 878/week @ 2024-10-28 455/week @ 2024-11-04 302/week @ 2024-11-11

1,924 downloads per month
Used in 71 crates (3 directly)

MIT license

22KB
508 lines

Module :: interval_adapter

experimental rust-status docs.rs Open in Gitpod discord

Integer interval adapter for both Range and RangeInclusive.

Let's assume you have a function which should accept Interval. But you don't want to limit caller of the function to either half-open interval core::ops::Range or closed one core::ops::RangeInclusive you want allow to use anyone of iterable interval. To make that work smoothly use IterableInterval. Both core::ops::Range and core::ops::RangeInclusive implement the trait, also it's possible to work with non-iterable intervals, like ( -Infinity .. +Infinity ).

Basic use-case


use interval_adapter::IterableInterval;

fn f1( interval : impl IterableInterval )
{
  for i in interval
  {
    println!( "{i}" );
  }
}

// Calling the function either with
// half-open interval `core::ops::Range`.
f1( 0..=3 );
// Or closed one `core::ops::RangeInclusive`.
f1( 0..4 );

More flexibility

If you need more flexibility in defining intervals, you can convert a tuple of endpoints to an interval.


use interval_adapter::{ IterableInterval, IntoInterval, Bound };

fn f1( interval : impl IterableInterval )
{
  for i in interval
  {
    println!( "{i}" );
  }
}

// Calling the function either with
// half-open interval `core::ops::Range`.
f1( 0..=3 );
// Or closed one `core::ops::RangeInclusive`.
f1( 0..4 );
// Alternatively you construct your custom interval from a tuple.
f1( ( 0, 3 ).into_interval() );
f1( ( Bound::Included( 0 ), Bound::Included( 3 ) ).into_interval() );
// All the calls to the function `f1`` perform the same task,
// and the output is exactly identical.

Non-iterable intervals

You may also use the crate to specify non-iterable intervals. Non-iterable intervals have either one or several unbound endpoints. For example, interval core::ops::RangeFull has no bounds and represents the range from minus infinity to plus infinity.


use interval_adapter::{ NonIterableInterval, IntoInterval, Bound };

fn f1( interval : impl NonIterableInterval )
{
  println!( "Do something with this {:?} .. {:?} interval", interval.left(), interval.right() );
}

// Iterable/bound interval from tuple.
f1( ( Bound::Included( 0 ), Bound::Included( 3 ) ).into_interval() );
// Non-iterable/unbound interval from tuple.
f1( ( Bound::Included( 0 ), Bound::Unbounded ).into_interval() );
// Non-iterable/unbound interval from `core::ops::RangeFrom`.
f1( 0.. );
// Non-iterable/unbound interval from `core::ops::RangeFull`
// what is ( -Infinity .. +Infinity ).
f1( .. );

To add to your project

cargo add interval_adaptor

Try out from the repository

git clone https://github.com/Wandalen/wTools
cd wTools
cargo run --example interval_adapter_trivial

No runtime deps

Features