Use strongly typed wrapped indices in VecDeque#152112
Use strongly typed wrapped indices in VecDeque#152112Kobzol wants to merge 3 commits intorust-lang:mainfrom
VecDeque#152112Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
38ead54 to
5aed3d9
Compare
| pub(super) struct WrappedIndex(usize); | ||
|
|
||
| impl WrappedIndex { | ||
| /// Safety invariant: the newly constructed index must be in-bounds for the VecDeque |
There was a problem hiding this comment.
I don't think this safety invariant is particularly useful, as all functions that act on the buffer still need to require that the WrappedIndex is specific to that particular VecDeque with a given capacity.
There was a problem hiding this comment.
Yeah, it's a relatively weak invariant. That being said, I still thought it would be a good idea to provide a "SLOW DOWN" sign when someone wants to conjure the index out of the blue, which is why I marked the function unsafe.
| } | ||
|
|
||
| #[inline(always)] | ||
| pub(super) fn abs_diff(self, other: Self) -> Self { |
There was a problem hiding this comment.
The return type should be usize, right? It isn't a buffer index but rather a length.
There was a problem hiding this comment.
Right, the type represents something like "value in-bounds". My thought process was that if we have two in-bounds values, and we do an absolute diff of them, the result also has to be in-bounds (w.r.t. the same VecDeque). But we do treat it as usize everywhere the function is currently used, so.. I'll change it to usize.
|
I did name if |
|
I do slightly prefer the Wrapped name, I think that the buffer part is implied by the word "index". The main idea is to express that the index was wrapped to not go over bounds, which is how it is created through most means. @rustbot ready |
This comment has been minimized.
This comment has been minimized.
72a5f8f to
841763e
Compare
This is far from perfect, but it would have prevented #151769.
r? @joboet