-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Additions/improvements for doc examples. #42749
Conversation
src/libstd/ffi/c_str.rs
Outdated
/// ``` | ||
/// | ||
/// Had `c_str` contained invalid unicode, the `to_string_lossy` call might | ||
/// have returned `Cow::Borrowed("fo�")`. |
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.
How about adding an example for the invalid Unicode case? Either way is definitely wouldn't return Cow::Borrowed
it would return Cow::Owned
.
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 figured it'd be undefined behavior to create a string w/ invalid unicode, as your comment here.
Either way is definitely wouldn't return Cow::Borrowed it would return Cow::Owned.
Yep, good catch, I'll change that
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 figured it'd be undefined behavior to create a string w/ invalid unicode
String
s and str
s have to contain valid UTF-8 but CString
s and Cstr
s are just sequences of any none null bytes. The following example would be fine:
use std::borrow::Cow;
use std::ffi::CStr;
let c_str = CStr::from_bytes_with_nul(b"foo\xc3\0").unwrap();
assert_eq!(c_str.to_string_lossy(), "foo�");
It would also be more correct to say "(in)valid UTF-8" rather than "(in)valid unicode" in this case.
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 worth borrowing from the example in String::from_utf8_lossy
, which uses the input string b"Hello \xF0\x90\x80World"
to demonstrate the replacement.
Looks good, other than the discussion surrounding |
098c0ef
to
4a408c6
Compare
Fixed the failing doc test and updated the |
4a408c6
to
2aeebbb
Compare
src/libstd/ffi/c_str.rs
Outdated
/// Create a `CString`, pass ownership to an `extern` function (via raw pointer), then retake | ||
/// ownership with `from_raw`: | ||
/// | ||
/// ```ignore |
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.
Could you change this to ```no_run
? Also the some_external_function(raw)
on line 307 should be some_extern_function(raw)
.
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 think I'll get linker errors upon building, right?
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.
Latest force push addresses the typo, good catch
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.
@frewsxcv Currently no_run
(and compile_fail
as well) will stop after analysis. It won't even run codegen.
97de688
to
94b09dd
Compare
Changed |
☔ The latest upstream changes (presumably #42716) made this pull request unmergeable. Please resolve the merge conflicts. |
94b09dd
to
58bbe1d
Compare
Rebased, addressed merge conflicts |
@bors r+ |
📌 Commit 58bbe1d has been approved by |
@bors rollup |
…QuietMisdreavus Additions/improvements for doc examples. None
No description provided.