Description
In theory, String could implement a small-string optimization where in certain cases the string is stored inline and Deref
returns a pointer to the flat in-memory representation.
The only thing that really changes here would be the behavior of into_raw_parts()
, it would be forced to perform a last minute allocation.
And that would potentially break Rust code that was calling this function with an older pointer.
It also of course breaks StableDeref
, though there are already open questions about that.
Concretely, I have the following questions:
- Would it make sense to be explicit about this on the invariants of String, such that people maintaining a copy of the stdlib can make such a change, and/or potentially contribute it upstream (making no judgement on whether SSOs are a good idea for Rust in general, just whether such a change should be allowed by Rust's rules)
- Would it make sense to have a miri check that ensures that pointers obtained via
Deref
are not valid forfrom_raw_parts
, only ones frominto_raw_parts()
?
even if we decide that StableDeref is an important property to maintain here, it might be worth getting unsafe code to not rely on into_raw_parts()
' behavior, so that people tweaking the stdlib can still have some hope of having this work (e.g. "as long as we patch out impl StableDeref for String
, we are fine")
Anyway, curious about thoughts.
cc @gribozavr