Re: [PATCH v2 1/9] rust: list: add ListArc

From: Benno Lossin
Date: Mon May 27 2024 - 05:26:09 EST


On 06.05.24 11:53, Alice Ryhl wrote:
> +impl<T, const ID: u64> ListArc<T, ID>
> +where
> + T: ListArcSafe<ID> + ?Sized,
> +{
> + /// Convert a [`UniqueArc`] into a [`ListArc`].
> + #[inline]
> + pub fn from_unique(unique: UniqueArc<T>) -> Self {
> + Self::from_pin_unique(Pin::from(unique))
> + }
> +
> + /// Convert a pinned [`UniqueArc`] into a [`ListArc`].
> + #[inline]
> + pub fn from_pin_unique(mut unique: Pin<UniqueArc<T>>) -> Self {
> + // SAFETY: We have a `UniqueArc`, so there is no `ListArc`.
> + unsafe { T::on_create_list_arc_from_unique(unique.as_mut()) };
> + let arc = Arc::from(unique);
> + // SAFETY: We just called `on_create_list_arc_from_unique` on an arc without a `ListArc`,
> + // so we can create a `ListArc`.
> + unsafe { Self::transmute_from_arc(arc) }
> + }

I think these two functions would make sense as `From` impls.

> +
> + /// Like [`from_unique`], but creates two `ListArcs`.
> + ///
> + /// The two ids must be different.
> + ///
> + /// [`from_unique`]: ListArc::from_unique
> + #[inline]
> + pub fn pair_from_unique<const ID2: u64>(unique: UniqueArc<T>) -> (Self, ListArc<T, ID2>)
> + where
> + T: ListArcSafe<ID2>,
> + {
> + Self::pair_from_pin_unique(Pin::from(unique))
> + }

[...]

> + /// Returns a reference to an [`Arc`] from the given [`ListArc`].
> + ///
> + /// This is useful when the argument of a function call is an [`&Arc`] (e.g., in a method
> + /// receiver), but we have a [`ListArc`] instead.
> + ///
> + /// [`&Arc`]: Arc
> + #[inline]
> + pub fn as_arc(&self) -> &Arc<T> {
> + &self.arc
> + }

Should this be an `AsRef` impl instead?

---
Cheers,
Benno

> +
> + /// Returns an [`ArcBorrow`] from the given [`ListArc`].
> + ///
> + /// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method
> + /// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised.
> + #[inline]
> + pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
> + self.arc.as_arc_borrow()
> + }

[...]