Skip to content

Commit 2882119

Browse files
committed
Rollup merge of rust-lang#53976 - GuillaumeGomez:expect-world, r=steveklabnik
Replace unwrap calls in example by expect Part of rust-lang#51668. r? @steveklabnik
2 parents 9af125d + 72ba0ba commit 2882119

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/libstd/ffi/c_str.rs

+34-30
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ use sys;
102102
/// }
103103
///
104104
/// // We are certain that our string doesn't have 0 bytes in the middle,
105-
/// // so we can .unwrap()
106-
/// let c_to_print = CString::new("Hello, world!").unwrap();
105+
/// // so we can .expect()
106+
/// let c_to_print = CString::new("Hello, world!").expect("CString::new failed");
107107
/// unsafe {
108108
/// my_printer(c_to_print.as_ptr());
109109
/// }
@@ -175,7 +175,7 @@ pub struct CString {
175175
/// unsafe { work_with(data.as_ptr()) }
176176
/// }
177177
///
178-
/// let s = CString::new("data data data data").unwrap();
178+
/// let s = CString::new("data data data data").expect("CString::new failed");
179179
/// work(&s);
180180
/// ```
181181
///
@@ -314,7 +314,7 @@ impl CString {
314314
///
315315
/// extern { fn puts(s: *const c_char); }
316316
///
317-
/// let to_print = CString::new("Hello!").unwrap();
317+
/// let to_print = CString::new("Hello!").expect("CString::new failed");
318318
/// unsafe {
319319
/// puts(to_print.as_ptr());
320320
/// }
@@ -398,7 +398,7 @@ impl CString {
398398
/// fn some_extern_function(s: *mut c_char);
399399
/// }
400400
///
401-
/// let c_string = CString::new("Hello!").unwrap();
401+
/// let c_string = CString::new("Hello!").expect("CString::new failed");
402402
/// let raw = c_string.into_raw();
403403
/// unsafe {
404404
/// some_extern_function(raw);
@@ -428,7 +428,7 @@ impl CString {
428428
/// ```
429429
/// use std::ffi::CString;
430430
///
431-
/// let c_string = CString::new("foo").unwrap();
431+
/// let c_string = CString::new("foo").expect("CString::new failed");
432432
///
433433
/// let ptr = c_string.into_raw();
434434
///
@@ -460,12 +460,12 @@ impl CString {
460460
/// use std::ffi::CString;
461461
///
462462
/// let valid_utf8 = vec![b'f', b'o', b'o'];
463-
/// let cstring = CString::new(valid_utf8).unwrap();
464-
/// assert_eq!(cstring.into_string().unwrap(), "foo");
463+
/// let cstring = CString::new(valid_utf8).expect("CString::new failed");
464+
/// assert_eq!(cstring.into_string().expect("into_string() call failed"), "foo");
465465
///
466466
/// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o'];
467-
/// let cstring = CString::new(invalid_utf8).unwrap();
468-
/// let err = cstring.into_string().err().unwrap();
467+
/// let cstring = CString::new(invalid_utf8).expect("CString::new failed");
468+
/// let err = cstring.into_string().err().expect("into_string().err() failed");
469469
/// assert_eq!(err.utf8_error().valid_up_to(), 1);
470470
/// ```
471471
@@ -489,7 +489,7 @@ impl CString {
489489
/// ```
490490
/// use std::ffi::CString;
491491
///
492-
/// let c_string = CString::new("foo").unwrap();
492+
/// let c_string = CString::new("foo").expect("CString::new failed");
493493
/// let bytes = c_string.into_bytes();
494494
/// assert_eq!(bytes, vec![b'f', b'o', b'o']);
495495
/// ```
@@ -511,7 +511,7 @@ impl CString {
511511
/// ```
512512
/// use std::ffi::CString;
513513
///
514-
/// let c_string = CString::new("foo").unwrap();
514+
/// let c_string = CString::new("foo").expect("CString::new failed");
515515
/// let bytes = c_string.into_bytes_with_nul();
516516
/// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
517517
/// ```
@@ -534,7 +534,7 @@ impl CString {
534534
/// ```
535535
/// use std::ffi::CString;
536536
///
537-
/// let c_string = CString::new("foo").unwrap();
537+
/// let c_string = CString::new("foo").expect("CString::new failed");
538538
/// let bytes = c_string.as_bytes();
539539
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
540540
/// ```
@@ -554,7 +554,7 @@ impl CString {
554554
/// ```
555555
/// use std::ffi::CString;
556556
///
557-
/// let c_string = CString::new("foo").unwrap();
557+
/// let c_string = CString::new("foo").expect("CString::new failed");
558558
/// let bytes = c_string.as_bytes_with_nul();
559559
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
560560
/// ```
@@ -573,9 +573,10 @@ impl CString {
573573
/// ```
574574
/// use std::ffi::{CString, CStr};
575575
///
576-
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
576+
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
577577
/// let c_str = c_string.as_c_str();
578-
/// assert_eq!(c_str, CStr::from_bytes_with_nul(b"foo\0").unwrap());
578+
/// assert_eq!(c_str,
579+
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
579580
/// ```
580581
#[inline]
581582
#[stable(feature = "as_c_str", since = "1.20.0")]
@@ -592,16 +593,17 @@ impl CString {
592593
/// ```
593594
/// use std::ffi::{CString, CStr};
594595
///
595-
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
596+
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
596597
/// let boxed = c_string.into_boxed_c_str();
597-
/// assert_eq!(&*boxed, CStr::from_bytes_with_nul(b"foo\0").unwrap());
598+
/// assert_eq!(&*boxed,
599+
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
598600
/// ```
599601
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
600602
pub fn into_boxed_c_str(self) -> Box<CStr> {
601603
unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
602604
}
603605

604-
// Bypass "move out of struct which implements [`Drop`] trait" restriction.
606+
/// Bypass "move out of struct which implements [`Drop`] trait" restriction.
605607
///
606608
/// [`Drop`]: ../ops/trait.Drop.html
607609
fn into_inner(self) -> Box<[u8]> {
@@ -1031,7 +1033,7 @@ impl CStr {
10311033
/// use std::ffi::{CStr, CString};
10321034
///
10331035
/// unsafe {
1034-
/// let cstring = CString::new("hello").unwrap();
1036+
/// let cstring = CString::new("hello").expect("CString::new failed");
10351037
/// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
10361038
/// assert_eq!(cstr, &*cstring);
10371039
/// }
@@ -1058,7 +1060,7 @@ impl CStr {
10581060
/// # #![allow(unused_must_use)]
10591061
/// use std::ffi::{CString};
10601062
///
1061-
/// let ptr = CString::new("Hello").unwrap().as_ptr();
1063+
/// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
10621064
/// unsafe {
10631065
/// // `ptr` is dangling
10641066
/// *ptr;
@@ -1067,14 +1069,14 @@ impl CStr {
10671069
///
10681070
/// This happens because the pointer returned by `as_ptr` does not carry any
10691071
/// lifetime information and the [`CString`] is deallocated immediately after
1070-
/// the `CString::new("Hello").unwrap().as_ptr()` expression is evaluated.
1072+
/// the `CString::new("Hello").expect("CString::new failed").as_ptr()` expression is evaluated.
10711073
/// To fix the problem, bind the `CString` to a local variable:
10721074
///
10731075
/// ```no_run
10741076
/// # #![allow(unused_must_use)]
10751077
/// use std::ffi::{CString};
10761078
///
1077-
/// let hello = CString::new("Hello").unwrap();
1079+
/// let hello = CString::new("Hello").expect("CString::new failed");
10781080
/// let ptr = hello.as_ptr();
10791081
/// unsafe {
10801082
/// // `ptr` is valid because `hello` is in scope
@@ -1106,7 +1108,7 @@ impl CStr {
11061108
/// ```
11071109
/// use std::ffi::CStr;
11081110
///
1109-
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
1111+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
11101112
/// assert_eq!(c_str.to_bytes(), b"foo");
11111113
/// ```
11121114
#[inline]
@@ -1132,7 +1134,7 @@ impl CStr {
11321134
/// ```
11331135
/// use std::ffi::CStr;
11341136
///
1135-
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
1137+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
11361138
/// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0");
11371139
/// ```
11381140
#[inline]
@@ -1159,7 +1161,7 @@ impl CStr {
11591161
/// ```
11601162
/// use std::ffi::CStr;
11611163
///
1162-
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
1164+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
11631165
/// assert_eq!(c_str.to_str(), Ok("foo"));
11641166
/// ```
11651167
#[stable(feature = "cstr_to_str", since = "1.4.0")]
@@ -1200,7 +1202,8 @@ impl CStr {
12001202
/// use std::borrow::Cow;
12011203
/// use std::ffi::CStr;
12021204
///
1203-
/// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
1205+
/// let c_str = CStr::from_bytes_with_nul(b"Hello World\0")
1206+
/// .expect("CStr::from_bytes_with_nul failed");
12041207
/// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
12051208
/// ```
12061209
///
@@ -1210,7 +1213,8 @@ impl CStr {
12101213
/// use std::borrow::Cow;
12111214
/// use std::ffi::CStr;
12121215
///
1213-
/// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
1216+
/// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0")
1217+
/// .expect("CStr::from_bytes_with_nul failed");
12141218
/// assert_eq!(
12151219
/// c_str.to_string_lossy(),
12161220
/// Cow::Owned(String::from("Hello �World")) as Cow<str>
@@ -1231,9 +1235,9 @@ impl CStr {
12311235
/// ```
12321236
/// use std::ffi::CString;
12331237
///
1234-
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
1238+
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
12351239
/// let boxed = c_string.into_boxed_c_str();
1236-
/// assert_eq!(boxed.into_c_string(), CString::new("foo").unwrap());
1240+
/// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
12371241
/// ```
12381242
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
12391243
pub fn into_c_string(self: Box<CStr>) -> CString {

0 commit comments

Comments
 (0)