I have a little library where I can define integer types. These are intended for type-safe indexing into arrays and strings in the kind of algorithms I often write. For example, I can use it to define an offset type, Offset
and an index type, Idx
such that you can get an Offset
by subtracting two Idx
, you can get Idx
by adding or subtracting Offset
, but you cannot for example multiple or add Idx
.
let (i,j): (Idx,Idx) = ...;
let offset: Offset = j - i; // Subtracting indices gives an offset
let k: Idx = j + offset; // Adding an offset to an index gives an index
// let _ = i + j; -- You can't add indices
I managed (with some difficulty) to implement std::iter::Step
so I can also iterate through a range of indices
for k in i .. j { /* ... */ }
but now I've set my eyes on a higher goal: I also want to use ranges of these types to slice into sequences
let v: Vec<sometype> = vec![...];
let w: &[sometype] = &v[i..j]; // Slice with a range of Idx
This should be a simple matter of implementing std::ops::Index
and std::ops::IndexMut
, except that the type system won't let me implement
impl<I,T> std::ops::Index<std::ops::Range<I>> for Vec<T>
where I: /* my types */
{ ... }
for a wrapper type, or a generic
impl<I,T> std::ops::Index<std::ops::Range<Wrapper<I>>> for Vec<T>
where I: /* my types */
{ ... }
where Wrapper
is the type I actually use and I
a trait that helps me write generic code.
The problem is that both Index
and Range
are defined outside of my crate, so this kind of specialisation is not allowed. Or is it?
Is there any way that I can implement a trait for a generic type outside my crate when its generic parameters are from within my crate?
Or, better still, is there any way to tap into the syntactic sugar of the ..
operator, so I can get a wrapped type? What I really want is to wrap Range
to get the same behaviour and then some. I can do that by wrapping Range
and implementing Deref
, but if I go that route, I lose the syntactic sugar of the ..
operator.
It is not a big problem, but I can imagine some confusion when you could write
for k in i .. j { /* ... */ }
like you can for the built-in types, but you have to use
let w: &[type] = &v[range(i,j)];
for slicing. It gets even more cumbersome if I want to allow slices such as i..
, ..j
to be wrapped. (The ..
slice doesn't matter here, it won't get my types anyway). If I did that, I would need constructors for three types of ranges, or some ugly wrapping using Option
, I think.
The ..
syntactic sugar is really neat, but from what I have explored you just cannot use that much for ranges of your own types. You can define ranges and you can, with some hacking, iterate through them, but you can't index with them.
Tell me I'm wrong, or let me know if there are any tricks that gets the job done, or even half-done. Or, if this is indeed impossible, let me know so I can stop wasting time on it and write a wrapper class and give up on the ..
operator.
Update I have put a simplified example in playgrounds
Vec
.get(&self, range: Range<Yourtype>)