diff options
| author | kennytm <kennytm@gmail.com> | 2018-09-13 10:02:17 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2018-09-14 00:41:34 +0800 |
| commit | 2882119febc065f05e835ec306b93b14c6e61b1b (patch) | |
| tree | 129b09776f4a42fb21e05f5350b8d38922d40991 /src/libstd | |
| parent | 9af125d248659d16246279b03a521b8c53a183d6 (diff) | |
| parent | 72ba0ba3d7ce8a5d34bbffaca4be5da5b988499f (diff) | |
Rollup merge of #53976 - GuillaumeGomez:expect-world, r=steveklabnik
Replace unwrap calls in example by expect Part of #51668. r? @steveklabnik
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 372f3e83e3d..63dd12f782f 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -102,8 +102,8 @@ use sys; /// } /// /// // We are certain that our string doesn't have 0 bytes in the middle, -/// // so we can .unwrap() -/// let c_to_print = CString::new("Hello, world!").unwrap(); +/// // so we can .expect() +/// let c_to_print = CString::new("Hello, world!").expect("CString::new failed"); /// unsafe { /// my_printer(c_to_print.as_ptr()); /// } @@ -175,7 +175,7 @@ pub struct CString { /// unsafe { work_with(data.as_ptr()) } /// } /// -/// let s = CString::new("data data data data").unwrap(); +/// let s = CString::new("data data data data").expect("CString::new failed"); /// work(&s); /// ``` /// @@ -314,7 +314,7 @@ impl CString { /// /// extern { fn puts(s: *const c_char); } /// - /// let to_print = CString::new("Hello!").unwrap(); + /// let to_print = CString::new("Hello!").expect("CString::new failed"); /// unsafe { /// puts(to_print.as_ptr()); /// } @@ -398,7 +398,7 @@ impl CString { /// fn some_extern_function(s: *mut c_char); /// } /// - /// let c_string = CString::new("Hello!").unwrap(); + /// let c_string = CString::new("Hello!").expect("CString::new failed"); /// let raw = c_string.into_raw(); /// unsafe { /// some_extern_function(raw); @@ -428,7 +428,7 @@ impl CString { /// ``` /// use std::ffi::CString; /// - /// let c_string = CString::new("foo").unwrap(); + /// let c_string = CString::new("foo").expect("CString::new failed"); /// /// let ptr = c_string.into_raw(); /// @@ -460,12 +460,12 @@ impl CString { /// use std::ffi::CString; /// /// let valid_utf8 = vec![b'f', b'o', b'o']; - /// let cstring = CString::new(valid_utf8).unwrap(); - /// assert_eq!(cstring.into_string().unwrap(), "foo"); + /// let cstring = CString::new(valid_utf8).expect("CString::new failed"); + /// assert_eq!(cstring.into_string().expect("into_string() call failed"), "foo"); /// /// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o']; - /// let cstring = CString::new(invalid_utf8).unwrap(); - /// let err = cstring.into_string().err().unwrap(); + /// let cstring = CString::new(invalid_utf8).expect("CString::new failed"); + /// let err = cstring.into_string().err().expect("into_string().err() failed"); /// assert_eq!(err.utf8_error().valid_up_to(), 1); /// ``` @@ -489,7 +489,7 @@ impl CString { /// ``` /// use std::ffi::CString; /// - /// let c_string = CString::new("foo").unwrap(); + /// let c_string = CString::new("foo").expect("CString::new failed"); /// let bytes = c_string.into_bytes(); /// assert_eq!(bytes, vec![b'f', b'o', b'o']); /// ``` @@ -511,7 +511,7 @@ impl CString { /// ``` /// use std::ffi::CString; /// - /// let c_string = CString::new("foo").unwrap(); + /// let c_string = CString::new("foo").expect("CString::new failed"); /// let bytes = c_string.into_bytes_with_nul(); /// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']); /// ``` @@ -534,7 +534,7 @@ impl CString { /// ``` /// use std::ffi::CString; /// - /// let c_string = CString::new("foo").unwrap(); + /// let c_string = CString::new("foo").expect("CString::new failed"); /// let bytes = c_string.as_bytes(); /// assert_eq!(bytes, &[b'f', b'o', b'o']); /// ``` @@ -554,7 +554,7 @@ impl CString { /// ``` /// use std::ffi::CString; /// - /// let c_string = CString::new("foo").unwrap(); + /// let c_string = CString::new("foo").expect("CString::new failed"); /// let bytes = c_string.as_bytes_with_nul(); /// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']); /// ``` @@ -573,9 +573,10 @@ impl CString { /// ``` /// use std::ffi::{CString, CStr}; /// - /// let c_string = CString::new(b"foo".to_vec()).unwrap(); + /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed"); /// let c_str = c_string.as_c_str(); - /// assert_eq!(c_str, CStr::from_bytes_with_nul(b"foo\0").unwrap()); + /// assert_eq!(c_str, + /// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed")); /// ``` #[inline] #[stable(feature = "as_c_str", since = "1.20.0")] @@ -592,16 +593,17 @@ impl CString { /// ``` /// use std::ffi::{CString, CStr}; /// - /// let c_string = CString::new(b"foo".to_vec()).unwrap(); + /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed"); /// let boxed = c_string.into_boxed_c_str(); - /// assert_eq!(&*boxed, CStr::from_bytes_with_nul(b"foo\0").unwrap()); + /// assert_eq!(&*boxed, + /// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed")); /// ``` #[stable(feature = "into_boxed_c_str", since = "1.20.0")] pub fn into_boxed_c_str(self) -> Box<CStr> { unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) } } - // Bypass "move out of struct which implements [`Drop`] trait" restriction. + /// Bypass "move out of struct which implements [`Drop`] trait" restriction. /// /// [`Drop`]: ../ops/trait.Drop.html fn into_inner(self) -> Box<[u8]> { @@ -1031,7 +1033,7 @@ impl CStr { /// use std::ffi::{CStr, CString}; /// /// unsafe { - /// let cstring = CString::new("hello").unwrap(); + /// let cstring = CString::new("hello").expect("CString::new failed"); /// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul()); /// assert_eq!(cstr, &*cstring); /// } @@ -1058,7 +1060,7 @@ impl CStr { /// # #![allow(unused_must_use)] /// use std::ffi::{CString}; /// - /// let ptr = CString::new("Hello").unwrap().as_ptr(); + /// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr(); /// unsafe { /// // `ptr` is dangling /// *ptr; @@ -1067,14 +1069,14 @@ impl CStr { /// /// This happens because the pointer returned by `as_ptr` does not carry any /// lifetime information and the [`CString`] is deallocated immediately after - /// the `CString::new("Hello").unwrap().as_ptr()` expression is evaluated. + /// the `CString::new("Hello").expect("CString::new failed").as_ptr()` expression is evaluated. /// To fix the problem, bind the `CString` to a local variable: /// /// ```no_run /// # #![allow(unused_must_use)] /// use std::ffi::{CString}; /// - /// let hello = CString::new("Hello").unwrap(); + /// let hello = CString::new("Hello").expect("CString::new failed"); /// let ptr = hello.as_ptr(); /// unsafe { /// // `ptr` is valid because `hello` is in scope @@ -1106,7 +1108,7 @@ impl CStr { /// ``` /// use std::ffi::CStr; /// - /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap(); + /// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"); /// assert_eq!(c_str.to_bytes(), b"foo"); /// ``` #[inline] @@ -1132,7 +1134,7 @@ impl CStr { /// ``` /// use std::ffi::CStr; /// - /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap(); + /// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"); /// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0"); /// ``` #[inline] @@ -1159,7 +1161,7 @@ impl CStr { /// ``` /// use std::ffi::CStr; /// - /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap(); + /// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"); /// assert_eq!(c_str.to_str(), Ok("foo")); /// ``` #[stable(feature = "cstr_to_str", since = "1.4.0")] @@ -1200,7 +1202,8 @@ impl CStr { /// use std::borrow::Cow; /// use std::ffi::CStr; /// - /// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap(); + /// let c_str = CStr::from_bytes_with_nul(b"Hello World\0") + /// .expect("CStr::from_bytes_with_nul failed"); /// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World")); /// ``` /// @@ -1210,7 +1213,8 @@ impl CStr { /// use std::borrow::Cow; /// use std::ffi::CStr; /// - /// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap(); + /// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0") + /// .expect("CStr::from_bytes_with_nul failed"); /// assert_eq!( /// c_str.to_string_lossy(), /// Cow::Owned(String::from("Hello �World")) as Cow<str> @@ -1231,9 +1235,9 @@ impl CStr { /// ``` /// use std::ffi::CString; /// - /// let c_string = CString::new(b"foo".to_vec()).unwrap(); + /// 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").unwrap()); + /// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed")); /// ``` #[stable(feature = "into_boxed_c_str", since = "1.20.0")] pub fn into_c_string(self: Box<CStr>) -> CString { |
