Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tathanhdinh/error_support_wip
error handling
- Loading branch information
Showing
7 changed files
with
154 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
edition = "2018" | ||
indent_style = "block" | ||
format_strings = true | ||
merge_imports = true | ||
reorder_imports = true | ||
wrap_comments = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub const PAGE_SHIFT: u32 = xenctrl_sys::XC_PAGE_SHIFT; | ||
pub const PAGE_SIZE: u32 = xenctrl_sys::XC_PAGE_SIZE; | ||
pub const PAGE_MASK: i32 = xenctrl_sys::XC_PAGE_MASK; | ||
|
||
pub const CORE_MAGIC: u32 = xenctrl_sys::XC_CORE_MAGIC; | ||
pub const CORE_MAGIC_HVM: u32 = xenctrl_sys::XC_CORE_MAGIC_HVM; | ||
|
||
pub const CPUPOOL_POOLID_ANY: u32 = xenctrl_sys::XC_CPUPOOL_POOLID_ANY; | ||
|
||
pub const MAX_ERROR_MSG_LEN: u32 = xenctrl_sys::XC_MAX_ERROR_MSG_LEN; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::{ | ||
error, ffi, | ||
fmt::{self, Debug, Display, Formatter}, | ||
}; | ||
|
||
use xenctrl_sys::{xc_error_code, xc_error_code_to_desc}; | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct Error(xc_error_code); | ||
|
||
impl Error { | ||
pub fn new(code: xc_error_code) -> Self { | ||
Self(code) | ||
} | ||
|
||
pub fn desc(self) -> &'static str { | ||
unsafe { | ||
let desc = xc_error_code_to_desc(self.0 as _); | ||
ffi::CStr::from_ptr(desc).to_str().unwrap() | ||
} | ||
} | ||
} | ||
|
||
impl Display for Error { | ||
#[inline] | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.desc()) | ||
} | ||
} | ||
|
||
impl Debug for Error { | ||
#[inline] | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.desc()) | ||
} | ||
} | ||
|
||
impl error::Error for Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
macro_rules! last_error { | ||
($xc:expr, $ok:expr) => { | ||
unsafe { | ||
let err = xc_get_last_error($xc); | ||
match (*err).code { | ||
xc_error_code::XC_ERROR_NONE => Ok($ok), | ||
code => Err(Error::new(code)), | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[repr(C)] | ||
pub struct PageInfo { | ||
opaque: [u8; 0], | ||
} |