-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
add CString::from_vec_until_nul #96186
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
(Second attempt at this; the first attempt was #95160 which was closed by github during force-push shenanigans, and I don't seem to have the powers to re-open it). |
r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs |
This PR seems to have fallen between the cracks, so I will ping a few people who were helpful on the earlier PR (#94984): @m-ou-se @AzureMarker @mbartlett21 Please let me know if there's anything I can do to improve this change or push it forward. I think it makes sense to have an owned (
|
It seems like a good idea to match the borrowed version.
That's fine.
In my opinion, it would be worse having the matching methods for CStr not share an error type but the methods for CString sharing one. |
/// If the nul byte is not at the end of the input `Vec`, then the `Vec` | ||
/// will be truncated so that there is only one nul byte, and that byte | ||
/// is at the end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be good to mention that this doesn't reduce the capacity of the Vec/CString. (Like the Vec::truncate docs.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait, never mind: CString stores a Box<[u8]>, not a Vec, so it does shrink the capacity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That does raise the question of how useful this method is, though, since it most likely ends up reallocating anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the same thing could be accomplished with Cstr::from_bytes_with_nul()
following by .into_c_string()
. But being able to do the same thing from a CString
context would be a kindness. Particularly if the user doesn't think to check the docs for both places when looking for this functionality.
23fd684
to
6b486a5
Compare
I had to tweak the unit test, as some time in the last month support for |
This adds a member fn that converts a Vec into a CString; it is intended to more useful than from_vec_with_nul (which requires that the caller already know where the nul byte is). This is a companion to CStr::from_bytes_until_nul, and shares the feature gate: cstr_from_bytes_until_nul
6b486a5
to
2de237a
Compare
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
I rebased onto a recent master, just to check that this still applies. No changes were made since the last version. |
It's probably my fault this went stale, but perhaps reviewers would be willing to have another look? |
I am not fully convinced we should have this method, as its use cases are limited and it will still have to reallocate in all cases that are not already covered by from_vec_with_nul. When would you use this function? Are those use cases already already covered by CString::from_vec_with_nul() or CStr::from_vec_until_nul().to_owned()? |
To me it's mostly about consistency: But this PR has been open a long time, so I'm looking forward to seeing it resolved, one way or the other. |
I kind of agree, since the implementation of |
@ericseppanen if you've addressed the concerns, comment I would put the justification from your previous comment into the issue description. Since this would need FCP, it probably wouldn't be bad to further update it to match the ACP template https://github.com/rust-lang/libs-team/issues/new?assignees=&labels=api-change-proposal%2C+T-libs-api&template=api-change-proposal.md&title=%28My+API+Change+Proposal%29 Also, make sure you update the mentioned gate |
As far as I can tell, this has already been reviewed and isn't going anywhere. I don't see any point in asking for more reviews when nothing has changed since the last one. |
This adds a fn that converts a
Vec
into aCString
, without requiring the nul byte to be at the end of the Vec. It is intended for the case when an FFI function call wrote a string of unknown length, whichfrom_vec_with_nul
doesn't handle easily.For example,
This is a companion to
CStr::from_bytes_until_nul
(#94984) , and shares the feature gate:cstr_from_bytes_until_nul
(tracking issue #95027).Unresolved questions:
I couldn't decide whether this fn should use its own unique error type, or whether it should share the error type with
from_vec_with_nul
. In this version I reused the existing error type, but I'm happy to change if that's the better choice.