Skip to content

Commit 72ba0ba

Browse files
Replace unwrap calls in example by expect
1 parent 8785e34 commit 72ba0ba

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
@@ -101,8 +101,8 @@ use sys;
101101
/// }
102102
///
103103
/// // We are certain that our string doesn't have 0 bytes in the middle,
104-
/// // so we can .unwrap()
105-
/// let c_to_print = CString::new("Hello, world!").unwrap();
104+
/// // so we can .expect()
105+
/// let c_to_print = CString::new("Hello, world!").expect("CString::new failed");
106106
/// unsafe {
107107
/// my_printer(c_to_print.as_ptr());
108108
/// }
@@ -174,7 +174,7 @@ pub struct CString {
174174
/// unsafe { work_with(data.as_ptr()) }
175175
/// }
176176
///
177-
/// let s = CString::new("data data data data").unwrap();
177+
/// let s = CString::new("data data data data").expect("CString::new failed");
178178
/// work(&s);
179179
/// ```
180180
///
@@ -313,7 +313,7 @@ impl CString {
313313
///
314314
/// extern { fn puts(s: *const c_char); }
315315
///
316-
/// let to_print = CString::new("Hello!").unwrap();
316+
/// let to_print = CString::new("Hello!").expect("CString::new failed");
317317
/// unsafe {
318318
/// puts(to_print.as_ptr());
319319
/// }
@@ -397,7 +397,7 @@ impl CString {
397397
/// fn some_extern_function(s: *mut c_char);
398398
/// }
399399
///
400-
/// let c_string = CString::new("Hello!").unwrap();
400+
/// let c_string = CString::new("Hello!").expect("CString::new failed");
401401
/// let raw = c_string.into_raw();
402402
/// unsafe {
403403
/// some_extern_function(raw);
@@ -427,7 +427,7 @@ impl CString {
427427
/// ```
428428
/// use std::ffi::CString;
429429
///
430-
/// let c_string = CString::new("foo").unwrap();
430+
/// let c_string = CString::new("foo").expect("CString::new failed");
431431
///
432432
/// let ptr = c_string.into_raw();
433433
///
@@ -459,12 +459,12 @@ impl CString {
459459
/// use std::ffi::CString;
460460
///
461461
/// let valid_utf8 = vec![b'f', b'o', b'o'];
462-
/// let cstring = CString::new(valid_utf8).unwrap();
463-
/// assert_eq!(cstring.into_string().unwrap(), "foo");
462+
/// let cstring = CString::new(valid_utf8).expect("CString::new failed");
463+
/// assert_eq!(cstring.into_string().expect("into_string() call failed"), "foo");
464464
///
465465
/// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o'];
466-
/// let cstring = CString::new(invalid_utf8).unwrap();
467-
/// let err = cstring.into_string().err().unwrap();
466+
/// let cstring = CString::new(invalid_utf8).expect("CString::new failed");
467+
/// let err = cstring.into_string().err().expect("into_string().err() failed");
468468
/// assert_eq!(err.utf8_error().valid_up_to(), 1);
469469
/// ```
470470
@@ -488,7 +488,7 @@ impl CString {
488488
/// ```
489489
/// use std::ffi::CString;
490490
///
491-
/// let c_string = CString::new("foo").unwrap();
491+
/// let c_string = CString::new("foo").expect("CString::new failed");
492492
/// let bytes = c_string.into_bytes();
493493
/// assert_eq!(bytes, vec![b'f', b'o', b'o']);
494494
/// ```
@@ -510,7 +510,7 @@ impl CString {
510510
/// ```
511511
/// use std::ffi::CString;
512512
///
513-
/// let c_string = CString::new("foo").unwrap();
513+
/// let c_string = CString::new("foo").expect("CString::new failed");
514514
/// let bytes = c_string.into_bytes_with_nul();
515515
/// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
516516
/// ```
@@ -533,7 +533,7 @@ impl CString {
533533
/// ```
534534
/// use std::ffi::CString;
535535
///
536-
/// let c_string = CString::new("foo").unwrap();
536+
/// let c_string = CString::new("foo").expect("CString::new failed");
537537
/// let bytes = c_string.as_bytes();
538538
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
539539
/// ```
@@ -553,7 +553,7 @@ impl CString {
553553
/// ```
554554
/// use std::ffi::CString;
555555
///
556-
/// let c_string = CString::new("foo").unwrap();
556+
/// let c_string = CString::new("foo").expect("CString::new failed");
557557
/// let bytes = c_string.as_bytes_with_nul();
558558
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
559559
/// ```
@@ -572,9 +572,10 @@ impl CString {
572572
/// ```
573573
/// use std::ffi::{CString, CStr};
574574
///
575-
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
575+
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
576576
/// let c_str = c_string.as_c_str();
577-
/// assert_eq!(c_str, CStr::from_bytes_with_nul(b"foo\0").unwrap());
577+
/// assert_eq!(c_str,
578+
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
578579
/// ```
579580
#[inline]
580581
#[stable(feature = "as_c_str", since = "1.20.0")]
@@ -591,16 +592,17 @@ impl CString {
591592
/// ```
592593
/// use std::ffi::{CString, CStr};
593594
///
594-
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
595+
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
595596
/// let boxed = c_string.into_boxed_c_str();
596-
/// assert_eq!(&*boxed, CStr::from_bytes_with_nul(b"foo\0").unwrap());
597+
/// assert_eq!(&*boxed,
598+
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
597599
/// ```
598600
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
599601
pub fn into_boxed_c_str(self) -> Box<CStr> {
600602
unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
601603
}
602604

603-
// Bypass "move out of struct which implements [`Drop`] trait" restriction.
605+
/// Bypass "move out of struct which implements [`Drop`] trait" restriction.
604606
///
605607
/// [`Drop`]: ../ops/trait.Drop.html
606608
fn into_inner(self) -> Box<[u8]> {
@@ -1030,7 +1032,7 @@ impl CStr {
10301032
/// use std::ffi::{CStr, CString};
10311033
///
10321034
/// unsafe {
1033-
/// let cstring = CString::new("hello").unwrap();
1035+
/// let cstring = CString::new("hello").expect("CString::new failed");
10341036
/// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
10351037
/// assert_eq!(cstr, &*cstring);
10361038
/// }
@@ -1057,7 +1059,7 @@ impl CStr {
10571059
/// # #![allow(unused_must_use)]
10581060
/// use std::ffi::{CString};
10591061
///
1060-
/// let ptr = CString::new("Hello").unwrap().as_ptr();
1062+
/// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
10611063
/// unsafe {
10621064
/// // `ptr` is dangling
10631065
/// *ptr;
@@ -1066,14 +1068,14 @@ impl CStr {
10661068
///
10671069
/// This happens because the pointer returned by `as_ptr` does not carry any
10681070
/// lifetime information and the [`CString`] is deallocated immediately after
1069-
/// the `CString::new("Hello").unwrap().as_ptr()` expression is evaluated.
1071+
/// the `CString::new("Hello").expect("CString::new failed").as_ptr()` expression is evaluated.
10701072
/// To fix the problem, bind the `CString` to a local variable:
10711073
///
10721074
/// ```no_run
10731075
/// # #![allow(unused_must_use)]
10741076
/// use std::ffi::{CString};
10751077
///
1076-
/// let hello = CString::new("Hello").unwrap();
1078+
/// let hello = CString::new("Hello").expect("CString::new failed");
10771079
/// let ptr = hello.as_ptr();
10781080
/// unsafe {
10791081
/// // `ptr` is valid because `hello` is in scope
@@ -1105,7 +1107,7 @@ impl CStr {
11051107
/// ```
11061108
/// use std::ffi::CStr;
11071109
///
1108-
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
1110+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
11091111
/// assert_eq!(c_str.to_bytes(), b"foo");
11101112
/// ```
11111113
#[inline]
@@ -1131,7 +1133,7 @@ impl CStr {
11311133
/// ```
11321134
/// use std::ffi::CStr;
11331135
///
1134-
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
1136+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
11351137
/// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0");
11361138
/// ```
11371139
#[inline]
@@ -1158,7 +1160,7 @@ impl CStr {
11581160
/// ```
11591161
/// use std::ffi::CStr;
11601162
///
1161-
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
1163+
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
11621164
/// assert_eq!(c_str.to_str(), Ok("foo"));
11631165
/// ```
11641166
#[stable(feature = "cstr_to_str", since = "1.4.0")]
@@ -1199,7 +1201,8 @@ impl CStr {
11991201
/// use std::borrow::Cow;
12001202
/// use std::ffi::CStr;
12011203
///
1202-
/// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
1204+
/// let c_str = CStr::from_bytes_with_nul(b"Hello World\0")
1205+
/// .expect("CStr::from_bytes_with_nul failed");
12031206
/// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
12041207
/// ```
12051208
///
@@ -1209,7 +1212,8 @@ impl CStr {
12091212
/// use std::borrow::Cow;
12101213
/// use std::ffi::CStr;
12111214
///
1212-
/// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
1215+
/// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0")
1216+
/// .expect("CStr::from_bytes_with_nul failed");
12131217
/// assert_eq!(
12141218
/// c_str.to_string_lossy(),
12151219
/// Cow::Owned(String::from("Hello �World")) as Cow<str>
@@ -1230,9 +1234,9 @@ impl CStr {
12301234
/// ```
12311235
/// use std::ffi::CString;
12321236
///
1233-
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
1237+
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
12341238
/// let boxed = c_string.into_boxed_c_str();
1235-
/// assert_eq!(boxed.into_c_string(), CString::new("foo").unwrap());
1239+
/// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
12361240
/// ```
12371241
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
12381242
pub fn into_c_string(self: Box<CStr>) -> CString {

0 commit comments

Comments
 (0)