Improving self-referential structs

I don't know, maybe. I'd like to see what that would look like. Say the parser today returns a &str. With the bytes crate, it would instead return a Buf that is the same subslice that &str references. But probably what you want it to return is AsRef<str> (or more generally, AsRef<[u8]>). So to that end, the Bytes type itself would not be surfaced in the return types. But I've not thought this through fully. It seems like some abstraction over owned vs borrowed underlying types could be made.

The performance concerns still stand, of course. But I take @withoutboats's point that it's moot to compare this performance vs support in the language, particularly since we don't even have a design for the language support (let alone the implementation and its performance implications).

But I'm with @jpernst - I'd like folks to acknowledge that this is a real problem (fortunately not too widespread overall but unfortunately a difficult one), and to continue brainstorming of how it might be solved at the language level. I'm hopeful that we don't have to resort to a library solution for what's otherwise a language shortcoming.

6 Likes