ros2_rust icon indicating copy to clipboard operation
ros2_rust copied to clipboard

Use Fn or FnMut for callbacks?

Open nnmm opened this issue 3 years ago • 2 comments

Services and Subscriptions should have the same trait bounds on their callbacks. It can be either Fn or FnMut. Fn callbacks can run concurrently with themselves without synchronization, but FnMut callbacks allow more operations inside the callback.

nnmm avatar Jul 05 '22 13:07 nnmm

I wouldn't characterize it as FnMut allowing more operations. You would just need to wrap your operations inside of some kind of Mutex and Cell or RefCell.

The FnMut is more convenient for the user in some specific scenarios, but the above techniques will probably be necessary as soon as callbacks are operating on shared data anyway.

mxgrey avatar Jul 05 '22 13:07 mxgrey

OK, so seems like the consensus is more towards Fn, correct @jhdcs @esteve? It's fine to move to Fn with me.

nnmm avatar Jul 12 '22 10:07 nnmm