use super::{ActorSnapshot, Timestamp};
use crate::rpc::ActorId;
use carla_sys::carla_rust::client::{FfiActorSnapshotList, FfiWorldSnapshot};
use cxx::UniquePtr;
use derivative::Derivative;
use static_assertions::assert_impl_all;
#[cfg_attr(
carla_version_0916,
doc = " See [carla.WorldSnapshot](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.WorldSnapshot) in the Python API."
)]
#[cfg_attr(
carla_version_0915,
doc = " See [carla.WorldSnapshot](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.WorldSnapshot) in the Python API."
)]
#[cfg_attr(
carla_version_0914,
doc = " See [carla.WorldSnapshot](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.WorldSnapshot) in the Python API."
)]
#[derive(Derivative)]
#[derivative(Debug)]
#[repr(transparent)]
pub struct WorldSnapshot {
#[derivative(Debug = "ignore")]
inner: UniquePtr<FfiWorldSnapshot>,
}
impl WorldSnapshot {
pub fn id(&self) -> u64 {
self.inner.GetId()
}
pub fn frame(&self) -> usize {
self.inner.GetFrame()
}
pub fn timestamp(&self) -> &Timestamp {
self.inner.GetTimestamp()
}
pub fn contains(&self, actor_id: ActorId) -> bool {
self.inner.Contains(actor_id.into())
}
pub fn find(&self, actor_id: ActorId) -> Option<ActorSnapshot> {
ActorSnapshot::from_cxx(self.inner.Find(actor_id.into()))
}
pub fn actor_snapshots(&self) -> ActorSnapshotIter {
ActorSnapshotIter {
list: self.inner.GetActorSnapshots(),
index: 0,
}
}
pub(crate) fn from_cxx(ptr: UniquePtr<FfiWorldSnapshot>) -> Option<Self> {
if ptr.is_null() {
None
} else {
Some(Self { inner: ptr })
}
}
}
pub struct ActorSnapshotIter {
list: UniquePtr<FfiActorSnapshotList>,
index: usize,
}
impl Iterator for ActorSnapshotIter {
type Item = ActorSnapshot;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.list.size() {
return None;
}
let snapshot = ActorSnapshot::from_cxx(self.list.get(self.index))?;
self.index += 1;
Some(snapshot)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.list.size().saturating_sub(self.index);
(remaining, Some(remaining))
}
}
impl ExactSizeIterator for ActorSnapshotIter {
fn len(&self) -> usize {
self.list.size().saturating_sub(self.index)
}
}
assert_impl_all!(WorldSnapshot: Send);