### 10 unstable releases (3 breaking)

0.3.0 | Oct 28, 2023 |
---|---|

0.2.3 | Oct 17, 2023 |

0.1.5 | Jun 12, 2022 |

0.1.3 | May 26, 2022 |

0.0.1 | Dec 21, 2021 |

#**6** in #closed

**1,308** downloads per month

Used in **32** crates
(2 directly)

**MIT**license

29KB

517 lines

# Module :: winterval

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

or closed one `core ::`

`ops`Range

`::``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` `winterval``::`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` `winterval``::``{` 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

has no bounds and represents the range from minus infinity to plus infinity.`core ::`

`ops`RangeFull

`::``
``use` `winterval``::``{` 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 winterval_trivial