@owenhilyard suggested that buffer_to_search as defined on the trait could be generic over the origin, but I have not been able to sort out a working version of that, or find a good example in the stdlib.
Is there an explicit way to construct this without rebind or going through the span pointer constructor?
And I personally prefer rebind since it is free. constructing an instance is not. The only thing “unsafe” about rebind is that you need to be sure that you aren’t rebinding things that aren’t true. Casting the origin of a struct’s internal data binding it to the struct’s origin (which is what you are doing) is fine.
So all in all I’d do:
fn buffer_to_search(ref self, out result: Span[UInt8, __origin_of(self)]):
result = rebind[__type_of(result)](Span(self.seq))
The big problem is traits and trying to be generic over origins. The “correct” way to do the function would be to link it to the struct’s variable (not sure if this works though):
but your trait would no longer apply to the struct. This is a problem I’ve had when trying to do things generic over types that return a Span (like String and StringSlice) in the stdlib.
Is this something that is considered a language defect at the moment that may get a fix, or is this origins working as intended?
I think this is a current limitation in the language, and that the solution will come naturally once we have parametrizable traits