strerror(3): created strerror() method
This commit is contained in:
parent
b356ac522f
commit
1891c3e1aa
@ -9,19 +9,23 @@
|
|||||||
|
|
||||||
use std::ffi::{ c_int, c_char, CStr };
|
use std::ffi::{ c_int, c_char, CStr };
|
||||||
|
|
||||||
/* binding to strerror(3p) */
|
pub trait StrError { fn strerror(&self) -> String; }
|
||||||
extern "C" { fn strerror(errnum: c_int) -> *mut c_char; }
|
|
||||||
|
|
||||||
/* wrapper function for use in Rust */
|
impl StrError for std::io::Error {
|
||||||
pub fn raw_message(err: std::io::Error) -> String {
|
/* wrapper function for use in Rust */
|
||||||
/* Get the raw OS error. If it’s None, what the hell is going on‽ */
|
fn strerror(&self) -> String {
|
||||||
let errno = err.raw_os_error().unwrap_or(0) as c_int;
|
/* Get the raw OS error. If it’s None, what the hell is going on‽ */
|
||||||
|
let errno = self.raw_os_error().unwrap_or(0) 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,
|
||||||
* that error instead. */
|
* return that error instead. */
|
||||||
match unsafe { CStr::from_ptr(strerror(errno)) }.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 :(
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* binding to strerror(3p) */
|
||||||
|
extern "C" { fn strerror(errnum: c_int) -> *mut c_char; }
|
||||||
|
Loading…
Reference in New Issue
Block a user