[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230925150047.1961646-1-aliceryhl@google.com>
Date: Mon, 25 Sep 2023 15:00:47 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: boqun.feng@...il.com
Cc: a.hindborg@...sung.com, alex.gaynor@...il.com,
aliceryhl@...gle.com, benno.lossin@...ton.me,
bjorn3_gh@...tonmail.com, gary@...yguo.net,
linux-kernel@...r.kernel.org, ojeda@...nel.org,
rust-for-linux@...r.kernel.org, walmeida@...rosoft.com,
wedsonaf@...il.com
Subject: Re: [PATCH v2 2/2] rust: arc: remove `ArcBorrow` in favour of `WithRef`
>>> I'm concerned about this change, because an `&WithRef<T>` only has
>>> immutable permissions for the allocation. No pointer derived from it
>>> may be used to modify the value in the Arc, however, the drop
>>> implementation of Arc will do exactly that.
>>
>> That is indeed a problem. We could put the value in an `UnsafeCell`, but
>> that would lose us niche optimizations and probably also other optimizations.
>>
>
> Not sure I understand the problem here, why do we allow modifying the
> value in the Arc if you only have a shared ownership?
Well, usually it's when you have exclusive access even though the value
is in an `Arc`.
The main example of this is the destructor of the `Arc`. When the last
refcount drops to zero, this gives you exclusive access. This lets you
run the destructor. The destructor requires mutable access.
Another example would be converting the `Arc` back into an `UniqueArc`
by checking that the refcount is 1. Once you have a `UniqueArc`, you can
use it to mutate the inner value.
Finally, there are methods like `Arc::get_mut_unchecked`, where you
unsafely assert that nobody else is using the value while you are
modifying it. We don't have that in our version of `Arc` right now, but
we might want to add it later.
> Also I fail to see why `ArcBorrow` doesn't have the problem. Maybe I'm
> missing something subtle here? Could you provide an example?
It's because `ArcBorrow` just has a raw pointer inside it. Immutable
references give up write permissions, but raw pointers don't even if
they are `*const T`.
Alice
Powered by blists - more mailing lists