Custom slice index with SliceIndex trait?

I wrap a usize value into a struct that allows me to handle overflow behaviour (with custom max value). I implement all the Sub/Add traits to make it behave like a primitive type. It's great! But I'm missing two things:

  • indexing a slice from that custom type (myslice[custom_usize] ). Apparently implementing SliceIndex is the way to go, but this appears to only be possible within core:: (because it requires my type to implement a core:: private trait: Sealed ).
  • support ranges. I want to be able to do for x in custom_usize_1..custom_usize_2 . No idea how to do that.

Any idea if any of these things are possible?

You can't implement the sealed SliceIndex trait, but you can implement indexing for slices directly.

You can't make Range<CustomUsize> implement Iterator on stable without Step or such becoming stable. So until then you would need some sort of wrapper or custom iterator.

See also the other kinds of ranges here.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.