carla/client/
bounding_box_list.rs

1use crate::geom::BoundingBox;
2use carla_sys::carla_rust::{client::FfiBoundingBoxList, geom::FfiBoundingBox};
3use core::slice;
4use cxx::UniquePtr;
5use derivative::Derivative;
6use std::mem;
7
8/// A list of bounding boxes.
9#[derive(Derivative)]
10#[derivative(Debug)]
11#[repr(transparent)]
12pub struct BoundingBoxList {
13    #[derivative(Debug = "ignore")]
14    inner: UniquePtr<FfiBoundingBoxList>,
15}
16
17impl BoundingBoxList {
18    pub fn len(&self) -> usize {
19        self.inner.len()
20    }
21
22    #[must_use]
23    pub fn is_empty(&self) -> bool {
24        self.len() == 0
25    }
26
27    pub fn get(&self, index: usize) -> Option<BoundingBox<f32>> {
28        let orig = self.as_slice().get(index)?;
29        Some(BoundingBox::from_native(orig))
30    }
31
32    pub fn iter(&self) -> impl Iterator<Item = BoundingBox<f32>> + '_ {
33        self.as_slice().iter().map(BoundingBox::from_native)
34    }
35
36    fn as_slice(&self) -> &[FfiBoundingBox] {
37        unsafe {
38            let slice = slice::from_raw_parts(self.inner.data(), self.len());
39            mem::transmute(slice)
40        }
41    }
42
43    pub(crate) fn from_cxx(ptr: UniquePtr<FfiBoundingBoxList>) -> Option<Self> {
44        if ptr.is_null() {
45            None
46        } else {
47            Some(Self { inner: ptr })
48        }
49    }
50}