forked from jerry73204/realsense-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.rs
122 lines (104 loc) · 3.1 KB
/
error.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Defines the error type used by the crate.
use crate::common::*;
#[derive(Debug)]
pub(crate) struct ErrorChecker {
checked: bool,
ptr: *mut sys::rs2_error,
}
impl ErrorChecker {
pub fn new() -> ErrorChecker {
ErrorChecker {
checked: false,
ptr: ptr::null_mut(),
}
}
pub fn inner_mut_ptr(&mut self) -> *mut *mut sys::rs2_error {
&mut self.ptr as *mut _
}
pub fn check(mut self) -> Result<()> {
self.checked = true;
match NonNull::new(self.ptr) {
Some(nonnull) => {
let msg = get_error_message(nonnull);
let err = if msg.starts_with("Frame didn't arrive within ") {
Error::Timeout(nonnull)
} else if msg.starts_with("object doesn\'t support option #") {
Error::UnsupportedOption(nonnull)
} else {
Error::Other(nonnull)
};
Err(err)
}
None => Ok(()),
}
}
}
impl Drop for ErrorChecker {
fn drop(&mut self) {
if !self.checked {
panic!("internal error: forget to call check()");
}
}
}
/// The error type wraps around underlying error thrown by librealsense library.
pub enum Error {
ToCStrConversion(&'static str),
Timeout(NonNull<sys::rs2_error>),
UnsupportedOption(NonNull<sys::rs2_error>),
Other(NonNull<sys::rs2_error>),
}
impl Error {
pub fn error_message(&self) -> &str {
match (self, self.ptr()) {
(_, Some(ptr)) => get_error_message(ptr),
(Self::ToCStrConversion(reason), None) => reason,
_ => unreachable!(),
}
}
pub fn into_raw(self) -> Option<*mut sys::rs2_error> {
let ptr = self.ptr()?.as_ptr();
mem::forget(self);
Some(ptr)
}
pub(crate) fn ptr(&self) -> Option<NonNull<sys::rs2_error>> {
let ptr = match *self {
Error::ToCStrConversion(_reason) => return None,
Error::Timeout(ptr) => ptr,
Error::UnsupportedOption(ptr) => ptr,
Error::Other(ptr) => ptr,
};
Some(ptr)
}
}
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FormatResult {
let message = self.error_message();
write!(formatter, "RealSense error: {}", message)
}
}
impl Debug for Error {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FormatResult {
let message = self.error_message();
write!(formatter, "RealSense error: {}", message)
}
}
impl StdError for Error {}
unsafe impl Send for Error {}
unsafe impl Sync for Error {}
impl Drop for Error {
fn drop(&mut self) {
if let Some(ptr) = self.ptr() {
unsafe {
sys::rs2_free_error(ptr.as_ptr());
}
}
}
}
/// A convenient alias Result type.
pub type Result<T> = result::Result<T, Error>;
fn get_error_message<'a>(ptr: NonNull<sys::rs2_error>) -> &'a str {
unsafe {
let ptr = sys::rs2_get_error_message(ptr.as_ptr());
CStr::from_ptr(ptr).to_str().unwrap()
}
}