Skip to content

Commit d90ee83

Browse files
committed
Add iterators for streams, provide .iter on device list
This slightly reworks the DeviceList iterator, but not much. Topic: stream-iterators
1 parent d28b0aa commit d90ee83

3 files changed

Lines changed: 96 additions & 25 deletions

File tree

src/device.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ impl<'a> DeviceList<'a> {
289289

290290
device.map(Device::new).map_err(OrbbecError::from)
291291
}
292+
293+
/// Get an iterator over the devices in the list.
294+
pub fn iter(&self) -> DeviceListIterator<'a, '_> {
295+
DeviceListIterator::new(self)
296+
}
292297
}
293298

294299
/// An iterator over the devices in a device list
@@ -299,12 +304,12 @@ pub struct DeviceListIterator<'a, 'b> {
299304
}
300305

301306
impl<'a, 'b> DeviceListIterator<'a, 'b> {
302-
fn new(device_list: &'a DeviceList<'b>) -> Result<Self, OrbbecError> {
303-
Ok(DeviceListIterator {
307+
fn new(device_list: &'b DeviceList<'a>) -> Self {
308+
DeviceListIterator {
304309
device_list,
305310
index: 0,
306311
count: device_list.len(),
307-
})
312+
}
308313
}
309314
}
310315

@@ -313,12 +318,12 @@ impl<'a, 'b> Iterator for DeviceListIterator<'a, 'b> {
313318

314319
fn next(&mut self) -> Option<Self::Item> {
315320
if self.index >= self.count {
316-
None
317-
} else {
318-
let device = self.device_list.get(self.index);
319-
self.index += 1;
320-
Some(device)
321+
return None;
321322
}
323+
324+
let device = self.device_list.get(self.index);
325+
self.index += 1;
326+
Some(device)
322327
}
323328
}
324329

@@ -330,6 +335,6 @@ where
330335
type IntoIter = DeviceListIterator<'a, 'b>;
331336

332337
fn into_iter(self) -> Self::IntoIter {
333-
DeviceListIterator::<'a, 'b>::new(self).unwrap()
338+
self.iter()
334339
}
335340
}

src/stream.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,70 @@ impl StreamProfileList {
9696
.map(VideoStreamProfile::new)
9797
.map_err(OrbbecError::from)
9898
}
99+
100+
/// Get the number of stream profiles in this list
101+
pub fn len(&self) -> Result<usize, OrbbecError> {
102+
self.inner
103+
.get_count()
104+
.map(|count| count as usize)
105+
.map_err(OrbbecError::from)
106+
}
107+
108+
/// Return whether this list has no profiles
109+
pub fn is_empty(&self) -> Result<bool, OrbbecError> {
110+
Ok(self.len()? == 0)
111+
}
112+
113+
/// Get the video stream profile at `index`
114+
pub fn get(&self, index: usize) -> Result<VideoStreamProfile, OrbbecError> {
115+
self.inner
116+
.get_stream_profile(index as u32)
117+
.map(VideoStreamProfile::new)
118+
.map_err(OrbbecError::from)
119+
}
120+
121+
/// Get an iterator over the stream profiles in the list.
122+
pub fn iter(&self) -> StreamProfileListIterator<'_> {
123+
StreamProfileListIterator::new(self)
124+
}
125+
}
126+
127+
/// An iterator over video stream profiles in a stream profile list
128+
pub struct StreamProfileListIterator<'a> {
129+
profile_list: &'a StreamProfileList,
130+
index: usize,
131+
count: usize,
132+
}
133+
134+
impl<'a> StreamProfileListIterator<'a> {
135+
fn new(profile_list: &'a StreamProfileList) -> Self {
136+
StreamProfileListIterator {
137+
profile_list,
138+
index: 0,
139+
count: profile_list.len().unwrap(),
140+
}
141+
}
142+
}
143+
144+
impl<'a> Iterator for StreamProfileListIterator<'a> {
145+
type Item = Result<VideoStreamProfile, OrbbecError>;
146+
147+
fn next(&mut self) -> Option<Self::Item> {
148+
if self.index >= self.count {
149+
None
150+
} else {
151+
let profile = self.profile_list.get(self.index);
152+
self.index += 1;
153+
Some(profile)
154+
}
155+
}
156+
}
157+
158+
impl<'a> IntoIterator for &'a StreamProfileList {
159+
type Item = Result<VideoStreamProfile, OrbbecError>;
160+
type IntoIter = StreamProfileListIterator<'a>;
161+
162+
fn into_iter(self) -> Self::IntoIter {
163+
self.iter()
164+
}
99165
}

src/sys/stream.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,27 @@ impl OBStreamProfileList {
8686
OBStreamProfileList { inner }
8787
}
8888

89-
// /// Get the number of stream profiles in the list
90-
// pub fn get_count(&self) -> Result<u32, OBError> {
91-
// let mut err_ptr = std::ptr::null_mut();
89+
/// Get the number of stream profiles in the list
90+
pub fn get_count(&self) -> Result<u32, OBError> {
91+
let mut err_ptr = std::ptr::null_mut();
9292

93-
// let count = unsafe { orb::ob_stream_profile_list_get_count(self.inner, &mut err_ptr) };
94-
// OBError::consume(err_ptr)?;
93+
let count = unsafe { orb::ob_stream_profile_list_get_count(self.inner, &mut err_ptr) };
94+
OBError::consume(err_ptr)?;
9595

96-
// Ok(count)
97-
// }
96+
Ok(count)
97+
}
9898

99-
// /// Get the stream profile at the specified index
100-
// pub fn get_stream_profile(&self, index: i32) -> Result<OBStreamProfile, OBError> {
101-
// let mut err_ptr = std::ptr::null_mut();
99+
/// Get the stream profile at the specified index
100+
pub fn get_stream_profile(&self, index: u32) -> Result<OBStreamProfile, OBError> {
101+
let mut err_ptr = std::ptr::null_mut();
102102

103-
// let profile = unsafe {
104-
// orb::ob_stream_profile_list_get_profile(self.inner, index as c_int, &mut err_ptr)
105-
// };
106-
// OBError::consume(err_ptr)?;
103+
let profile = unsafe {
104+
orb::ob_stream_profile_list_get_profile(self.inner, index as i32, &mut err_ptr)
105+
};
106+
OBError::consume(err_ptr)?;
107107

108-
// Ok(OBStreamProfile::new(profile))
109-
// }
108+
Ok(OBStreamProfile::new(profile))
109+
}
110110

111111
/// Match the corresponding ob_stream_profile through the passed parameters. If there are multiple matches, the first one in the list will be returned by default.
112112
pub fn get_video_stream_profile(

0 commit comments

Comments
 (0)