16cd34171SAlice Ryhl // SPDX-License-Identifier: GPL-2.0 26cd34171SAlice Ryhl 36cd34171SAlice Ryhl // Copyright (C) 2024 Google LLC. 46cd34171SAlice Ryhl 56cd34171SAlice Ryhl //! A wrapper around `Arc` for linked lists. 66cd34171SAlice Ryhl 76cd34171SAlice Ryhl use crate::alloc::{AllocError, Flags}; 86cd34171SAlice Ryhl use crate::prelude::*; 96cd34171SAlice Ryhl use crate::sync::{Arc, ArcBorrow, UniqueArc}; 10*47cb6bf7SXiangfei Ding use core::marker::PhantomPinned; 116cd34171SAlice Ryhl use core::ops::Deref; 126cd34171SAlice Ryhl use core::pin::Pin; 13a4802631SAlice Ryhl use core::sync::atomic::{AtomicBool, Ordering}; 146cd34171SAlice Ryhl 156cd34171SAlice Ryhl /// Declares that this type has some way to ensure that there is exactly one `ListArc` instance for 166cd34171SAlice Ryhl /// this id. 176cd34171SAlice Ryhl /// 186cd34171SAlice Ryhl /// Types that implement this trait should include some kind of logic for keeping track of whether 196cd34171SAlice Ryhl /// a [`ListArc`] exists or not. We refer to this logic as "the tracking inside `T`". 206cd34171SAlice Ryhl /// 216cd34171SAlice Ryhl /// We allow the case where the tracking inside `T` thinks that a [`ListArc`] exists, but actually, 226cd34171SAlice Ryhl /// there isn't a [`ListArc`]. However, we do not allow the opposite situation where a [`ListArc`] 236cd34171SAlice Ryhl /// exists, but the tracking thinks it doesn't. This is because the former can at most result in us 246cd34171SAlice Ryhl /// failing to create a [`ListArc`] when the operation could succeed, whereas the latter can result 256cd34171SAlice Ryhl /// in the creation of two [`ListArc`] references. Only the latter situation can lead to memory 266cd34171SAlice Ryhl /// safety issues. 276cd34171SAlice Ryhl /// 286cd34171SAlice Ryhl /// A consequence of the above is that you may implement the tracking inside `T` by not actually 296cd34171SAlice Ryhl /// keeping track of anything. To do this, you always claim that a [`ListArc`] exists, even if 306cd34171SAlice Ryhl /// there isn't one. This implementation is allowed by the above rule, but it means that 316cd34171SAlice Ryhl /// [`ListArc`] references can only be created if you have ownership of *all* references to the 326cd34171SAlice Ryhl /// refcounted object, as you otherwise have no way of knowing whether a [`ListArc`] exists. 336cd34171SAlice Ryhl pub trait ListArcSafe<const ID: u64 = 0> { 346cd34171SAlice Ryhl /// Informs the tracking inside this type that it now has a [`ListArc`] reference. 356cd34171SAlice Ryhl /// 366cd34171SAlice Ryhl /// This method may be called even if the tracking inside this type thinks that a `ListArc` 376cd34171SAlice Ryhl /// reference exists. (But only if that's not actually the case.) 386cd34171SAlice Ryhl /// 396cd34171SAlice Ryhl /// # Safety 406cd34171SAlice Ryhl /// 416cd34171SAlice Ryhl /// Must not be called if a [`ListArc`] already exist for this value. on_create_list_arc_from_unique(self: Pin<&mut Self>)426cd34171SAlice Ryhl unsafe fn on_create_list_arc_from_unique(self: Pin<&mut Self>); 436cd34171SAlice Ryhl 446cd34171SAlice Ryhl /// Informs the tracking inside this type that there is no [`ListArc`] reference anymore. 456cd34171SAlice Ryhl /// 466cd34171SAlice Ryhl /// # Safety 476cd34171SAlice Ryhl /// 486cd34171SAlice Ryhl /// Must only be called if there is no [`ListArc`] reference, but the tracking thinks there is. on_drop_list_arc(&self)496cd34171SAlice Ryhl unsafe fn on_drop_list_arc(&self); 506cd34171SAlice Ryhl } 516cd34171SAlice Ryhl 52a4802631SAlice Ryhl /// Declares that this type is able to safely attempt to create `ListArc`s at any time. 53a4802631SAlice Ryhl /// 54a4802631SAlice Ryhl /// # Safety 55a4802631SAlice Ryhl /// 56a4802631SAlice Ryhl /// The guarantees of `try_new_list_arc` must be upheld. 57a4802631SAlice Ryhl pub unsafe trait TryNewListArc<const ID: u64 = 0>: ListArcSafe<ID> { 58a4802631SAlice Ryhl /// Attempts to convert an `Arc<Self>` into an `ListArc<Self>`. Returns `true` if the 59a4802631SAlice Ryhl /// conversion was successful. 60a4802631SAlice Ryhl /// 61a4802631SAlice Ryhl /// This method should not be called directly. Use [`ListArc::try_from_arc`] instead. 62a4802631SAlice Ryhl /// 63a4802631SAlice Ryhl /// # Guarantees 64a4802631SAlice Ryhl /// 65a4802631SAlice Ryhl /// If this call returns `true`, then there is no [`ListArc`] pointing to this value. 66a4802631SAlice Ryhl /// Additionally, this call will have transitioned the tracking inside `Self` from not thinking 67a4802631SAlice Ryhl /// that a [`ListArc`] exists, to thinking that a [`ListArc`] exists. try_new_list_arc(&self) -> bool68a4802631SAlice Ryhl fn try_new_list_arc(&self) -> bool; 69a4802631SAlice Ryhl } 70a4802631SAlice Ryhl 716cd34171SAlice Ryhl /// Declares that this type supports [`ListArc`]. 726cd34171SAlice Ryhl /// 73a4802631SAlice Ryhl /// This macro supports a few different strategies for implementing the tracking inside the type: 74a4802631SAlice Ryhl /// 75a4802631SAlice Ryhl /// * The `untracked` strategy does not actually keep track of whether a [`ListArc`] exists. When 76a4802631SAlice Ryhl /// using this strategy, the only way to create a [`ListArc`] is using a [`UniqueArc`]. 77a4802631SAlice Ryhl /// * The `tracked_by` strategy defers the tracking to a field of the struct. The user much specify 78a4802631SAlice Ryhl /// which field to defer the tracking to. The field must implement [`ListArcSafe`]. If the field 79a4802631SAlice Ryhl /// implements [`TryNewListArc`], then the type will also implement [`TryNewListArc`]. 80a4802631SAlice Ryhl /// 81a4802631SAlice Ryhl /// The `tracked_by` strategy is usually used by deferring to a field of type 82a4802631SAlice Ryhl /// [`AtomicTracker`]. However, it is also possible to defer the tracking to another struct 83a4802631SAlice Ryhl /// using also using this macro. 846cd34171SAlice Ryhl #[macro_export] 856cd34171SAlice Ryhl macro_rules! impl_list_arc_safe { 866cd34171SAlice Ryhl (impl$({$($generics:tt)*})? ListArcSafe<$num:tt> for $t:ty { untracked; } $($rest:tt)*) => { 876cd34171SAlice Ryhl impl$(<$($generics)*>)? $crate::list::ListArcSafe<$num> for $t { 886cd34171SAlice Ryhl unsafe fn on_create_list_arc_from_unique(self: ::core::pin::Pin<&mut Self>) {} 896cd34171SAlice Ryhl unsafe fn on_drop_list_arc(&self) {} 906cd34171SAlice Ryhl } 916cd34171SAlice Ryhl $crate::list::impl_list_arc_safe! { $($rest)* } 926cd34171SAlice Ryhl }; 936cd34171SAlice Ryhl 94a4802631SAlice Ryhl (impl$({$($generics:tt)*})? ListArcSafe<$num:tt> for $t:ty { 95a4802631SAlice Ryhl tracked_by $field:ident : $fty:ty; 96a4802631SAlice Ryhl } $($rest:tt)*) => { 97a4802631SAlice Ryhl impl$(<$($generics)*>)? $crate::list::ListArcSafe<$num> for $t { 98a4802631SAlice Ryhl unsafe fn on_create_list_arc_from_unique(self: ::core::pin::Pin<&mut Self>) { 99a4802631SAlice Ryhl $crate::assert_pinned!($t, $field, $fty, inline); 100a4802631SAlice Ryhl 101a4802631SAlice Ryhl // SAFETY: This field is structurally pinned as per the above assertion. 102a4802631SAlice Ryhl let field = unsafe { 103a4802631SAlice Ryhl ::core::pin::Pin::map_unchecked_mut(self, |me| &mut me.$field) 104a4802631SAlice Ryhl }; 105a4802631SAlice Ryhl // SAFETY: The caller promises that there is no `ListArc`. 106a4802631SAlice Ryhl unsafe { 107a4802631SAlice Ryhl <$fty as $crate::list::ListArcSafe<$num>>::on_create_list_arc_from_unique(field) 108a4802631SAlice Ryhl }; 109a4802631SAlice Ryhl } 110a4802631SAlice Ryhl unsafe fn on_drop_list_arc(&self) { 111a4802631SAlice Ryhl // SAFETY: The caller promises that there is no `ListArc` reference, and also 112a4802631SAlice Ryhl // promises that the tracking thinks there is a `ListArc` reference. 113a4802631SAlice Ryhl unsafe { <$fty as $crate::list::ListArcSafe<$num>>::on_drop_list_arc(&self.$field) }; 114a4802631SAlice Ryhl } 115a4802631SAlice Ryhl } 116a4802631SAlice Ryhl unsafe impl$(<$($generics)*>)? $crate::list::TryNewListArc<$num> for $t 117a4802631SAlice Ryhl where 118a4802631SAlice Ryhl $fty: TryNewListArc<$num>, 119a4802631SAlice Ryhl { 120a4802631SAlice Ryhl fn try_new_list_arc(&self) -> bool { 121a4802631SAlice Ryhl <$fty as $crate::list::TryNewListArc<$num>>::try_new_list_arc(&self.$field) 122a4802631SAlice Ryhl } 123a4802631SAlice Ryhl } 124a4802631SAlice Ryhl $crate::list::impl_list_arc_safe! { $($rest)* } 125a4802631SAlice Ryhl }; 126a4802631SAlice Ryhl 1276cd34171SAlice Ryhl () => {}; 1286cd34171SAlice Ryhl } 1296cd34171SAlice Ryhl pub use impl_list_arc_safe; 1306cd34171SAlice Ryhl 1316cd34171SAlice Ryhl /// A wrapper around [`Arc`] that's guaranteed unique for the given id. 1326cd34171SAlice Ryhl /// 1336cd34171SAlice Ryhl /// The `ListArc` type can be thought of as a special reference to a refcounted object that owns the 1346cd34171SAlice Ryhl /// permission to manipulate the `next`/`prev` pointers stored in the refcounted object. By ensuring 1356cd34171SAlice Ryhl /// that each object has only one `ListArc` reference, the owner of that reference is assured 136db841866SAlice Ryhl /// exclusive access to the `next`/`prev` pointers. When a `ListArc` is inserted into a [`List`], 137db841866SAlice Ryhl /// the [`List`] takes ownership of the `ListArc` reference. 1386cd34171SAlice Ryhl /// 1396cd34171SAlice Ryhl /// There are various strategies to ensuring that a value has only one `ListArc` reference. The 1406cd34171SAlice Ryhl /// simplest is to convert a [`UniqueArc`] into a `ListArc`. However, the refcounted object could 1416cd34171SAlice Ryhl /// also keep track of whether a `ListArc` exists using a boolean, which could allow for the 1426cd34171SAlice Ryhl /// creation of new `ListArc` references from an [`Arc`] reference. Whatever strategy is used, the 1436cd34171SAlice Ryhl /// relevant tracking is referred to as "the tracking inside `T`", and the [`ListArcSafe`] trait 1446cd34171SAlice Ryhl /// (and its subtraits) are used to update the tracking when a `ListArc` is created or destroyed. 1456cd34171SAlice Ryhl /// 1466cd34171SAlice Ryhl /// Note that we allow the case where the tracking inside `T` thinks that a `ListArc` exists, but 1476cd34171SAlice Ryhl /// actually, there isn't a `ListArc`. However, we do not allow the opposite situation where a 1486cd34171SAlice Ryhl /// `ListArc` exists, but the tracking thinks it doesn't. This is because the former can at most 1496cd34171SAlice Ryhl /// result in us failing to create a `ListArc` when the operation could succeed, whereas the latter 1506cd34171SAlice Ryhl /// can result in the creation of two `ListArc` references. 1516cd34171SAlice Ryhl /// 1526cd34171SAlice Ryhl /// While this `ListArc` is unique for the given id, there still might exist normal `Arc` 1536cd34171SAlice Ryhl /// references to the object. 1546cd34171SAlice Ryhl /// 1556cd34171SAlice Ryhl /// # Invariants 1566cd34171SAlice Ryhl /// 1576cd34171SAlice Ryhl /// * Each reference counted object has at most one `ListArc` for each value of `ID`. 1586cd34171SAlice Ryhl /// * The tracking inside `T` is aware that a `ListArc` reference exists. 159db841866SAlice Ryhl /// 160db841866SAlice Ryhl /// [`List`]: crate::list::List 1616cd34171SAlice Ryhl #[repr(transparent)] 162*47cb6bf7SXiangfei Ding #[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, derive(core::marker::CoercePointee))] 1636cd34171SAlice Ryhl pub struct ListArc<T, const ID: u64 = 0> 1646cd34171SAlice Ryhl where 1656cd34171SAlice Ryhl T: ListArcSafe<ID> + ?Sized, 1666cd34171SAlice Ryhl { 1676cd34171SAlice Ryhl arc: Arc<T>, 1686cd34171SAlice Ryhl } 1696cd34171SAlice Ryhl 1706cd34171SAlice Ryhl impl<T: ListArcSafe<ID>, const ID: u64> ListArc<T, ID> { 1716cd34171SAlice Ryhl /// Constructs a new reference counted instance of `T`. 1726cd34171SAlice Ryhl #[inline] new(contents: T, flags: Flags) -> Result<Self, AllocError>1736cd34171SAlice Ryhl pub fn new(contents: T, flags: Flags) -> Result<Self, AllocError> { 1746cd34171SAlice Ryhl Ok(Self::from(UniqueArc::new(contents, flags)?)) 1756cd34171SAlice Ryhl } 1766cd34171SAlice Ryhl 1776cd34171SAlice Ryhl /// Use the given initializer to in-place initialize a `T`. 1786cd34171SAlice Ryhl /// 1796cd34171SAlice Ryhl /// If `T: !Unpin` it will not be able to move afterwards. 1806cd34171SAlice Ryhl // We don't implement `InPlaceInit` because `ListArc` is implicitly pinned. This is similar to 1816cd34171SAlice Ryhl // what we do for `Arc`. 1826cd34171SAlice Ryhl #[inline] pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self, E> where E: From<AllocError>,1836cd34171SAlice Ryhl pub fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self, E> 1846cd34171SAlice Ryhl where 1856cd34171SAlice Ryhl E: From<AllocError>, 1866cd34171SAlice Ryhl { 1876cd34171SAlice Ryhl Ok(Self::from(UniqueArc::try_pin_init(init, flags)?)) 1886cd34171SAlice Ryhl } 1896cd34171SAlice Ryhl 1906cd34171SAlice Ryhl /// Use the given initializer to in-place initialize a `T`. 1916cd34171SAlice Ryhl /// 1926cd34171SAlice Ryhl /// This is equivalent to [`ListArc<T>::pin_init`], since a [`ListArc`] is always pinned. 1936cd34171SAlice Ryhl #[inline] init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E> where E: From<AllocError>,1946cd34171SAlice Ryhl pub fn init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E> 1956cd34171SAlice Ryhl where 1966cd34171SAlice Ryhl E: From<AllocError>, 1976cd34171SAlice Ryhl { 1986cd34171SAlice Ryhl Ok(Self::from(UniqueArc::try_init(init, flags)?)) 1996cd34171SAlice Ryhl } 2006cd34171SAlice Ryhl } 2016cd34171SAlice Ryhl 2026cd34171SAlice Ryhl impl<T, const ID: u64> From<UniqueArc<T>> for ListArc<T, ID> 2036cd34171SAlice Ryhl where 2046cd34171SAlice Ryhl T: ListArcSafe<ID> + ?Sized, 2056cd34171SAlice Ryhl { 2066cd34171SAlice Ryhl /// Convert a [`UniqueArc`] into a [`ListArc`]. 2076cd34171SAlice Ryhl #[inline] from(unique: UniqueArc<T>) -> Self2086cd34171SAlice Ryhl fn from(unique: UniqueArc<T>) -> Self { 2096cd34171SAlice Ryhl Self::from(Pin::from(unique)) 2106cd34171SAlice Ryhl } 2116cd34171SAlice Ryhl } 2126cd34171SAlice Ryhl 2136cd34171SAlice Ryhl impl<T, const ID: u64> From<Pin<UniqueArc<T>>> for ListArc<T, ID> 2146cd34171SAlice Ryhl where 2156cd34171SAlice Ryhl T: ListArcSafe<ID> + ?Sized, 2166cd34171SAlice Ryhl { 2176cd34171SAlice Ryhl /// Convert a pinned [`UniqueArc`] into a [`ListArc`]. 2186cd34171SAlice Ryhl #[inline] from(mut unique: Pin<UniqueArc<T>>) -> Self2196cd34171SAlice Ryhl fn from(mut unique: Pin<UniqueArc<T>>) -> Self { 2206cd34171SAlice Ryhl // SAFETY: We have a `UniqueArc`, so there is no `ListArc`. 2216cd34171SAlice Ryhl unsafe { T::on_create_list_arc_from_unique(unique.as_mut()) }; 2226cd34171SAlice Ryhl let arc = Arc::from(unique); 2236cd34171SAlice Ryhl // SAFETY: We just called `on_create_list_arc_from_unique` on an arc without a `ListArc`, 2246cd34171SAlice Ryhl // so we can create a `ListArc`. 2256cd34171SAlice Ryhl unsafe { Self::transmute_from_arc(arc) } 2266cd34171SAlice Ryhl } 2276cd34171SAlice Ryhl } 2286cd34171SAlice Ryhl 2296cd34171SAlice Ryhl impl<T, const ID: u64> ListArc<T, ID> 2306cd34171SAlice Ryhl where 2316cd34171SAlice Ryhl T: ListArcSafe<ID> + ?Sized, 2326cd34171SAlice Ryhl { 2336cd34171SAlice Ryhl /// Creates two `ListArc`s from a [`UniqueArc`]. 2346cd34171SAlice Ryhl /// 2356cd34171SAlice Ryhl /// The two ids must be different. 2366cd34171SAlice Ryhl #[inline] pair_from_unique<const ID2: u64>(unique: UniqueArc<T>) -> (Self, ListArc<T, ID2>) where T: ListArcSafe<ID2>,2376cd34171SAlice Ryhl pub fn pair_from_unique<const ID2: u64>(unique: UniqueArc<T>) -> (Self, ListArc<T, ID2>) 2386cd34171SAlice Ryhl where 2396cd34171SAlice Ryhl T: ListArcSafe<ID2>, 2406cd34171SAlice Ryhl { 2416cd34171SAlice Ryhl Self::pair_from_pin_unique(Pin::from(unique)) 2426cd34171SAlice Ryhl } 2436cd34171SAlice Ryhl 2446cd34171SAlice Ryhl /// Creates two `ListArc`s from a pinned [`UniqueArc`]. 2456cd34171SAlice Ryhl /// 2466cd34171SAlice Ryhl /// The two ids must be different. 2476cd34171SAlice Ryhl #[inline] pair_from_pin_unique<const ID2: u64>( mut unique: Pin<UniqueArc<T>>, ) -> (Self, ListArc<T, ID2>) where T: ListArcSafe<ID2>,2486cd34171SAlice Ryhl pub fn pair_from_pin_unique<const ID2: u64>( 2496cd34171SAlice Ryhl mut unique: Pin<UniqueArc<T>>, 2506cd34171SAlice Ryhl ) -> (Self, ListArc<T, ID2>) 2516cd34171SAlice Ryhl where 2526cd34171SAlice Ryhl T: ListArcSafe<ID2>, 2536cd34171SAlice Ryhl { 2546cd34171SAlice Ryhl build_assert!(ID != ID2); 2556cd34171SAlice Ryhl 2566cd34171SAlice Ryhl // SAFETY: We have a `UniqueArc`, so there is no `ListArc`. 2576cd34171SAlice Ryhl unsafe { <T as ListArcSafe<ID>>::on_create_list_arc_from_unique(unique.as_mut()) }; 2586cd34171SAlice Ryhl // SAFETY: We have a `UniqueArc`, so there is no `ListArc`. 2596cd34171SAlice Ryhl unsafe { <T as ListArcSafe<ID2>>::on_create_list_arc_from_unique(unique.as_mut()) }; 2606cd34171SAlice Ryhl 2616cd34171SAlice Ryhl let arc1 = Arc::from(unique); 2626cd34171SAlice Ryhl let arc2 = Arc::clone(&arc1); 2636cd34171SAlice Ryhl 2646cd34171SAlice Ryhl // SAFETY: We just called `on_create_list_arc_from_unique` on an arc without a `ListArc` 2656cd34171SAlice Ryhl // for both IDs (which are different), so we can create two `ListArc`s. 2666cd34171SAlice Ryhl unsafe { 2676cd34171SAlice Ryhl ( 2686cd34171SAlice Ryhl Self::transmute_from_arc(arc1), 2696cd34171SAlice Ryhl ListArc::transmute_from_arc(arc2), 2706cd34171SAlice Ryhl ) 2716cd34171SAlice Ryhl } 2726cd34171SAlice Ryhl } 2736cd34171SAlice Ryhl 274a4802631SAlice Ryhl /// Try to create a new `ListArc`. 275a4802631SAlice Ryhl /// 276a4802631SAlice Ryhl /// This fails if this value already has a `ListArc`. try_from_arc(arc: Arc<T>) -> Result<Self, Arc<T>> where T: TryNewListArc<ID>,277a4802631SAlice Ryhl pub fn try_from_arc(arc: Arc<T>) -> Result<Self, Arc<T>> 278a4802631SAlice Ryhl where 279a4802631SAlice Ryhl T: TryNewListArc<ID>, 280a4802631SAlice Ryhl { 281a4802631SAlice Ryhl if arc.try_new_list_arc() { 282a4802631SAlice Ryhl // SAFETY: The `try_new_list_arc` method returned true, so we made the tracking think 283a4802631SAlice Ryhl // that a `ListArc` exists. This lets us create a `ListArc`. 284a4802631SAlice Ryhl Ok(unsafe { Self::transmute_from_arc(arc) }) 285a4802631SAlice Ryhl } else { 286a4802631SAlice Ryhl Err(arc) 287a4802631SAlice Ryhl } 288a4802631SAlice Ryhl } 289a4802631SAlice Ryhl 290a4802631SAlice Ryhl /// Try to create a new `ListArc`. 291a4802631SAlice Ryhl /// 292a4802631SAlice Ryhl /// This fails if this value already has a `ListArc`. try_from_arc_borrow(arc: ArcBorrow<'_, T>) -> Option<Self> where T: TryNewListArc<ID>,293a4802631SAlice Ryhl pub fn try_from_arc_borrow(arc: ArcBorrow<'_, T>) -> Option<Self> 294a4802631SAlice Ryhl where 295a4802631SAlice Ryhl T: TryNewListArc<ID>, 296a4802631SAlice Ryhl { 297a4802631SAlice Ryhl if arc.try_new_list_arc() { 298a4802631SAlice Ryhl // SAFETY: The `try_new_list_arc` method returned true, so we made the tracking think 299a4802631SAlice Ryhl // that a `ListArc` exists. This lets us create a `ListArc`. 300a4802631SAlice Ryhl Some(unsafe { Self::transmute_from_arc(Arc::from(arc)) }) 301a4802631SAlice Ryhl } else { 302a4802631SAlice Ryhl None 303a4802631SAlice Ryhl } 304a4802631SAlice Ryhl } 305a4802631SAlice Ryhl 306a4802631SAlice Ryhl /// Try to create a new `ListArc`. 307a4802631SAlice Ryhl /// 308a4802631SAlice Ryhl /// If it's not possible to create a new `ListArc`, then the `Arc` is dropped. This will never 309a4802631SAlice Ryhl /// run the destructor of the value. try_from_arc_or_drop(arc: Arc<T>) -> Option<Self> where T: TryNewListArc<ID>,310a4802631SAlice Ryhl pub fn try_from_arc_or_drop(arc: Arc<T>) -> Option<Self> 311a4802631SAlice Ryhl where 312a4802631SAlice Ryhl T: TryNewListArc<ID>, 313a4802631SAlice Ryhl { 314a4802631SAlice Ryhl match Self::try_from_arc(arc) { 315a4802631SAlice Ryhl Ok(list_arc) => Some(list_arc), 316a4802631SAlice Ryhl Err(arc) => Arc::into_unique_or_drop(arc).map(Self::from), 317a4802631SAlice Ryhl } 318a4802631SAlice Ryhl } 319a4802631SAlice Ryhl 3206cd34171SAlice Ryhl /// Transmutes an [`Arc`] into a `ListArc` without updating the tracking inside `T`. 3216cd34171SAlice Ryhl /// 3226cd34171SAlice Ryhl /// # Safety 3236cd34171SAlice Ryhl /// 3246cd34171SAlice Ryhl /// * The value must not already have a `ListArc` reference. 3256cd34171SAlice Ryhl /// * The tracking inside `T` must think that there is a `ListArc` reference. 3266cd34171SAlice Ryhl #[inline] transmute_from_arc(arc: Arc<T>) -> Self3276cd34171SAlice Ryhl unsafe fn transmute_from_arc(arc: Arc<T>) -> Self { 3286cd34171SAlice Ryhl // INVARIANT: By the safety requirements, the invariants on `ListArc` are satisfied. 3296cd34171SAlice Ryhl Self { arc } 3306cd34171SAlice Ryhl } 3316cd34171SAlice Ryhl 3326cd34171SAlice Ryhl /// Transmutes a `ListArc` into an [`Arc`] without updating the tracking inside `T`. 3336cd34171SAlice Ryhl /// 3346cd34171SAlice Ryhl /// After this call, the tracking inside `T` will still think that there is a `ListArc` 3356cd34171SAlice Ryhl /// reference. 3366cd34171SAlice Ryhl #[inline] transmute_to_arc(self) -> Arc<T>3376cd34171SAlice Ryhl fn transmute_to_arc(self) -> Arc<T> { 3386cd34171SAlice Ryhl // Use a transmute to skip destructor. 3396cd34171SAlice Ryhl // 3406cd34171SAlice Ryhl // SAFETY: ListArc is repr(transparent). 3416cd34171SAlice Ryhl unsafe { core::mem::transmute(self) } 3426cd34171SAlice Ryhl } 3436cd34171SAlice Ryhl 3446cd34171SAlice Ryhl /// Convert ownership of this `ListArc` into a raw pointer. 3456cd34171SAlice Ryhl /// 3466cd34171SAlice Ryhl /// The returned pointer is indistinguishable from pointers returned by [`Arc::into_raw`]. The 3476cd34171SAlice Ryhl /// tracking inside `T` will still think that a `ListArc` exists after this call. 3486cd34171SAlice Ryhl #[inline] into_raw(self) -> *const T3496cd34171SAlice Ryhl pub fn into_raw(self) -> *const T { 3506cd34171SAlice Ryhl Arc::into_raw(Self::transmute_to_arc(self)) 3516cd34171SAlice Ryhl } 3526cd34171SAlice Ryhl 3536cd34171SAlice Ryhl /// Take ownership of the `ListArc` from a raw pointer. 3546cd34171SAlice Ryhl /// 3556cd34171SAlice Ryhl /// # Safety 3566cd34171SAlice Ryhl /// 3576cd34171SAlice Ryhl /// * `ptr` must satisfy the safety requirements of [`Arc::from_raw`]. 3586cd34171SAlice Ryhl /// * The value must not already have a `ListArc` reference. 3596cd34171SAlice Ryhl /// * The tracking inside `T` must think that there is a `ListArc` reference. 3606cd34171SAlice Ryhl #[inline] from_raw(ptr: *const T) -> Self3616cd34171SAlice Ryhl pub unsafe fn from_raw(ptr: *const T) -> Self { 3626cd34171SAlice Ryhl // SAFETY: The pointer satisfies the safety requirements for `Arc::from_raw`. 3636cd34171SAlice Ryhl let arc = unsafe { Arc::from_raw(ptr) }; 3646cd34171SAlice Ryhl // SAFETY: The value doesn't already have a `ListArc` reference, but the tracking thinks it 3656cd34171SAlice Ryhl // does. 3666cd34171SAlice Ryhl unsafe { Self::transmute_from_arc(arc) } 3676cd34171SAlice Ryhl } 3686cd34171SAlice Ryhl 3696cd34171SAlice Ryhl /// Converts the `ListArc` into an [`Arc`]. 3706cd34171SAlice Ryhl #[inline] into_arc(self) -> Arc<T>3716cd34171SAlice Ryhl pub fn into_arc(self) -> Arc<T> { 3726cd34171SAlice Ryhl let arc = Self::transmute_to_arc(self); 3736cd34171SAlice Ryhl // SAFETY: There is no longer a `ListArc`, but the tracking thinks there is. 3746cd34171SAlice Ryhl unsafe { T::on_drop_list_arc(&arc) }; 3756cd34171SAlice Ryhl arc 3766cd34171SAlice Ryhl } 3776cd34171SAlice Ryhl 3786cd34171SAlice Ryhl /// Clone a `ListArc` into an [`Arc`]. 3796cd34171SAlice Ryhl #[inline] clone_arc(&self) -> Arc<T>3806cd34171SAlice Ryhl pub fn clone_arc(&self) -> Arc<T> { 3816cd34171SAlice Ryhl self.arc.clone() 3826cd34171SAlice Ryhl } 3836cd34171SAlice Ryhl 3846cd34171SAlice Ryhl /// Returns a reference to an [`Arc`] from the given [`ListArc`]. 3856cd34171SAlice Ryhl /// 3866cd34171SAlice Ryhl /// This is useful when the argument of a function call is an [`&Arc`] (e.g., in a method 3876cd34171SAlice Ryhl /// receiver), but we have a [`ListArc`] instead. 3886cd34171SAlice Ryhl /// 3896cd34171SAlice Ryhl /// [`&Arc`]: Arc 3906cd34171SAlice Ryhl #[inline] as_arc(&self) -> &Arc<T>3916cd34171SAlice Ryhl pub fn as_arc(&self) -> &Arc<T> { 3926cd34171SAlice Ryhl &self.arc 3936cd34171SAlice Ryhl } 3946cd34171SAlice Ryhl 3956cd34171SAlice Ryhl /// Returns an [`ArcBorrow`] from the given [`ListArc`]. 3966cd34171SAlice Ryhl /// 3976cd34171SAlice Ryhl /// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method 3986cd34171SAlice Ryhl /// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised. 3996cd34171SAlice Ryhl #[inline] as_arc_borrow(&self) -> ArcBorrow<'_, T>4006cd34171SAlice Ryhl pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> { 4016cd34171SAlice Ryhl self.arc.as_arc_borrow() 4026cd34171SAlice Ryhl } 4036cd34171SAlice Ryhl 4046cd34171SAlice Ryhl /// Compare whether two [`ListArc`] pointers reference the same underlying object. 4056cd34171SAlice Ryhl #[inline] ptr_eq(this: &Self, other: &Self) -> bool4066cd34171SAlice Ryhl pub fn ptr_eq(this: &Self, other: &Self) -> bool { 4076cd34171SAlice Ryhl Arc::ptr_eq(&this.arc, &other.arc) 4086cd34171SAlice Ryhl } 4096cd34171SAlice Ryhl } 4106cd34171SAlice Ryhl 4116cd34171SAlice Ryhl impl<T, const ID: u64> Deref for ListArc<T, ID> 4126cd34171SAlice Ryhl where 4136cd34171SAlice Ryhl T: ListArcSafe<ID> + ?Sized, 4146cd34171SAlice Ryhl { 4156cd34171SAlice Ryhl type Target = T; 4166cd34171SAlice Ryhl 4176cd34171SAlice Ryhl #[inline] deref(&self) -> &Self::Target4186cd34171SAlice Ryhl fn deref(&self) -> &Self::Target { 4196cd34171SAlice Ryhl self.arc.deref() 4206cd34171SAlice Ryhl } 4216cd34171SAlice Ryhl } 4226cd34171SAlice Ryhl 4236cd34171SAlice Ryhl impl<T, const ID: u64> Drop for ListArc<T, ID> 4246cd34171SAlice Ryhl where 4256cd34171SAlice Ryhl T: ListArcSafe<ID> + ?Sized, 4266cd34171SAlice Ryhl { 4276cd34171SAlice Ryhl #[inline] drop(&mut self)4286cd34171SAlice Ryhl fn drop(&mut self) { 4296cd34171SAlice Ryhl // SAFETY: There is no longer a `ListArc`, but the tracking thinks there is by the type 4306cd34171SAlice Ryhl // invariants on `Self`. 4316cd34171SAlice Ryhl unsafe { T::on_drop_list_arc(&self.arc) }; 4326cd34171SAlice Ryhl } 4336cd34171SAlice Ryhl } 4346cd34171SAlice Ryhl 4356cd34171SAlice Ryhl impl<T, const ID: u64> AsRef<Arc<T>> for ListArc<T, ID> 4366cd34171SAlice Ryhl where 4376cd34171SAlice Ryhl T: ListArcSafe<ID> + ?Sized, 4386cd34171SAlice Ryhl { 4396cd34171SAlice Ryhl #[inline] as_ref(&self) -> &Arc<T>4406cd34171SAlice Ryhl fn as_ref(&self) -> &Arc<T> { 4416cd34171SAlice Ryhl self.as_arc() 4426cd34171SAlice Ryhl } 4436cd34171SAlice Ryhl } 4446cd34171SAlice Ryhl 4456cd34171SAlice Ryhl // This is to allow coercion from `ListArc<T>` to `ListArc<U>` if `T` can be converted to the 4466cd34171SAlice Ryhl // dynamically-sized type (DST) `U`. 447*47cb6bf7SXiangfei Ding #[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))] 4486cd34171SAlice Ryhl impl<T, U, const ID: u64> core::ops::CoerceUnsized<ListArc<U, ID>> for ListArc<T, ID> 4496cd34171SAlice Ryhl where 450*47cb6bf7SXiangfei Ding T: ListArcSafe<ID> + core::marker::Unsize<U> + ?Sized, 4516cd34171SAlice Ryhl U: ListArcSafe<ID> + ?Sized, 4526cd34171SAlice Ryhl { 4536cd34171SAlice Ryhl } 4546cd34171SAlice Ryhl 4556cd34171SAlice Ryhl // This is to allow `ListArc<U>` to be dispatched on when `ListArc<T>` can be coerced into 4566cd34171SAlice Ryhl // `ListArc<U>`. 457*47cb6bf7SXiangfei Ding #[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))] 4586cd34171SAlice Ryhl impl<T, U, const ID: u64> core::ops::DispatchFromDyn<ListArc<U, ID>> for ListArc<T, ID> 4596cd34171SAlice Ryhl where 460*47cb6bf7SXiangfei Ding T: ListArcSafe<ID> + core::marker::Unsize<U> + ?Sized, 4616cd34171SAlice Ryhl U: ListArcSafe<ID> + ?Sized, 4626cd34171SAlice Ryhl { 4636cd34171SAlice Ryhl } 464a4802631SAlice Ryhl 465a4802631SAlice Ryhl /// A utility for tracking whether a [`ListArc`] exists using an atomic. 466a4802631SAlice Ryhl /// 467a4802631SAlice Ryhl /// # Invariant 468a4802631SAlice Ryhl /// 469a4802631SAlice Ryhl /// If the boolean is `false`, then there is no [`ListArc`] for this value. 470a4802631SAlice Ryhl #[repr(transparent)] 471a4802631SAlice Ryhl pub struct AtomicTracker<const ID: u64 = 0> { 472a4802631SAlice Ryhl inner: AtomicBool, 473a4802631SAlice Ryhl // This value needs to be pinned to justify the INVARIANT: comment in `AtomicTracker::new`. 474a4802631SAlice Ryhl _pin: PhantomPinned, 475a4802631SAlice Ryhl } 476a4802631SAlice Ryhl 477a4802631SAlice Ryhl impl<const ID: u64> AtomicTracker<ID> { 478a4802631SAlice Ryhl /// Creates a new initializer for this type. new() -> impl PinInit<Self>479a4802631SAlice Ryhl pub fn new() -> impl PinInit<Self> { 480a4802631SAlice Ryhl // INVARIANT: Pin-init initializers can't be used on an existing `Arc`, so this value will 481a4802631SAlice Ryhl // not be constructed in an `Arc` that already has a `ListArc`. 482a4802631SAlice Ryhl Self { 483a4802631SAlice Ryhl inner: AtomicBool::new(false), 484a4802631SAlice Ryhl _pin: PhantomPinned, 485a4802631SAlice Ryhl } 486a4802631SAlice Ryhl } 487a4802631SAlice Ryhl project_inner(self: Pin<&mut Self>) -> &mut AtomicBool488a4802631SAlice Ryhl fn project_inner(self: Pin<&mut Self>) -> &mut AtomicBool { 489a4802631SAlice Ryhl // SAFETY: The `inner` field is not structurally pinned, so we may obtain a mutable 490a4802631SAlice Ryhl // reference to it even if we only have a pinned reference to `self`. 491a4802631SAlice Ryhl unsafe { &mut Pin::into_inner_unchecked(self).inner } 492a4802631SAlice Ryhl } 493a4802631SAlice Ryhl } 494a4802631SAlice Ryhl 495a4802631SAlice Ryhl impl<const ID: u64> ListArcSafe<ID> for AtomicTracker<ID> { on_create_list_arc_from_unique(self: Pin<&mut Self>)496a4802631SAlice Ryhl unsafe fn on_create_list_arc_from_unique(self: Pin<&mut Self>) { 497a4802631SAlice Ryhl // INVARIANT: We just created a ListArc, so the boolean should be true. 498a4802631SAlice Ryhl *self.project_inner().get_mut() = true; 499a4802631SAlice Ryhl } 500a4802631SAlice Ryhl on_drop_list_arc(&self)501a4802631SAlice Ryhl unsafe fn on_drop_list_arc(&self) { 502a4802631SAlice Ryhl // INVARIANT: We just dropped a ListArc, so the boolean should be false. 503a4802631SAlice Ryhl self.inner.store(false, Ordering::Release); 504a4802631SAlice Ryhl } 505a4802631SAlice Ryhl } 506a4802631SAlice Ryhl 507a4802631SAlice Ryhl // SAFETY: If this method returns `true`, then by the type invariant there is no `ListArc` before 508a4802631SAlice Ryhl // this call, so it is okay to create a new `ListArc`. 509a4802631SAlice Ryhl // 510a4802631SAlice Ryhl // The acquire ordering will synchronize with the release store from the destruction of any 511a4802631SAlice Ryhl // previous `ListArc`, so if there was a previous `ListArc`, then the destruction of the previous 512a4802631SAlice Ryhl // `ListArc` happens-before the creation of the new `ListArc`. 513a4802631SAlice Ryhl unsafe impl<const ID: u64> TryNewListArc<ID> for AtomicTracker<ID> { try_new_list_arc(&self) -> bool514a4802631SAlice Ryhl fn try_new_list_arc(&self) -> bool { 515a4802631SAlice Ryhl // INVARIANT: If this method returns true, then the boolean used to be false, and is no 516a4802631SAlice Ryhl // longer false, so it is okay for the caller to create a new [`ListArc`]. 517a4802631SAlice Ryhl self.inner 518a4802631SAlice Ryhl .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) 519a4802631SAlice Ryhl .is_ok() 520a4802631SAlice Ryhl } 521a4802631SAlice Ryhl } 522