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
use std::marker::PhantomData;
use std::os::raw::c_void;
use crate::collision::Collision;
use crate::ffi;
use crate::handle::{Handle, HandleInner};
pub struct Handles<'a> {
pub(super) collision: *const ffi::NewtonCollision,
pub(super) next: *const c_void,
pub(super) get_next: Box<dyn Fn(*const ffi::NewtonCollision, *const c_void) -> *const c_void>,
pub(super) _phantom: PhantomData<&'a ()>,
}
pub struct Collisions<'a> {
pub(crate) handles: Handles<'a>,
pub(crate) get_col:
Box<dyn Fn(*const ffi::NewtonCollision, *const c_void) -> *const ffi::NewtonCollision>,
}
impl<'a> Iterator for Handles<'a> {
type Item = Handle;
fn next(&mut self) -> Option<Self::Item> {
let current = self.next;
if current.is_null() {
None
} else {
self.next = (self.get_next)(self.collision, current);
Some(Handle::from_ptr(current as _))
}
}
}
impl<'a> Iterator for Collisions<'a> {
type Item = Collision<'a>;
fn next(&mut self) -> Option<Self::Item> {
let collision = self.handles.collision;
self.handles.next().and_then(|h| unsafe {
match h.inner() {
HandleInner::Index(_) => panic!("Unexpected index handle."),
HandleInner::Pointer(ptr) => {
let col = (self.get_col)(collision, ptr as _);
Some(Collision::from_raw(col, false))
}
}
})
}
}