Re: [PATCH v11 2/2] rust: xarray: Add an abstraction for XArray

From: Andreas Hindborg
Date: Wed Dec 04 2024 - 08:04:44 EST


"Tamir Duberstein" <tamird@xxxxxxxxx> writes:

> `XArray` is an efficient sparse array of pointers. Add a Rust
> abstraction for this type.
>
> This implementation bounds the element type on `ForeignOwnable` and
> requires explicit locking for all operations. Future work may leverage
> RCU to enable lockless operation.
>
> Inspired-by: Maíra Canal <mcanal@xxxxxxxxxx>
> Inspired-by: Asahi Lina <lina@xxxxxxxxxxxxx>
> Signed-off-by: Tamir Duberstein <tamird@xxxxxxxxx>
> ---

[cut]

> +// SAFETY: It is safe to send `&XArray<T>` to another thread when the underlying `T` is `Sync`
> +// because it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it
> +// needs `T` to be `Send` because any thread that has a `&XArray<T>` may lock it and get a
> +// `Guard<T>` on that thread, so the thread may ultimately access `T` using a mutable reference, for
> +// example, using `get_mut` or `remove`.
> +unsafe impl<T: ForeignOwnable + Send + Sync> Sync for XArray<T> {}

Thanks, I think this is much more clear.


Reviewed-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>


Best regards,
Andreas Hindborg