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
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()");
}
}
}
pub enum Error {
Timeout(NonNull<sys::rs2_error>),
UnsupportedOption(NonNull<sys::rs2_error>),
Other(NonNull<sys::rs2_error>),
}
impl Error {
pub fn error_message(&self) -> &str {
get_error_message(self.ptr())
}
pub fn into_raw(self) -> *mut sys::rs2_error {
let ptr = self.ptr().as_ptr();
mem::forget(self);
ptr
}
pub(crate) fn ptr(&self) -> NonNull<sys::rs2_error> {
match *self {
Error::Timeout(ptr) => ptr,
Error::UnsupportedOption(ptr) => ptr,
Error::Other(ptr) => 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) {
let ptr = self.ptr();
unsafe {
sys::rs2_free_error(ptr.as_ptr());
}
}
}
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()
}
}