rust-client icon indicating copy to clipboard operation
rust-client copied to clipboard

introduce basic ops on `Range` and `ValuesCount`

Open nkbelov opened this issue 2 years ago • 2 comments

This introduces some typical operations on Range and ValueCount, conforms them to std::ops::RangeBounds and provides macros to construct them with a syntax analogous to normal Rust ranges.

I'm a bit unsure about the macros for a number of reasons:

  1. they require a different syntax to make it possible to exclude the lower bound,
  2. they sometimes don't parse very well, requiring parentheses around some expressions like f64::INFINITY,
  3. rustfmt rewrites a=..=b into a = ..=b,

but, on the other hand, they drastically reduce the verbosity of the other ways to construct these types.

An alternative approach would be to allow construction as From<std::ops::Range> etc. and allow to exclude the lower bound by a separate method:

Range::from(0..=1).exclude_lower()

The various checks for infinites and NaNs on f64 are perhaps somewhat excessive, as these can't be expressed in JSON and thus stored in payloads, but these still can be passed into Range if the latter isn't constructed carefully and produce surprising query results.

nkbelov avatar Aug 01 '23 20:08 nkbelov

Nice work on this. Thank you for the efforts.

Regarding ranges, I don't like that range!(a..=b) now excludes a. It differs with Rusts ranges which is confusing.

Maybe we can come up with a different syntax for excluding. a-..=b or a!..=b could work, though that might look a bit weird. Adding exclude_lower() may end up being the least confusing.

timvisee avatar Aug 02 '23 07:08 timvisee

After some thought I indeed too feel that the macros will be more confusing than helpful.

I have updated the code to support the exclude_lower() approach and construction from stdlib's ranges.

nkbelov avatar Aug 02 '23 15:08 nkbelov