Add abomonation instance for Arc<T>#35
Add abomonation instance for Arc<T>#35rklaehn wants to merge 2 commits intoTimelyDataflow:masterfrom
Conversation
| * pointer to an ArcInner that we need to dispose of without trying to run | ||
| * its destructor (which would panic). | ||
| */ | ||
| let (value, bytes) = decode::<T>(bytes)?; |
There was a problem hiding this comment.
@frankmcsherry Here value contains a reference to a T. Since T: 'static this structure cannot contain non-static references (but it could contain boxes). My understanding is that T can only directly reference bytes by unsafely reinterpreting a reference as a box or equivalently by transmuting a lifetime to 'static. Is my assumption correct that such behaviour violates the contract of Abomonation?
| */ | ||
| let (value, bytes) = decode::<T>(bytes)?; | ||
| // value is just a reference to the first part of old bytes, so move it into a new Arc | ||
| let arc = Arc::new(ptr::read(value)); |
There was a problem hiding this comment.
Here we lift the bytes from the underlying slice and move them into the ArcInner, i.e. we move the value of type T to a new memory location. Since it cannot reference bytes by the above argument, the result should be a correctly self-contained Arc<T>.
There was a problem hiding this comment.
I think this might be the problematic moment. Something like String can still reference bytes even after a ptr::read. The read will only grab the 24 bytes of the String type, but it may contain a pointer in to bytes (and very likely does, as this is how String implements Abomonation).
There was a problem hiding this comment.
Wow, that’s evil! So the only correct implementation would be to require T: Clone and clone the referenced value returned by decode.
|
Hello! Sorry for the late review. The thing that worries me here is that afaict the For example, in your test you could imagine decoding the Does that make sense to you, or should I try and modify the test to demonstrate this? |
If this approach turns out to be correct, I guess we could do the same for Rc...