Proposal: Get Range of sub-slice

Perhaps when const generics are more complete there could be a statically-checked bound, where { size_of::<T>() } > 0.

Or, less burdensome to the compiler, there could be mostly-ordinary traits for a type to promise that its size is not zero (or is zero, for other use cases), compiler-validated in the same way you can't falsely impl Copy for .... Has any such thing been proposed before?

But if we want to have that this will be required to block stabilization, and we already have panicking methods like offset_from() (not saying this is not a good idea, just noting).

Is it that big of a problem? In my opinion the panicking on ZSTs would be a bigger gotcha. Of course then there should be a note in the documentation that only slice->slice but not range->range roundtrips are guaranteed for ZSTs

What will you return then? If you return some constant value it will be just wrong. It is much bigger surprise than a panic with a clear message "function is unsupported on ZSTs".

You still have length information, so you can return None if the two ranges aren't at the same address or the latter slice is larger and Some(0..len) otherwise.

1 Like

But in case of Some, what would you return?

An argument that the operations should work for ZSTs:

If you take the question as “Does this slice include that reference, such that it would be equivalent to use this as an alternate representation of the reference?” then the answer is just always “yes” if the address is equal.

An argument that the operations shouldn't work for ZSTs:

If you want an answer that is about uniqueness (say, the value is some kind of token whose possession, or number of extant instances, is meaningful, even if it has no data itself) then it is not valid to return an answer that (effectively) points at the parent slice without correctly identifying how much of the parent slice the query is. In this regard, it's very much like dividing by zero as an attempt to undo multiplying by zero (indexing a slice of ZSTs): there cannot be an answer because the information wanted was discarded.

CAD97 already wrote it:

For a ZST slice slice[5..10] is the same as slice[0..5] in practice (assuming a total length > 10 for this example). It has the same address, the same length and instances of the same zero-sized type are indistinguishable. So for every subslice slice[i..j] there is a range 0..(j-i) that can be used to reconstruct the subslice.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.