-
Notifications
You must be signed in to change notification settings - Fork 20
impl Display for CStr
#550
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
Comments
The new I'm unsure about the alternate Is there a semantic difference between Does a |
Oh I had not heard of |
I updated the solution sketch to say that this shares the impl with |
I kinda love the idea of a Deref impl here. And yes, I think the Display impls should match, other than omitting the trailing NUL in the case of CStr/CString. |
Personally I think the probability of “thin pointer CStr” change ever happening is already low and getting lower every year but a Deref impl feels like a novel nail in the coffin. |
C's It's likely best to say that our https://internals.rust-lang.org/t/pre-rfc-deprecate-and-replace-cstr-cstring/5016/38 has some more details about C encodings (that entire thread has some good comments). |
Also related, regarding |
Related PR: rust-lang/rust#138498 |
Given the concerns about |
Trait impls on stable types are insta-stable themselves, so that would need to go through FCP. But the next step here is for us to sign off on this ACP. Then PR. Then that has to go through FCP because it's insta stable. A One concern for stabilization is whether this should block on |
@BurntSushi thanks. Are more sign offs required? The feature lifecycle doc describes tracking issues as tracking feature stabilization, but since this would be insta-stable, I presume the tracking issue would be used to hold the FCP? If my understanding is correct, the next steps are to create a PR and a tracking issue, then start the FCP on the tracking issue. Once the FCP is concluded, the PR can be merged. Is that correct? |
Yes one is enough. I think just a tracking issue is enough for now? I don't think a PR would hurt anything though. I can never keep track of whether we do FCP on the tracking issue or the PR. I think in practice we do whatever is convenient or wherever most of the discussion coalesces. |
Delegate to `<ByteStr as Display>::fmt`. Link: rust-lang/libs-team#550 Link: rust-lang#139984.
Delegate to `<ByteStr as Display>::fmt`. Link: rust-lang/libs-team#550 Link: rust-lang#139984.
Delegate to `<ByteStr as Display>::fmt`. Link: rust-lang/libs-team#550 Link: rust-lang#139984.
Delegate to `<ByteStr as Display>::fmt`. Link: rust-lang/libs-team#550 Link: rust-lang#139984.
We discussed this in the @rust-lang/libs-api meeting and couldn't come to a consensus on adding a |
While not stable, |
ByteStr was discussed in the meeting, its purpose is to explicitly opt into treating maybe-utf8 as utf8-or-replacement output, so 🤔... perhaps we could generalize |
Some more libs-api discussion happened: We may want to reassess the "thin pointer Cstr" situation (in a separate topic) and commit to it being a fat pointer, then if the outcome for that is affirmative then we don't need a |
I agree with adding a @Darksonn I know you argued against a Would a |
No. The main reason we still have a custom |
Note that it was suggested earlier in this thread to make |
Proposal
Problem statement
In multi-language projects that need to perform interop with C or C++, you will often need to manipulate most of your strings using the
core::ffi::CStr
type rather than the usualstr
type, because you need the string to be nul-terminated before you can pass it into the C/C++ codebase. This means that working with nul-terminated strings should be somewhat convenient.Unfortunately, it's currently very difficult to print a nul-terminated string. The type does not implement
Display
and there's nodisplay()
function either. Currently, you have to do something like this to print it:Or use an extension trait for
CStr
.What about
Debug
?It doesn't print the right thing.
It wraps the string in quotes, and also escapes various characters such as quotes and newlines.
Motivating examples or use cases
This is motivated by the Linux Kernel, where we currently have a custom
CStr
type that implementsDisplay
. We would like to move away from the customCStr
so that we can usec""
literals. However, there are many places where we print a nul-terminated string for various reasons.This becomes:
Ideally we would like to be able to continue printing strings with the original syntax.
We don't really care about utf-8 here, and the thing we are printing to doesn't either. Printing replacement characters like how
String::from_utf8_lossy
would be fine.Solution sketch
The solution sketch is to implement
Display
forCStr
andCString
. The implementation would be the same as for theByteStr
type.Alternatives
The primary question to consider here is how to deal with bytes that are not valid utf-8.
Add a
.display()
functionThe
Path
type has a similar problem, but the standard library has chosen to handle it by adding a.display()
method. ThePath
type does not implement theDisplay
trait directly.I think that we should not repeat this solution for
CStr
. ThePath
type carries with it the intent that you are being careful with your paths and that you want to avoid accidentally breaking a path by round-tripping it through theString
type. TheCStr
type does not carry the same intent with it, and implementingDisplay
is more convenient because it lets you print with the"{name}"
syntax instead of having to do"{}", name.display()
.How to escape the string
This proposal says that the default way to print a
CStr
should be to use replacement characters rather than some other form of escaping. This is because cstr printing is generally used to display the string to a user, and � is much better at conveying that the data is invalid than\xff
to a non-technical user.Links and related work
Zulip thread: zulip
Adding
OsStr::display
: tracking issueLKML thread: lkml
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: