Skip to content

Commit

Permalink
Use c"lit" for CStrings without unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski authored and gitbot committed Feb 20, 2025
1 parent a6dcdc1 commit d1c9629
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
32 changes: 15 additions & 17 deletions alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl CString {
/// fn some_extern_function(s: *mut c_char);
/// }
///
/// let c_string = CString::new("Hello!").expect("CString::new failed");
/// let c_string = CString::from(c"Hello!");
/// let raw = c_string.into_raw();
/// unsafe {
/// some_extern_function(raw);
Expand Down Expand Up @@ -429,7 +429,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let c_string = CString::from(c"foo");
///
/// let ptr = c_string.into_raw();
///
Expand Down Expand Up @@ -487,7 +487,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let c_string = CString::from(c"foo");
/// let bytes = c_string.into_bytes();
/// assert_eq!(bytes, vec![b'f', b'o', b'o']);
/// ```
Expand All @@ -508,7 +508,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let c_string = CString::from(c"foo");
/// let bytes = c_string.into_bytes_with_nul();
/// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
/// ```
Expand All @@ -530,7 +530,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let c_string = CString::from(c"foo");
/// let bytes = c_string.as_bytes();
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
/// ```
Expand All @@ -550,7 +550,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let c_string = CString::from(c"foo");
/// let bytes = c_string.as_bytes_with_nul();
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
/// ```
Expand All @@ -568,7 +568,7 @@ impl CString {
/// ```
/// use std::ffi::{CString, CStr};
///
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
/// let c_string = CString::from(c"foo");
/// let cstr = c_string.as_c_str();
/// assert_eq!(cstr,
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
Expand All @@ -586,12 +586,9 @@ impl CString {
/// # Examples
///
/// ```
/// use std::ffi::{CString, CStr};
///
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
/// let c_string = c"foo".to_owned();
/// let boxed = c_string.into_boxed_c_str();
/// assert_eq!(&*boxed,
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
/// assert_eq!(boxed.to_bytes_with_nul(), b"foo\0");
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
Expand Down Expand Up @@ -658,7 +655,7 @@ impl CString {
/// assert_eq!(
/// CString::from_vec_with_nul(b"abc\0".to_vec())
/// .expect("CString::from_vec_with_nul failed"),
/// CString::new(b"abc".to_vec()).expect("CString::new failed")
/// c"abc".to_owned()
/// );
/// ```
///
Expand Down Expand Up @@ -1168,11 +1165,12 @@ impl CStr {
/// # Examples
///
/// ```
/// use std::ffi::CString;
/// use std::ffi::{CStr, CString};
///
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
/// let boxed = c_string.into_boxed_c_str();
/// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
/// let boxed: Box<CStr> = Box::from(c"foo");
/// let c_string: CString = c"foo".to_owned();
///
/// assert_eq!(boxed.into_c_string(), c_string);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "`self` will be dropped if the result is not used"]
Expand Down
2 changes: 1 addition & 1 deletion alloc/src/ffi/c_str/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn boxed_default() {

#[test]
fn test_c_str_clone_into() {
let mut c_string = CString::new("lorem").unwrap();
let mut c_string = c"lorem".to_owned();
let c_ptr = c_string.as_ptr();
let c_str = CStr::from_bytes_with_nul(b"ipsum\0").unwrap();
c_str.clone_into(&mut c_string);
Expand Down
4 changes: 2 additions & 2 deletions alloc/src/rc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ fn test_unsized() {
#[test]
fn test_maybe_thin_unsized() {
// If/when custom thin DSTs exist, this test should be updated to use one
use std::ffi::{CStr, CString};
use std::ffi::CStr;

let x: Rc<CStr> = Rc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
let x: Rc<CStr> = Rc::from(c"swordfish");
assert_eq!(format!("{x:?}"), "\"swordfish\"");
let y: Weak<CStr> = Rc::downgrade(&x);
drop(x);
Expand Down
4 changes: 2 additions & 2 deletions alloc/src/sync/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,9 @@ fn test_unsized() {
#[test]
fn test_maybe_thin_unsized() {
// If/when custom thin DSTs exist, this test should be updated to use one
use std::ffi::{CStr, CString};
use std::ffi::CStr;

let x: Arc<CStr> = Arc::from(CString::new("swordfish").unwrap().into_boxed_c_str());
let x: Arc<CStr> = Arc::from(c"swordfish");
assert_eq!(format!("{x:?}"), "\"swordfish\"");
let y: Weak<CStr> = Arc::downgrade(&x);
drop(x);
Expand Down
2 changes: 1 addition & 1 deletion std/src/sys/pal/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl Command {
fn os2c(s: &OsStr, saw_nul: &mut bool) -> CString {
CString::new(s.as_bytes()).unwrap_or_else(|_e| {
*saw_nul = true;
CString::new("<string-with-nul>").unwrap()
c"<string-with-nul>".to_owned()
})
}

Expand Down

0 comments on commit d1c9629

Please sign in to comment.