forked from jerry73204/realsense-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor_list.rs
231 lines (203 loc) · 6.61 KB
/
sensor_list.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! Defines the iterable list of sensors.
use crate::{
common::*,
error::{ErrorChecker, Result},
sensor::{
AnySensor, ColorSensor, DepthSensor, DepthStereoSensor, FishEyeSensor, L500DepthSensor,
MotionSensor, PoseSensor, Sensor, SoftwareSensor, Tm2Sensor,
},
};
/// An iterable list of sensors.
#[derive(Debug)]
pub struct SensorList {
ptr: NonNull<sys::rs2_sensor_list>,
}
impl SensorList {
/// Gets the sensor instance at given index.
///
/// It returns error if index is out of bound given by [SensorList::len].
pub fn get(&mut self, index: usize) -> Result<AnySensor> {
let sensor = unsafe {
let mut checker = ErrorChecker::new();
let ptr =
sys::rs2_create_sensor(self.ptr.as_ptr(), index as c_int, checker.inner_mut_ptr());
checker.check()?;
Sensor::from_raw(ptr as *mut _)
};
Ok(sensor)
}
/// Gets the number of sensors in list.
pub fn len(&mut self) -> Result<usize> {
let len = unsafe {
let mut checker = ErrorChecker::new();
let len = sys::rs2_get_sensors_count(self.ptr.as_ptr(), checker.inner_mut_ptr());
checker.check()?;
len
};
Ok(len as usize)
}
/// Checks if the list is empty.
pub fn is_empty(&mut self) -> Result<bool> {
Ok(self.len()? == 0)
}
/// Turns into [SensorListIntoIter] iterable type.
pub fn try_into_iter(mut self) -> Result<SensorListIntoIter> {
let len = self.len()?;
let ptr = self.into_raw();
let iter = SensorListIntoIter {
len,
index: 0,
ptr: NonNull::new(ptr).unwrap(),
fused: len == 0,
};
Ok(iter)
}
pub fn first_color_sensor(self) -> Result<Option<ColorSensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.color();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn first_depth_sensor(self) -> Result<Option<DepthSensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.depth();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn first_depth_stereo_sensor(self) -> Result<Option<DepthStereoSensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.depth_stereo();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn first_fish_eye_sensor(self) -> Result<Option<FishEyeSensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.fish_eye();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn first_l500_depth_sensor(self) -> Result<Option<L500DepthSensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.l500_depth();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn first_motion_sensor(self) -> Result<Option<MotionSensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.motion();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn first_pose_sensor(self) -> Result<Option<PoseSensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.pose();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn first_software_sensor(self) -> Result<Option<SoftwareSensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.software();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn first_tm2_sensor(self) -> Result<Option<Tm2Sensor>> {
self.try_into_iter()?
.map(|result| -> Result<_> {
let sensor = result?.try_extend()?.tm2();
Ok(sensor)
})
.find_map(|result| result.transpose())
.transpose()
}
pub fn into_raw(self) -> *mut sys::rs2_sensor_list {
let ptr = self.ptr;
mem::forget(self);
ptr.as_ptr()
}
pub unsafe fn from_raw(ptr: *mut sys::rs2_sensor_list) -> Self {
Self {
ptr: NonNull::new(ptr).unwrap(),
}
}
}
impl IntoIterator for SensorList {
type Item = Result<AnySensor>;
type IntoIter = SensorListIntoIter;
/// The method internally calls [SensorList::try_into_iter](SensorList::try_into_iter).
///
/// # Panics
/// It panics if [SensorList::try_into_iter](SensorList::try_into_iter) returns error.
fn into_iter(self) -> Self::IntoIter {
self.try_into_iter().unwrap()
}
}
/// The iterator type returned by [SensorList::try_into_iter](SensorList::try_into_iter).
#[derive(Debug)]
pub struct SensorListIntoIter {
len: usize,
index: usize,
ptr: NonNull<sys::rs2_sensor_list>,
fused: bool,
}
impl Iterator for SensorListIntoIter {
type Item = Result<AnySensor>;
fn next(&mut self) -> Option<Self::Item> {
if self.fused {
return None;
}
let ptr = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_create_sensor(
self.ptr.as_ptr(),
self.index as c_int,
checker.inner_mut_ptr(),
);
match checker.check() {
Ok(()) => ptr,
Err(err) => {
self.fused = true;
return Some(Err(err));
}
}
};
self.index += 1;
if self.index >= self.len {
self.fused = true;
}
let sensor = unsafe { Sensor::from_raw(ptr) };
Some(Ok(sensor))
}
}
impl FusedIterator for SensorListIntoIter {}
unsafe impl Send for SensorList {}
impl Drop for SensorList {
fn drop(&mut self) {
unsafe {
sys::rs2_delete_sensor_list(self.ptr.as_ptr());
}
}
}