list.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2024 Google LLC.
  3. //! A linked list implementation.
  4. // May not be needed in Rust 1.87.0 (pending beta backport).
  5. #![allow(clippy::ptr_eq)]
  6. use crate::init::PinInit;
  7. use crate::sync::ArcBorrow;
  8. use crate::types::Opaque;
  9. use core::iter::{DoubleEndedIterator, FusedIterator};
  10. use core::marker::PhantomData;
  11. use core::ptr;
  12. mod impl_list_item_mod;
  13. pub use self::impl_list_item_mod::{
  14. impl_has_list_links, impl_has_list_links_self_ptr, impl_list_item, HasListLinks, HasSelfPtr,
  15. };
  16. mod arc;
  17. pub use self::arc::{impl_list_arc_safe, AtomicTracker, ListArc, ListArcSafe, TryNewListArc};
  18. mod arc_field;
  19. pub use self::arc_field::{define_list_arc_field_getter, ListArcField};
  20. /// A linked list.
  21. ///
  22. /// All elements in this linked list will be [`ListArc`] references to the value. Since a value can
  23. /// only have one `ListArc` (for each pair of prev/next pointers), this ensures that the same
  24. /// prev/next pointers are not used for several linked lists.
  25. ///
  26. /// # Invariants
  27. ///
  28. /// * If the list is empty, then `first` is null. Otherwise, `first` points at the `ListLinks`
  29. /// field of the first element in the list.
  30. /// * All prev/next pointers in `ListLinks` fields of items in the list are valid and form a cycle.
  31. /// * For every item in the list, the list owns the associated [`ListArc`] reference and has
  32. /// exclusive access to the `ListLinks` field.
  33. pub struct List<T: ?Sized + ListItem<ID>, const ID: u64 = 0> {
  34. first: *mut ListLinksFields,
  35. _ty: PhantomData<ListArc<T, ID>>,
  36. }
  37. // SAFETY: This is a container of `ListArc<T, ID>`, and access to the container allows the same
  38. // type of access to the `ListArc<T, ID>` elements.
  39. unsafe impl<T, const ID: u64> Send for List<T, ID>
  40. where
  41. ListArc<T, ID>: Send,
  42. T: ?Sized + ListItem<ID>,
  43. {
  44. }
  45. // SAFETY: This is a container of `ListArc<T, ID>`, and access to the container allows the same
  46. // type of access to the `ListArc<T, ID>` elements.
  47. unsafe impl<T, const ID: u64> Sync for List<T, ID>
  48. where
  49. ListArc<T, ID>: Sync,
  50. T: ?Sized + ListItem<ID>,
  51. {
  52. }
  53. /// Implemented by types where a [`ListArc<Self>`] can be inserted into a [`List`].
  54. ///
  55. /// # Safety
  56. ///
  57. /// Implementers must ensure that they provide the guarantees documented on methods provided by
  58. /// this trait.
  59. ///
  60. /// [`ListArc<Self>`]: ListArc
  61. pub unsafe trait ListItem<const ID: u64 = 0>: ListArcSafe<ID> {
  62. /// Views the [`ListLinks`] for this value.
  63. ///
  64. /// # Guarantees
  65. ///
  66. /// If there is a previous call to `prepare_to_insert` and there is no call to `post_remove`
  67. /// since the most recent such call, then this returns the same pointer as the one returned by
  68. /// the most recent call to `prepare_to_insert`.
  69. ///
  70. /// Otherwise, the returned pointer points at a read-only [`ListLinks`] with two null pointers.
  71. ///
  72. /// # Safety
  73. ///
  74. /// The provided pointer must point at a valid value. (It need not be in an `Arc`.)
  75. unsafe fn view_links(me: *const Self) -> *mut ListLinks<ID>;
  76. /// View the full value given its [`ListLinks`] field.
  77. ///
  78. /// Can only be used when the value is in a list.
  79. ///
  80. /// # Guarantees
  81. ///
  82. /// * Returns the same pointer as the one passed to the most recent call to `prepare_to_insert`.
  83. /// * The returned pointer is valid until the next call to `post_remove`.
  84. ///
  85. /// # Safety
  86. ///
  87. /// * The provided pointer must originate from the most recent call to `prepare_to_insert`, or
  88. /// from a call to `view_links` that happened after the most recent call to
  89. /// `prepare_to_insert`.
  90. /// * Since the most recent call to `prepare_to_insert`, the `post_remove` method must not have
  91. /// been called.
  92. unsafe fn view_value(me: *mut ListLinks<ID>) -> *const Self;
  93. /// This is called when an item is inserted into a [`List`].
  94. ///
  95. /// # Guarantees
  96. ///
  97. /// The caller is granted exclusive access to the returned [`ListLinks`] until `post_remove` is
  98. /// called.
  99. ///
  100. /// # Safety
  101. ///
  102. /// * The provided pointer must point at a valid value in an [`Arc`].
  103. /// * Calls to `prepare_to_insert` and `post_remove` on the same value must alternate.
  104. /// * The caller must own the [`ListArc`] for this value.
  105. /// * The caller must not give up ownership of the [`ListArc`] unless `post_remove` has been
  106. /// called after this call to `prepare_to_insert`.
  107. ///
  108. /// [`Arc`]: crate::sync::Arc
  109. unsafe fn prepare_to_insert(me: *const Self) -> *mut ListLinks<ID>;
  110. /// This undoes a previous call to `prepare_to_insert`.
  111. ///
  112. /// # Guarantees
  113. ///
  114. /// The returned pointer is the pointer that was originally passed to `prepare_to_insert`.
  115. ///
  116. /// # Safety
  117. ///
  118. /// The provided pointer must be the pointer returned by the most recent call to
  119. /// `prepare_to_insert`.
  120. unsafe fn post_remove(me: *mut ListLinks<ID>) -> *const Self;
  121. }
  122. #[repr(C)]
  123. #[derive(Copy, Clone)]
  124. struct ListLinksFields {
  125. next: *mut ListLinksFields,
  126. prev: *mut ListLinksFields,
  127. }
  128. /// The prev/next pointers for an item in a linked list.
  129. ///
  130. /// # Invariants
  131. ///
  132. /// The fields are null if and only if this item is not in a list.
  133. #[repr(transparent)]
  134. pub struct ListLinks<const ID: u64 = 0> {
  135. // This type is `!Unpin` for aliasing reasons as the pointers are part of an intrusive linked
  136. // list.
  137. inner: Opaque<ListLinksFields>,
  138. }
  139. // SAFETY: The only way to access/modify the pointers inside of `ListLinks<ID>` is via holding the
  140. // associated `ListArc<T, ID>`. Since that type correctly implements `Send`, it is impossible to
  141. // move this an instance of this type to a different thread if the pointees are `!Send`.
  142. unsafe impl<const ID: u64> Send for ListLinks<ID> {}
  143. // SAFETY: The type is opaque so immutable references to a ListLinks are useless. Therefore, it's
  144. // okay to have immutable access to a ListLinks from several threads at once.
  145. unsafe impl<const ID: u64> Sync for ListLinks<ID> {}
  146. impl<const ID: u64> ListLinks<ID> {
  147. /// Creates a new initializer for this type.
  148. pub fn new() -> impl PinInit<Self> {
  149. // INVARIANT: Pin-init initializers can't be used on an existing `Arc`, so this value will
  150. // not be constructed in an `Arc` that already has a `ListArc`.
  151. ListLinks {
  152. inner: Opaque::new(ListLinksFields {
  153. prev: ptr::null_mut(),
  154. next: ptr::null_mut(),
  155. }),
  156. }
  157. }
  158. /// # Safety
  159. ///
  160. /// `me` must be dereferenceable.
  161. #[inline]
  162. unsafe fn fields(me: *mut Self) -> *mut ListLinksFields {
  163. // SAFETY: The caller promises that the pointer is valid.
  164. unsafe { Opaque::raw_get(ptr::addr_of!((*me).inner)) }
  165. }
  166. /// # Safety
  167. ///
  168. /// `me` must be dereferenceable.
  169. #[inline]
  170. unsafe fn from_fields(me: *mut ListLinksFields) -> *mut Self {
  171. me.cast()
  172. }
  173. }
  174. /// Similar to [`ListLinks`], but also contains a pointer to the full value.
  175. ///
  176. /// This type can be used instead of [`ListLinks`] to support lists with trait objects.
  177. #[repr(C)]
  178. pub struct ListLinksSelfPtr<T: ?Sized, const ID: u64 = 0> {
  179. /// The `ListLinks` field inside this value.
  180. ///
  181. /// This is public so that it can be used with `impl_has_list_links!`.
  182. pub inner: ListLinks<ID>,
  183. // UnsafeCell is not enough here because we use `Opaque::uninit` as a dummy value, and
  184. // `ptr::null()` doesn't work for `T: ?Sized`.
  185. self_ptr: Opaque<*const T>,
  186. }
  187. // SAFETY: The fields of a ListLinksSelfPtr can be moved across thread boundaries.
  188. unsafe impl<T: ?Sized + Send, const ID: u64> Send for ListLinksSelfPtr<T, ID> {}
  189. // SAFETY: The type is opaque so immutable references to a ListLinksSelfPtr are useless. Therefore,
  190. // it's okay to have immutable access to a ListLinks from several threads at once.
  191. //
  192. // Note that `inner` being a public field does not prevent this type from being opaque, since
  193. // `inner` is a opaque type.
  194. unsafe impl<T: ?Sized + Sync, const ID: u64> Sync for ListLinksSelfPtr<T, ID> {}
  195. impl<T: ?Sized, const ID: u64> ListLinksSelfPtr<T, ID> {
  196. /// The offset from the [`ListLinks`] to the self pointer field.
  197. pub const LIST_LINKS_SELF_PTR_OFFSET: usize = core::mem::offset_of!(Self, self_ptr);
  198. /// Creates a new initializer for this type.
  199. pub fn new() -> impl PinInit<Self> {
  200. // INVARIANT: Pin-init initializers can't be used on an existing `Arc`, so this value will
  201. // not be constructed in an `Arc` that already has a `ListArc`.
  202. Self {
  203. inner: ListLinks {
  204. inner: Opaque::new(ListLinksFields {
  205. prev: ptr::null_mut(),
  206. next: ptr::null_mut(),
  207. }),
  208. },
  209. self_ptr: Opaque::uninit(),
  210. }
  211. }
  212. }
  213. impl<T: ?Sized + ListItem<ID>, const ID: u64> List<T, ID> {
  214. /// Creates a new empty list.
  215. pub const fn new() -> Self {
  216. Self {
  217. first: ptr::null_mut(),
  218. _ty: PhantomData,
  219. }
  220. }
  221. /// Returns whether this list is empty.
  222. pub fn is_empty(&self) -> bool {
  223. self.first.is_null()
  224. }
  225. /// Add the provided item to the back of the list.
  226. pub fn push_back(&mut self, item: ListArc<T, ID>) {
  227. let raw_item = ListArc::into_raw(item);
  228. // SAFETY:
  229. // * We just got `raw_item` from a `ListArc`, so it's in an `Arc`.
  230. // * Since we have ownership of the `ListArc`, `post_remove` must have been called after
  231. // the most recent call to `prepare_to_insert`, if any.
  232. // * We own the `ListArc`.
  233. // * Removing items from this list is always done using `remove_internal_inner`, which
  234. // calls `post_remove` before giving up ownership.
  235. let list_links = unsafe { T::prepare_to_insert(raw_item) };
  236. // SAFETY: We have not yet called `post_remove`, so `list_links` is still valid.
  237. let item = unsafe { ListLinks::fields(list_links) };
  238. if self.first.is_null() {
  239. self.first = item;
  240. // SAFETY: The caller just gave us ownership of these fields.
  241. // INVARIANT: A linked list with one item should be cyclic.
  242. unsafe {
  243. (*item).next = item;
  244. (*item).prev = item;
  245. }
  246. } else {
  247. let next = self.first;
  248. // SAFETY: By the type invariant, this pointer is valid or null. We just checked that
  249. // it's not null, so it must be valid.
  250. let prev = unsafe { (*next).prev };
  251. // SAFETY: Pointers in a linked list are never dangling, and the caller just gave us
  252. // ownership of the fields on `item`.
  253. // INVARIANT: This correctly inserts `item` between `prev` and `next`.
  254. unsafe {
  255. (*item).next = next;
  256. (*item).prev = prev;
  257. (*prev).next = item;
  258. (*next).prev = item;
  259. }
  260. }
  261. }
  262. /// Add the provided item to the front of the list.
  263. pub fn push_front(&mut self, item: ListArc<T, ID>) {
  264. let raw_item = ListArc::into_raw(item);
  265. // SAFETY:
  266. // * We just got `raw_item` from a `ListArc`, so it's in an `Arc`.
  267. // * If this requirement is violated, then the previous caller of `prepare_to_insert`
  268. // violated the safety requirement that they can't give up ownership of the `ListArc`
  269. // until they call `post_remove`.
  270. // * We own the `ListArc`.
  271. // * Removing items] from this list is always done using `remove_internal_inner`, which
  272. // calls `post_remove` before giving up ownership.
  273. let list_links = unsafe { T::prepare_to_insert(raw_item) };
  274. // SAFETY: We have not yet called `post_remove`, so `list_links` is still valid.
  275. let item = unsafe { ListLinks::fields(list_links) };
  276. if self.first.is_null() {
  277. // SAFETY: The caller just gave us ownership of these fields.
  278. // INVARIANT: A linked list with one item should be cyclic.
  279. unsafe {
  280. (*item).next = item;
  281. (*item).prev = item;
  282. }
  283. } else {
  284. let next = self.first;
  285. // SAFETY: We just checked that `next` is non-null.
  286. let prev = unsafe { (*next).prev };
  287. // SAFETY: Pointers in a linked list are never dangling, and the caller just gave us
  288. // ownership of the fields on `item`.
  289. // INVARIANT: This correctly inserts `item` between `prev` and `next`.
  290. unsafe {
  291. (*item).next = next;
  292. (*item).prev = prev;
  293. (*prev).next = item;
  294. (*next).prev = item;
  295. }
  296. }
  297. self.first = item;
  298. }
  299. /// Removes the last item from this list.
  300. pub fn pop_back(&mut self) -> Option<ListArc<T, ID>> {
  301. if self.first.is_null() {
  302. return None;
  303. }
  304. // SAFETY: We just checked that the list is not empty.
  305. let last = unsafe { (*self.first).prev };
  306. // SAFETY: The last item of this list is in this list.
  307. Some(unsafe { self.remove_internal(last) })
  308. }
  309. /// Removes the first item from this list.
  310. pub fn pop_front(&mut self) -> Option<ListArc<T, ID>> {
  311. if self.first.is_null() {
  312. return None;
  313. }
  314. // SAFETY: The first item of this list is in this list.
  315. Some(unsafe { self.remove_internal(self.first) })
  316. }
  317. /// Removes the provided item from this list and returns it.
  318. ///
  319. /// This returns `None` if the item is not in the list. (Note that by the safety requirements,
  320. /// this means that the item is not in any list.)
  321. ///
  322. /// # Safety
  323. ///
  324. /// `item` must not be in a different linked list (with the same id).
  325. pub unsafe fn remove(&mut self, item: &T) -> Option<ListArc<T, ID>> {
  326. // SAFETY: TODO.
  327. let mut item = unsafe { ListLinks::fields(T::view_links(item)) };
  328. // SAFETY: The user provided a reference, and reference are never dangling.
  329. //
  330. // As for why this is not a data race, there are two cases:
  331. //
  332. // * If `item` is not in any list, then these fields are read-only and null.
  333. // * If `item` is in this list, then we have exclusive access to these fields since we
  334. // have a mutable reference to the list.
  335. //
  336. // In either case, there's no race.
  337. let ListLinksFields { next, prev } = unsafe { *item };
  338. debug_assert_eq!(next.is_null(), prev.is_null());
  339. if !next.is_null() {
  340. // This is really a no-op, but this ensures that `item` is a raw pointer that was
  341. // obtained without going through a pointer->reference->pointer conversion roundtrip.
  342. // This ensures that the list is valid under the more restrictive strict provenance
  343. // ruleset.
  344. //
  345. // SAFETY: We just checked that `next` is not null, and it's not dangling by the
  346. // list invariants.
  347. unsafe {
  348. debug_assert_eq!(item, (*next).prev);
  349. item = (*next).prev;
  350. }
  351. // SAFETY: We just checked that `item` is in a list, so the caller guarantees that it
  352. // is in this list. The pointers are in the right order.
  353. Some(unsafe { self.remove_internal_inner(item, next, prev) })
  354. } else {
  355. None
  356. }
  357. }
  358. /// Removes the provided item from the list.
  359. ///
  360. /// # Safety
  361. ///
  362. /// `item` must point at an item in this list.
  363. unsafe fn remove_internal(&mut self, item: *mut ListLinksFields) -> ListArc<T, ID> {
  364. // SAFETY: The caller promises that this pointer is not dangling, and there's no data race
  365. // since we have a mutable reference to the list containing `item`.
  366. let ListLinksFields { next, prev } = unsafe { *item };
  367. // SAFETY: The pointers are ok and in the right order.
  368. unsafe { self.remove_internal_inner(item, next, prev) }
  369. }
  370. /// Removes the provided item from the list.
  371. ///
  372. /// # Safety
  373. ///
  374. /// The `item` pointer must point at an item in this list, and we must have `(*item).next ==
  375. /// next` and `(*item).prev == prev`.
  376. unsafe fn remove_internal_inner(
  377. &mut self,
  378. item: *mut ListLinksFields,
  379. next: *mut ListLinksFields,
  380. prev: *mut ListLinksFields,
  381. ) -> ListArc<T, ID> {
  382. // SAFETY: We have exclusive access to the pointers of items in the list, and the prev/next
  383. // pointers are always valid for items in a list.
  384. //
  385. // INVARIANT: There are three cases:
  386. // * If the list has at least three items, then after removing the item, `prev` and `next`
  387. // will be next to each other.
  388. // * If the list has two items, then the remaining item will point at itself.
  389. // * If the list has one item, then `next == prev == item`, so these writes have no
  390. // effect. The list remains unchanged and `item` is still in the list for now.
  391. unsafe {
  392. (*next).prev = prev;
  393. (*prev).next = next;
  394. }
  395. // SAFETY: We have exclusive access to items in the list.
  396. // INVARIANT: `item` is being removed, so the pointers should be null.
  397. unsafe {
  398. (*item).prev = ptr::null_mut();
  399. (*item).next = ptr::null_mut();
  400. }
  401. // INVARIANT: There are three cases:
  402. // * If `item` was not the first item, then `self.first` should remain unchanged.
  403. // * If `item` was the first item and there is another item, then we just updated
  404. // `prev->next` to `next`, which is the new first item, and setting `item->next` to null
  405. // did not modify `prev->next`.
  406. // * If `item` was the only item in the list, then `prev == item`, and we just set
  407. // `item->next` to null, so this correctly sets `first` to null now that the list is
  408. // empty.
  409. if self.first == item {
  410. // SAFETY: The `prev` pointer is the value that `item->prev` had when it was in this
  411. // list, so it must be valid. There is no race since `prev` is still in the list and we
  412. // still have exclusive access to the list.
  413. self.first = unsafe { (*prev).next };
  414. }
  415. // SAFETY: `item` used to be in the list, so it is dereferenceable by the type invariants
  416. // of `List`.
  417. let list_links = unsafe { ListLinks::from_fields(item) };
  418. // SAFETY: Any pointer in the list originates from a `prepare_to_insert` call.
  419. let raw_item = unsafe { T::post_remove(list_links) };
  420. // SAFETY: The above call to `post_remove` guarantees that we can recreate the `ListArc`.
  421. unsafe { ListArc::from_raw(raw_item) }
  422. }
  423. /// Moves all items from `other` into `self`.
  424. ///
  425. /// The items of `other` are added to the back of `self`, so the last item of `other` becomes
  426. /// the last item of `self`.
  427. pub fn push_all_back(&mut self, other: &mut List<T, ID>) {
  428. // First, we insert the elements into `self`. At the end, we make `other` empty.
  429. if self.is_empty() {
  430. // INVARIANT: All of the elements in `other` become elements of `self`.
  431. self.first = other.first;
  432. } else if !other.is_empty() {
  433. let other_first = other.first;
  434. // SAFETY: The other list is not empty, so this pointer is valid.
  435. let other_last = unsafe { (*other_first).prev };
  436. let self_first = self.first;
  437. // SAFETY: The self list is not empty, so this pointer is valid.
  438. let self_last = unsafe { (*self_first).prev };
  439. // SAFETY: We have exclusive access to both lists, so we can update the pointers.
  440. // INVARIANT: This correctly sets the pointers to merge both lists. We do not need to
  441. // update `self.first` because the first element of `self` does not change.
  442. unsafe {
  443. (*self_first).prev = other_last;
  444. (*other_last).next = self_first;
  445. (*self_last).next = other_first;
  446. (*other_first).prev = self_last;
  447. }
  448. }
  449. // INVARIANT: The other list is now empty, so update its pointer.
  450. other.first = ptr::null_mut();
  451. }
  452. /// Returns a cursor to the first element of the list.
  453. ///
  454. /// If the list is empty, this returns `None`.
  455. pub fn cursor_front(&mut self) -> Option<Cursor<'_, T, ID>> {
  456. if self.first.is_null() {
  457. None
  458. } else {
  459. Some(Cursor {
  460. current: self.first,
  461. list: self,
  462. })
  463. }
  464. }
  465. /// Creates an iterator over the list.
  466. pub fn iter(&self) -> Iter<'_, T, ID> {
  467. // INVARIANT: If the list is empty, both pointers are null. Otherwise, both pointers point
  468. // at the first element of the same list.
  469. Iter {
  470. current: self.first,
  471. stop: self.first,
  472. _ty: PhantomData,
  473. }
  474. }
  475. }
  476. impl<T: ?Sized + ListItem<ID>, const ID: u64> Default for List<T, ID> {
  477. fn default() -> Self {
  478. List::new()
  479. }
  480. }
  481. impl<T: ?Sized + ListItem<ID>, const ID: u64> Drop for List<T, ID> {
  482. fn drop(&mut self) {
  483. while let Some(item) = self.pop_front() {
  484. drop(item);
  485. }
  486. }
  487. }
  488. /// An iterator over a [`List`].
  489. ///
  490. /// # Invariants
  491. ///
  492. /// * There must be a [`List`] that is immutably borrowed for the duration of `'a`.
  493. /// * The `current` pointer is null or points at a value in that [`List`].
  494. /// * The `stop` pointer is equal to the `first` field of that [`List`].
  495. #[derive(Clone)]
  496. pub struct Iter<'a, T: ?Sized + ListItem<ID>, const ID: u64 = 0> {
  497. current: *mut ListLinksFields,
  498. stop: *mut ListLinksFields,
  499. _ty: PhantomData<&'a ListArc<T, ID>>,
  500. }
  501. impl<'a, T: ?Sized + ListItem<ID>, const ID: u64> Iterator for Iter<'a, T, ID> {
  502. type Item = ArcBorrow<'a, T>;
  503. fn next(&mut self) -> Option<ArcBorrow<'a, T>> {
  504. if self.current.is_null() {
  505. return None;
  506. }
  507. let current = self.current;
  508. // SAFETY: We just checked that `current` is not null, so it is in a list, and hence not
  509. // dangling. There's no race because the iterator holds an immutable borrow to the list.
  510. let next = unsafe { (*current).next };
  511. // INVARIANT: If `current` was the last element of the list, then this updates it to null.
  512. // Otherwise, we update it to the next element.
  513. self.current = if next != self.stop {
  514. next
  515. } else {
  516. ptr::null_mut()
  517. };
  518. // SAFETY: The `current` pointer points at a value in the list.
  519. let item = unsafe { T::view_value(ListLinks::from_fields(current)) };
  520. // SAFETY:
  521. // * All values in a list are stored in an `Arc`.
  522. // * The value cannot be removed from the list for the duration of the lifetime annotated
  523. // on the returned `ArcBorrow`, because removing it from the list would require mutable
  524. // access to the list. However, the `ArcBorrow` is annotated with the iterator's
  525. // lifetime, and the list is immutably borrowed for that lifetime.
  526. // * Values in a list never have a `UniqueArc` reference.
  527. Some(unsafe { ArcBorrow::from_raw(item) })
  528. }
  529. }
  530. /// A cursor into a [`List`].
  531. ///
  532. /// # Invariants
  533. ///
  534. /// The `current` pointer points a value in `list`.
  535. pub struct Cursor<'a, T: ?Sized + ListItem<ID>, const ID: u64 = 0> {
  536. current: *mut ListLinksFields,
  537. list: &'a mut List<T, ID>,
  538. }
  539. impl<'a, T: ?Sized + ListItem<ID>, const ID: u64> Cursor<'a, T, ID> {
  540. /// Access the current element of this cursor.
  541. pub fn current(&self) -> ArcBorrow<'_, T> {
  542. // SAFETY: The `current` pointer points a value in the list.
  543. let me = unsafe { T::view_value(ListLinks::from_fields(self.current)) };
  544. // SAFETY:
  545. // * All values in a list are stored in an `Arc`.
  546. // * The value cannot be removed from the list for the duration of the lifetime annotated
  547. // on the returned `ArcBorrow`, because removing it from the list would require mutable
  548. // access to the cursor or the list. However, the `ArcBorrow` holds an immutable borrow
  549. // on the cursor, which in turn holds a mutable borrow on the list, so any such
  550. // mutable access requires first releasing the immutable borrow on the cursor.
  551. // * Values in a list never have a `UniqueArc` reference, because the list has a `ListArc`
  552. // reference, and `UniqueArc` references must be unique.
  553. unsafe { ArcBorrow::from_raw(me) }
  554. }
  555. /// Move the cursor to the next element.
  556. pub fn next(self) -> Option<Cursor<'a, T, ID>> {
  557. // SAFETY: The `current` field is always in a list.
  558. let next = unsafe { (*self.current).next };
  559. if next == self.list.first {
  560. None
  561. } else {
  562. // INVARIANT: Since `self.current` is in the `list`, its `next` pointer is also in the
  563. // `list`.
  564. Some(Cursor {
  565. current: next,
  566. list: self.list,
  567. })
  568. }
  569. }
  570. /// Move the cursor to the previous element.
  571. pub fn prev(self) -> Option<Cursor<'a, T, ID>> {
  572. // SAFETY: The `current` field is always in a list.
  573. let prev = unsafe { (*self.current).prev };
  574. if self.current == self.list.first {
  575. None
  576. } else {
  577. // INVARIANT: Since `self.current` is in the `list`, its `prev` pointer is also in the
  578. // `list`.
  579. Some(Cursor {
  580. current: prev,
  581. list: self.list,
  582. })
  583. }
  584. }
  585. /// Remove the current element from the list.
  586. pub fn remove(self) -> ListArc<T, ID> {
  587. // SAFETY: The `current` pointer always points at a member of the list.
  588. unsafe { self.list.remove_internal(self.current) }
  589. }
  590. }
  591. impl<'a, T: ?Sized + ListItem<ID>, const ID: u64> FusedIterator for Iter<'a, T, ID> {}
  592. impl<'a, T: ?Sized + ListItem<ID>, const ID: u64> IntoIterator for &'a List<T, ID> {
  593. type IntoIter = Iter<'a, T, ID>;
  594. type Item = ArcBorrow<'a, T>;
  595. fn into_iter(self) -> Iter<'a, T, ID> {
  596. self.iter()
  597. }
  598. }
  599. /// An owning iterator into a [`List`].
  600. pub struct IntoIter<T: ?Sized + ListItem<ID>, const ID: u64 = 0> {
  601. list: List<T, ID>,
  602. }
  603. impl<T: ?Sized + ListItem<ID>, const ID: u64> Iterator for IntoIter<T, ID> {
  604. type Item = ListArc<T, ID>;
  605. fn next(&mut self) -> Option<ListArc<T, ID>> {
  606. self.list.pop_front()
  607. }
  608. }
  609. impl<T: ?Sized + ListItem<ID>, const ID: u64> FusedIterator for IntoIter<T, ID> {}
  610. impl<T: ?Sized + ListItem<ID>, const ID: u64> DoubleEndedIterator for IntoIter<T, ID> {
  611. fn next_back(&mut self) -> Option<ListArc<T, ID>> {
  612. self.list.pop_back()
  613. }
  614. }
  615. impl<T: ?Sized + ListItem<ID>, const ID: u64> IntoIterator for List<T, ID> {
  616. type IntoIter = IntoIter<T, ID>;
  617. type Item = ListArc<T, ID>;
  618. fn into_iter(self) -> IntoIter<T, ID> {
  619. IntoIter { list: self }
  620. }
  621. }