Re: [PATCH] rust: sync: lock: Add Lock::get_mut()
From: Guilherme Giacomo Simoes
Date: Mon Feb 03 2025 - 12:56:27 EST
Alice Ryhl <aliceryhl@xxxxxxxxxx> wrotes:
> I maintain my objection that this function cannot be correctly called.
> Yes, if you use dubious unsafe code, you can call it, but we shouldn't
> do that.
>
> At best, you could change this method to take `self: Pin<&mut Self>`.
Yes you is right, we should avoid unsafe code. But how the
`self: Pin<&mut Self` help us here?
The unsafe code at the get_mut() call place, is because of
`get_unchecked_mut`. You probably already know, but this is unsafe because you
need guarantee that the value is not move in memory. The `get_unchecked_mut`
return a &mut T, (in this case return a &mut Example), and we need a reference
to Example for access `d` field.
since we have access for `d` , we can get get_mut() without pinned `d`.
We need a way for get `Example.d` without `get_unchecked_mut`.
Thanks,
Guilherme