Detecting movement between closures

Is there an implementable trait for Rust that has a function that is called when the object is moved between closures?

Similar to the idea of Drop...

If I understand what you're asking, no, Rust does not invoke any user code when moving or copying a value. That is always just a plain memcpy.


One can create a trait for all T called Moveable (or whatever)

Then, one could go into the std:: or core:: and find the memcpy function (which externs to a C lib), and then prepend* (before the memcpy happens) a call into the trait function.

What are your thoughts on this? Of course, if this is done, the implementation ought to be absolutely non-blocking and maybe only a few operations at most

It's technically possible, but I think you would face philosophical opposition. It's a really nice property of Rust that moves and copies are always just memcpy. It makes this very predictable and amenable to optimization, without complicated rules like C++ copy elision.


Oh yeah, for sure. Small perturbations on the low-levels of a causal chain can lead to great and largely unpredictable results if not considered in the higher-levels. Chaos theory :slight_smile: :smiling_imp::smiling_imp:


Moves are not always a call to the memcopy. Sometimes they just .. you know .. copy the bytes. So you can't just replace the memcopy function.


Also, moves could be elided, as the stuff is just constructed in place rather than being moved around, in that case there would be no way to detect the move into the closure.


It's infeasible to introduce to current Rust, since there already exists tons of unsafe code relying the the fact that moves are just moving the bytes.


Of course, it's worth mentioning that you can stop a value being moved by using pin.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.