-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#[repr(transparent)] for str and String #70473
Comments
From the looks of it, it seems that |
To be clear, the standard library is allowed to depend on implementation details that aren't necessarily stable. That |
This is what we should have at the top of every single file instead of license headers. |
Ofc, I am aware. Poor choice of words on my part. I just thought it's worth pointing out. Though in this particular case I don't know how they would do it any other way. |
Yeah, that's why this issue, like the referenced issue about Box, is more about documenting and settings things in stone rather than changing behaviour. |
Would it be illegal for String to ever add more fields, on top of the current pointer + capacity + length? The docs say this:
But I think it's a bit too vague to answer my question. If it's allowed to add more fields, then the only way you're guaranteed to access the internal Vec of a String is by using Noting that you can also pointer cast from a larger String to a Vec if the layout was guaranteed such that a Vec is the very first field of a String (repr(C), like how gobject ensures child classes can be casted up to parents) |
No, you cannot. The docs don't say how these three components are encoded, at which type, and in which order. This is just like for |
You can. It's unsafe as hell, so it's not guaranteed to work, but the bare minimum requirement for it to compile successfully is met: a But back to my original point, would it be illegal for a String to change its internals, at all? The current wording seems a bit vague. And every bit of logic I'm seeing involving the |
Even unsafe code has rules on what you can or cannot do. If you're just transforming between unrelated types, you're basically begging the compiler to introduce UB. That's not what Your code is not even guaranteed to work on all existing platforms, because w/o
This is what this issue is about. It's currently perfectly legal for String to change internal layout and break any such code that relies on such It would be safe to use |
Typically, when people ask "can you do X?", there is an implied requirement that you can do it without causing UB. If you want to use a different definition of "can", you should say so. In this case, transmuting |
I ran in the same kind of issue today. Is there any cons about this change that are blocking it? |
Similarly to #52976, I'd suggest that
str
andString
should have a guaranteed transparent representation of[u8]
andVec<u8>
correspondingly.They are a newtype wrappers of these types under the hood anyway, and I don't see any reasons why they would need to behave differently in the future, and making them different probably would become a breaking change anyway as it would break things like
String::from_raw_parts
constructed from raw parts of theVec
.While
String::from_raw_parts
already covers most common cases, there are still other cases where it would be desired to have guaranteed representation.One such example is constructing
Rc<String>
from aRc<Vec<u8>>
where a library might want to check UTF-8 validity before construction and thenRc::from_raw
+Rc::into_raw
one type into another without actually copying the data.Currently this is not possible to do safely because it's not guaranteed that the
*const Vec<u8>
returned from the firstinto_raw
is compatible with*const String
accepted by the second.While this particular case could be alleviated by adding a special method, there are lots of other containers where such compatibility between these types might be desirable, and I don't see any reasons why it couldn't be guaranteed at a lower level.
The text was updated successfully, but these errors were encountered: