forked from bonsai/harakit
changes redundant .unwrap_or_else(panic!()) to .unwrap()
This commit is contained in:
parent
c392dbc680
commit
8193e471f0
@ -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 it’s None, what the hell is going on‽ */
|
/* Get the raw OS error. If it’s 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 it’s referenced and then
|
/* Get a CStr from the error message so that it’s 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
10
src/test.rs
Normal 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);
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user