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