leaprs/
point_mapping.rs

1use std::ops::Deref;
2
3use leap_sys::LEAP_POINT_MAPPING;
4
5use crate::{sized_with_trailing_data::SizedWithTrailingData, LeapVectorRef};
6
7/// # Fields
8/// Available via dereference: [LEAP_POINT_MAPPING].
9pub struct PointMapping {
10    /// Store a boxed dynamic sized event
11    /// The size is only known at runtime
12    pub(crate) handle: Box<SizedWithTrailingData<LEAP_POINT_MAPPING>>,
13}
14
15impl Deref for PointMapping {
16    type Target = LEAP_POINT_MAPPING;
17
18    fn deref(&self) -> &Self::Target {
19        &self.handle.sized
20    }
21}
22
23impl PointMapping {
24    /// Allocate a LEAP_POINT_MAPPING with more data contiguous to it.
25    /// Unsafe: inner struct is uninitialized
26    pub(crate) unsafe fn new_uninitialized(point_mapping_size: u64) -> Self {
27        let trailing_size = point_mapping_size as usize - std::mem::size_of::<LEAP_POINT_MAPPING>();
28        Self {
29            handle: SizedWithTrailingData::allocate(std::mem::zeroed(), trailing_size),
30        }
31    }
32
33    #[doc = " The 3D points being mapped. @since 4.0.0"]
34    pub fn points(&self) -> Vec<LeapVectorRef> {
35        unsafe {
36            (0..self.handle.sized.nPoints as isize)
37                .map(|i| &*self.handle.sized.pPoints.offset(i))
38                .map(LeapVectorRef)
39                .collect()
40        }
41    }
42
43    #[doc = " The IDs of the 3D points being mapped. @since 4.0.0"]
44    pub fn pids(&self) -> &[u32] {
45        unsafe {
46            std::slice::from_raw_parts(self.handle.sized.pIDs, self.handle.sized.nPoints as usize)
47        }
48    }
49}