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
//! Defines the iterable list of stream profiles.

use crate::{
    error::{ErrorChecker, Result as RsResult},
    stream_profile::StreamProfile,
};
use std::{iter::FusedIterator, mem::MaybeUninit, os::raw::c_int, ptr::NonNull};

/// An iterable list of streams.
#[derive(Debug)]
pub struct StreamProfileList {
    ptr: NonNull<realsense_sys::rs2_stream_profile_list>,
}

impl StreamProfileList {
    /// Gets the stream profile at given index.
    ///
    /// The method returns error if the index is out of bound given by [StreamProfileList::len].
    pub fn get(&mut self, index: usize) -> RsResult<StreamProfile> {
        let profile = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_get_stream_profile(
                self.ptr.as_ptr(),
                index as c_int,
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            StreamProfile::from_parts(NonNull::new(ptr as *mut _).unwrap(), false)
        };
        Ok(profile)
    }

    /// Gets the length of list.
    pub fn len(&mut self) -> RsResult<usize> {
        unsafe {
            let mut checker = ErrorChecker::new();
            let len = realsense_sys::rs2_get_stream_profiles_count(
                self.ptr.as_ptr(),
                checker.inner_mut_ptr(),
            );
            checker.check()?;
            Ok(len as usize)
        }
    }

    /// Turns into iterable [StreamProfileListIntoIter] instance.
    pub fn try_into_iter(mut self) -> RsResult<StreamProfileListIntoIter> {
        let len = self.len()?;
        let ptr = unsafe { self.take() };
        let iter = StreamProfileListIntoIter {
            len,
            index: 0,
            ptr,
            fused: len == 0,
        };
        Ok(iter)
    }

    pub(crate) unsafe fn take(mut self) -> NonNull<realsense_sys::rs2_stream_profile_list> {
        let ptr = std::mem::replace(&mut self.ptr, { MaybeUninit::uninit().assume_init() });
        std::mem::forget(self);
        ptr
    }

    pub(crate) unsafe fn from_ptr(ptr: NonNull<realsense_sys::rs2_stream_profile_list>) -> Self {
        Self { ptr }
    }
}

impl IntoIterator for StreamProfileList {
    type Item = RsResult<StreamProfile>;
    type IntoIter = StreamProfileListIntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.try_into_iter().unwrap()
    }
}

impl Drop for StreamProfileList {
    fn drop(&mut self) {
        unsafe {
            realsense_sys::rs2_delete_stream_profiles_list(self.ptr.as_ptr());
        }
    }
}

#[derive(Debug)]
pub struct StreamProfileListIntoIter {
    len: usize,
    index: usize,
    ptr: NonNull<realsense_sys::rs2_stream_profile_list>,
    fused: bool,
}

impl Iterator for StreamProfileListIntoIter {
    type Item = RsResult<StreamProfile>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.fused {
            return None;
        }

        let ptr = unsafe {
            let mut checker = ErrorChecker::new();
            let ptr = realsense_sys::rs2_get_stream_profile(
                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 profile =
            unsafe { StreamProfile::from_parts(NonNull::new(ptr as *mut _).unwrap(), false) };
        Some(Ok(profile))
    }
}

impl FusedIterator for StreamProfileListIntoIter {}

unsafe impl Send for StreamProfileList {}

impl Drop for StreamProfileListIntoIter {
    fn drop(&mut self) {
        unsafe {
            realsense_sys::rs2_delete_stream_profiles_list(self.ptr.as_ptr());
        }
    }
}