In Rust's Box2D bindings, the world struct has the following function:
pub fn set_contact_listener<L: ContactListener<U>>(&mut self, listener: Box<L>)
And then when bodies collide during the
step function, methods like
begin_contact will be called on the listener with information about the bodies that collided.
I would like to collect this information somewhere, but I'm not sure how to approach that. I can't just put it in a field of the struct I'm implementing ContactListener for, since I won't be able to read it after I call
set_contact_listener as that will take ownership of the listener object.
One way I can think of is to give my
ContactListener struct an
Rc<RefCell<Vec<Contact>>> field which it would then push contact info on, and my code could own the other end and read the messages. But this seems like quite an inelegant solution: it's weird to imagine that the library authors really meant for it to be used in that way. If
Rc<RefCell> was necessary, it would at least make sense to put it in the interface up-front.