Using ManuallyDrop to avoid copying

Instead of ManuallyDrop<Box<_>>, to afterwards never drop, you should use &'static _ or NonNull<_> to express the borrowing idea (forbidden without unsafe because Rust sees it as self-referential).

Here is an implementation using Pin to enforce the implicit invariant: the bytes on the heap will not move / be mutated as long as block is not dropped.

This is very related to this other thread, by the way, except for the fact that you have two borrows instead of one.

2 Likes