Step::steps_between does not distinguish overflow and unimplemented (unstable)

1d4a72b
Opened by scottmcm at 2020-08-04 22:57:59

It returns Option<size>: https://doc.rust-lang.org/std/iter/trait.Step.html#tymethod.steps_between

And the comment says it uses None for overflow: https://doc.rust-lang.org/src/core/iter/range.rs.html#29

But it also uses None for unimplemented https://doc.rust-lang.org/src/core/iter/range.rs.html#155

And as a result, Range needs to return (0, None) when it gets None: https://doc.rust-lang.org/src/core/iter/range.rs.html#235-240

It would be nice to either

  • Require that the method is implemented accurately (easy for fundamental integers; harder if Step is expected to support graph walks, but maybe not harder than PartialOrd)
  • Change the type to something else, maybe Option<Option<usize>> or be more hint-like with (usize, Option<usize>)
  • Something else

cc https://github.com/rust-lang/rust/issues/42168

  1. The links appear to have lost their original position.

    Does the original concern still stand? (Does it stand with #69659?)

    Crystal Durham at 2020-05-15 01:00:27

  2. @CAD97 Yes, I think there's still essentially the same problem -- the Invariants section still has None for both unreachable (steps_between(1, 0)) and overflow (steps_between(0_u128, 1<< 127)).

    So if this is used for size_hint, then getting None back from steps_between means you're stuck returning (0, None), when you'd rather be able to return either (0, Some(0)) or (usize::MAX, None).

    scottmcm at 2020-08-04 22:57:59