changes redundant .unwrap_or_else(panic!()) to .unwrap()

This commit is contained in:
Emma Tebibyte 2024-03-03 17:26:40 -07:00
parent c392dbc680
commit 8193e471f0
Signed by: emma
GPG Key ID: 06FA419A1698C270
2 changed files with 12 additions and 2 deletions

View File

@ -15,12 +15,12 @@ extern "C" { fn strerror(errnum: c_int) -> *mut c_char; }
/* wrapper function for use in Rust */ /* wrapper function for use in Rust */
pub fn raw_message(err: std::io::Error) -> String { pub fn raw_message(err: std::io::Error) -> String {
/* Get the raw OS error. If its None, what the hell is going on‽ */ /* Get the raw OS error. If its None, what the hell is going on‽ */
let error = err.raw_os_error().unwrap_or_else(|| { panic!() }) as c_int; let errno = err.raw_os_error().unwrap() as c_int;
/* Get a CStr from the error message so that its referenced and then /* Get a CStr from the error message so that its referenced and then
* convert it to an owned value. If the string is not valid UTF-8, return * convert it to an owned value. If the string is not valid UTF-8, return
* that error instead. */ * that error instead. */
match unsafe { CStr::from_ptr(strerror(error)) }.to_str() { match unsafe { CStr::from_ptr(strerror(errno)) }.to_str() {
Ok(s) => s.to_owned(), // yay!! :D Ok(s) => s.to_owned(), // yay!! :D
Err(e) => e.to_string(), // awww :( Err(e) => e.to_string(), // awww :(
} }

10
src/test.rs Normal file
View File

@ -0,0 +1,10 @@
extern crate strerror;
use strerror::raw_message;
fn main() {
stdout.write_all(b"meow\n").unwrap_or_else(|e| {
eprintln!("{}", raw_message(e));
std::process::exit(1);
});
}