Incorrect `Box::clone` doc

The current document is

a clone() of this box's contents.

But in reality, its behavior is specialized, if the content impl Copy, will not call clone.


What is our expected behavior, should we modify the document or remove specialization?

Incorrect Box::clone doc · Issue #112455 · rust-lang/rust (github.com)

This is relying on the semantics of RFC 1521, which led to this note in the Clone docs:

Types that are Copy should have a trivial implementation of Clone. More formally: if T: Copy, x: T, and y: &T, then let x = y.clone(); is equivalent to let x = *y;. Manual implementations should be careful to uphold this invariant; however, unsafe code must not rely on it to ensure memory safety.

So if you're doing anything in clone that is observably different than a Copy, then you are violating this expectation. That shouldn't cause any safety problems, as noted above, but you could see that dbg! statements in your clone aren't called if it was actually copied instead.

6 Likes

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