Messages in this thread |  | | Date | Tue, 06 Aug 2024 09:27:29 +0000 | | From | Benno Lossin <> | | Subject | Re: [PATCH v8 5/6] rust: rbtree: add `RBTreeCursor` |
| |
On 06.08.24 11:04, Alice Ryhl wrote: > On Tue, Aug 6, 2024 at 11:01 AM Benno Lossin <benno.lossin@proton.me> wrote: >> >> On 06.08.24 10:24, Alice Ryhl wrote: >>> On Mon, Aug 5, 2024 at 9:35 PM Benno Lossin <benno.lossin@proton.me> wrote: >>>> On 27.07.24 22:30, Matt Gilbride wrote: >>>>> + tree: &'a mut RBTree<K, V>, >>>>> + current: NonNull<bindings::rb_node>, >>>>> +} >>>>> + >>>>> +// SAFETY: The [`RBTreeCursor`] gives out immutable references to K and mutable references to V, >>>>> +// so it has the same thread safety requirements as mutable references. >>>>> +unsafe impl<'a, K: Send, V: Send> Send for RBTreeCursor<'a, K, V> {} >>>> >>>> Again, do we want to use `K: Sync` here instead? >>> >>> In this case, `K: Send` and `K: Sync` are both sufficient conditions, >>> but `K: Send` will generally be less restrictive for the user. >> >> What if `K = struct(RefCell<i32>, i32)` where only the second i32 is >> used in `(Partial)Ord`? Then you can send `RBTreeCursor` to another >> thread and call `borrow` there, even though `K: !Sync` (and the value >> still lives on another thread). > > In that scenario, all immutable references to the key would be on the > same thread as the cursor. The cursor borrows the tree mutably, so > there can only be one cursor. > > When using `K: Send`, it's basically like having `RBTreeCursor` store > mutable references to the key, but with an API that downgrades to > immutable reference when giving out access to the key.
Ah that's true.
--- Cheers, Benno
|  |