Open wr7 opened 3 weeks ago
Bikeshed: element_offset
or item_offset
seems better to me than abbreviating to elem
, there aren't many truncated words in the slice method names. And e.g. std::simd uses rotate_elements_{left,right}
and {Mask,Simd}Element
(that is unstable API, but has been around for a while)
Bikeshed:
element_offset
oritem_offset
seems better to me than abbreviating toelem
, there aren't many truncated words in the slice method names. And e.g. std::simd usesrotate_elements_{left,right}
and{Mask,Simd}Element
(that is unstable API, but has been around for a while)
+1
I'm somewhat impartial to the naming, but I think naming it element_offset
might make sense. It's much more descriptive than "item", and the current documentation for slices appears to use the term "element" much more than "item".
I originally thought that "element" seems kinda long, but it's actually shorter than "subslice", so I think it's fine.
The elem_offset
naming for the method was from a t-libs-api meeting, so I think I'll wait to see if I get more feedback from the rust community on the name before renaming it.
Should they be const?
Should they be const?
Good question
I think that it would be great for them to be const, but a const implementation does not seem to be trivial in current-day rust.
I can see two main ways of implementing something similar to the proposed methods:
usize
s and then use wrapping_sub
and wrapping_add
on them. The issue with this is that you cannot cast pointers into usize
s in const contexts. I don't see this changing too soon in the future either.pointer::byte_offset_from
. This is const, and this would work most of the time, but in cases where this method should return None
, pointer::byte_offset_from
would invoke undefined behavior, and there is no obvious way to get around this.Fortunately, the most common use cases aren't currently const either. Methods like str::split
return iterators which currently cannot be used in const contexts.
Making these methods const is a non-breaking change, so I personally think that we should move forwards with non-const implementations and file a separate issue in the future if constness is desired enough.
Feature gate:
#![feature(substr_range)]
This is a tracking issue for
str::substr_range
,slice::subslice_range
, andslice::elem_offset
as described in this ACP.These methods can be used to extend
str::lines
,str::split
,slice::split
, and other related methods.Public API
Steps / History
Unresolved Questions
elem_offset
instead be namedelement_offset
oritem_offset
?