-
Notifications
You must be signed in to change notification settings - Fork 13.7k
library: Move CStr
to libcore, and CString
to liballoc
#94079
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
Merged
+940
−695
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5bee741
library: Move `CStr` to libcore, and `CString` to liballoc
petrochenkov 7f3cc2f
library: Use type aliases to make `CStr(ing)` in libcore/liballoc uns…
petrochenkov afa2e6f
Fix targets not supporting `target_has_atomic = "ptr"`
petrochenkov 6eaec56
library: Remove definitions and reexports of `strlen` from libstd
petrochenkov f62c84e
clippy: Update full path to `CString`
petrochenkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
743 changes: 132 additions & 611 deletions
743
library/std/src/ffi/c_str.rs → library/alloc/src/ffi/c_str.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//! Utilities related to FFI bindings. | ||
//! | ||
//! This module provides utilities to handle data across non-Rust | ||
//! interfaces, like other programming languages and the underlying | ||
//! operating system. It is mainly of use for FFI (Foreign Function | ||
//! Interface) bindings and code that needs to exchange C-like strings | ||
//! with other languages. | ||
//! | ||
//! # Overview | ||
//! | ||
//! Rust represents owned strings with the [`String`] type, and | ||
//! borrowed slices of strings with the [`str`] primitive. Both are | ||
//! always in UTF-8 encoding, and may contain nul bytes in the middle, | ||
//! i.e., if you look at the bytes that make up the string, there may | ||
//! be a `\0` among them. Both `String` and `str` store their length | ||
//! explicitly; there are no nul terminators at the end of strings | ||
//! like in C. | ||
//! | ||
//! C strings are different from Rust strings: | ||
//! | ||
//! * **Encodings** - Rust strings are UTF-8, but C strings may use | ||
//! other encodings. If you are using a string from C, you should | ||
//! check its encoding explicitly, rather than just assuming that it | ||
//! is UTF-8 like you can do in Rust. | ||
//! | ||
//! * **Character size** - C strings may use `char` or `wchar_t`-sized | ||
//! characters; please **note** that C's `char` is different from Rust's. | ||
//! The C standard leaves the actual sizes of those types open to | ||
//! interpretation, but defines different APIs for strings made up of | ||
//! each character type. Rust strings are always UTF-8, so different | ||
//! Unicode characters will be encoded in a variable number of bytes | ||
//! each. The Rust type [`char`] represents a '[Unicode scalar | ||
//! value]', which is similar to, but not the same as, a '[Unicode | ||
//! code point]'. | ||
//! | ||
//! * **Nul terminators and implicit string lengths** - Often, C | ||
//! strings are nul-terminated, i.e., they have a `\0` character at the | ||
//! end. The length of a string buffer is not stored, but has to be | ||
//! calculated; to compute the length of a string, C code must | ||
//! manually call a function like `strlen()` for `char`-based strings, | ||
//! or `wcslen()` for `wchar_t`-based ones. Those functions return | ||
//! the number of characters in the string excluding the nul | ||
//! terminator, so the buffer length is really `len+1` characters. | ||
//! Rust strings don't have a nul terminator; their length is always | ||
//! stored and does not need to be calculated. While in Rust | ||
//! accessing a string's length is an *O*(1) operation (because the | ||
//! length is stored); in C it is an *O*(*n*) operation because the | ||
//! length needs to be computed by scanning the string for the nul | ||
//! terminator. | ||
//! | ||
//! * **Internal nul characters** - When C strings have a nul | ||
//! terminator character, this usually means that they cannot have nul | ||
//! characters in the middle — a nul character would essentially | ||
//! truncate the string. Rust strings *can* have nul characters in | ||
//! the middle, because nul does not have to mark the end of the | ||
//! string in Rust. | ||
//! | ||
//! # Representations of non-Rust strings | ||
//! | ||
//! [`CString`] and [`CStr`] are useful when you need to transfer | ||
//! UTF-8 strings to and from languages with a C ABI, like Python. | ||
//! | ||
//! * **From Rust to C:** [`CString`] represents an owned, C-friendly | ||
//! string: it is nul-terminated, and has no internal nul characters. | ||
//! Rust code can create a [`CString`] out of a normal string (provided | ||
//! that the string doesn't have nul characters in the middle), and | ||
//! then use a variety of methods to obtain a raw <code>\*mut [u8]</code> that can | ||
//! then be passed as an argument to functions which use the C | ||
//! conventions for strings. | ||
//! | ||
//! * **From C to Rust:** [`CStr`] represents a borrowed C string; it | ||
//! is what you would use to wrap a raw <code>\*const [u8]</code> that you got from | ||
//! a C function. A [`CStr`] is guaranteed to be a nul-terminated array | ||
//! of bytes. Once you have a [`CStr`], you can convert it to a Rust | ||
//! <code>&[str]</code> if it's valid UTF-8, or lossily convert it by adding | ||
//! replacement characters. | ||
//! | ||
//! [`String`]: crate::string::String | ||
//! [`CStr`]: core::ffi::CStr | ||
#![unstable(feature = "alloc_ffi", issue = "94079")] | ||
|
||
#[cfg(bootstrap)] | ||
#[unstable(feature = "cstr_internals", issue = "none")] | ||
pub use self::c_str::CStrExt; | ||
#[unstable(feature = "alloc_c_string", issue = "94079")] | ||
pub use self::c_str::FromVecWithNulError; | ||
#[unstable(feature = "alloc_c_string", issue = "94079")] | ||
pub use self::c_str::{CString, IntoStringError, NulError}; | ||
|
||
mod c_str; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::borrow::Cow::{Borrowed, Owned}; | ||
use std::ffi::{c_char, CStr}; | ||
|
||
#[test] | ||
fn to_str() { | ||
let data = b"123\xE2\x80\xA6\0"; | ||
let ptr = data.as_ptr() as *const c_char; | ||
unsafe { | ||
assert_eq!(CStr::from_ptr(ptr).to_str(), Ok("123…")); | ||
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Borrowed("123…")); | ||
} | ||
let data = b"123\xE2\0"; | ||
let ptr = data.as_ptr() as *const c_char; | ||
unsafe { | ||
assert!(CStr::from_ptr(ptr).to_str().is_err()); | ||
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}"))); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,5 +99,3 @@ pub fn hashmap_random_keys() -> (u64, u64) { | |
(x1, x2) | ||
} | ||
} | ||
|
||
pub use libc::strlen; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this change related? It doesn't seem to be documented in the PR.
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.
This is a bugfix for #94963 to account for incoherent impl types having
DefId
s, without it this PR ICEs.