1 // Copyright 2016 Amanieu d'Antras
2 // Copyright 2020 Amari Robinson
3 //
4 // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
5 // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
6 // http://opensource.org/licenses/MIT>, at your option. This file may not be
7 // copied, modified, or distributed except according to those terms.
8
9 //! Intrusive singly-linked list.
10
11 use core::cell::Cell;
12 use core::fmt;
13 use core::ptr::NonNull;
14
15 use crate::link_ops::{self, DefaultLinkOps};
16 use crate::pointer_ops::PointerOps;
17 use crate::xor_linked_list::XorLinkedListOps;
18 use crate::Adapter;
19
20 // =============================================================================
21 // SinglyLinkedListOps
22 // =============================================================================
23
24 /// Link operations for `SinglyLinkedList`.
25 pub unsafe trait SinglyLinkedListOps: link_ops::LinkOps {
26 /// Returns the "next" link pointer of `ptr`.
27 ///
28 /// # Safety
29 /// An implementation of `next` must not panic.
next(&self, ptr: Self::LinkPtr) -> Option<Self::LinkPtr>30 unsafe fn next(&self, ptr: Self::LinkPtr) -> Option<Self::LinkPtr>;
31
32 /// Sets the "next" link pointer of `ptr`.
33 ///
34 /// # Safety
35 /// An implementation of `set_next` must not panic.
set_next(&mut self, ptr: Self::LinkPtr, next: Option<Self::LinkPtr>)36 unsafe fn set_next(&mut self, ptr: Self::LinkPtr, next: Option<Self::LinkPtr>);
37 }
38
39 // =============================================================================
40 // Link
41 // =============================================================================
42
43 /// Intrusive link that allows an object to be inserted into a
44 /// `SinglyLinkedList`.
45 #[repr(align(2))]
46 pub struct Link {
47 next: Cell<Option<NonNull<Link>>>,
48 }
49
50 // Use a special value to indicate an unlinked node
51 const UNLINKED_MARKER: Option<NonNull<Link>> =
52 unsafe { Some(NonNull::new_unchecked(1 as *mut Link)) };
53
54 impl Link {
55 /// Creates a new `Link`.
56 #[inline]
new() -> Link57 pub const fn new() -> Link {
58 Link {
59 next: Cell::new(UNLINKED_MARKER),
60 }
61 }
62
63 /// Checks whether the `Link` is linked into a `SinglyLinkedList`.
64 #[inline]
is_linked(&self) -> bool65 pub fn is_linked(&self) -> bool {
66 self.next.get() != UNLINKED_MARKER
67 }
68
69 /// Forcibly unlinks an object from a `SinglyLinkedList`.
70 ///
71 /// # Safety
72 ///
73 /// It is undefined behavior to call this function while still linked into a
74 /// `SinglyLinkedList`. The only situation where this function is useful is
75 /// after calling `fast_clear` on a `SinglyLinkedList`, since this clears
76 /// the collection without marking the nodes as unlinked.
77 #[inline]
force_unlink(&self)78 pub unsafe fn force_unlink(&self) {
79 self.next.set(UNLINKED_MARKER);
80 }
81 }
82
83 impl DefaultLinkOps for Link {
84 type Ops = LinkOps;
85
86 const NEW: Self::Ops = LinkOps;
87 }
88
89 // An object containing a link can be sent to another thread if it is unlinked.
90 unsafe impl Send for Link {}
91
92 // Provide an implementation of Clone which simply initializes the new link as
93 // unlinked. This allows structs containing a link to derive Clone.
94 impl Clone for Link {
95 #[inline]
clone(&self) -> Link96 fn clone(&self) -> Link {
97 Link::new()
98 }
99 }
100
101 // Same as above
102 impl Default for Link {
103 #[inline]
default() -> Link104 fn default() -> Link {
105 Link::new()
106 }
107 }
108
109 // Provide an implementation of Debug so that structs containing a link can
110 // still derive Debug.
111 impl fmt::Debug for Link {
112 #[inline]
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result113 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 // There isn't anything sensible to print here except whether the link
115 // is currently in a list.
116 if self.is_linked() {
117 write!(f, "linked")
118 } else {
119 write!(f, "unlinked")
120 }
121 }
122 }
123
124 // =============================================================================
125 // LinkOps
126 // =============================================================================
127
128 /// Default `LinkOps` implementation for `SinglyLinkedList`.
129 #[derive(Clone, Copy, Default)]
130 pub struct LinkOps;
131
132 unsafe impl link_ops::LinkOps for LinkOps {
133 type LinkPtr = NonNull<Link>;
134
135 #[inline]
acquire_link(&mut self, ptr: Self::LinkPtr) -> bool136 unsafe fn acquire_link(&mut self, ptr: Self::LinkPtr) -> bool {
137 if ptr.as_ref().is_linked() {
138 false
139 } else {
140 ptr.as_ref().next.set(None);
141 true
142 }
143 }
144
145 #[inline]
release_link(&mut self, ptr: Self::LinkPtr)146 unsafe fn release_link(&mut self, ptr: Self::LinkPtr) {
147 ptr.as_ref().next.set(UNLINKED_MARKER);
148 }
149 }
150
151 unsafe impl SinglyLinkedListOps for LinkOps {
152 #[inline]
next(&self, ptr: Self::LinkPtr) -> Option<Self::LinkPtr>153 unsafe fn next(&self, ptr: Self::LinkPtr) -> Option<Self::LinkPtr> {
154 ptr.as_ref().next.get()
155 }
156
157 #[inline]
set_next(&mut self, ptr: Self::LinkPtr, next: Option<Self::LinkPtr>)158 unsafe fn set_next(&mut self, ptr: Self::LinkPtr, next: Option<Self::LinkPtr>) {
159 ptr.as_ref().next.set(next);
160 }
161 }
162
163 unsafe impl XorLinkedListOps for LinkOps {
164 #[inline]
next( &self, ptr: Self::LinkPtr, prev: Option<Self::LinkPtr>, ) -> Option<Self::LinkPtr>165 unsafe fn next(
166 &self,
167 ptr: Self::LinkPtr,
168 prev: Option<Self::LinkPtr>,
169 ) -> Option<Self::LinkPtr> {
170 let packed = ptr
171 .as_ref()
172 .next
173 .get()
174 .map(|x| x.as_ptr() as usize)
175 .unwrap_or(0);
176 let raw = packed ^ prev.map(|x| x.as_ptr() as usize).unwrap_or(0);
177
178 NonNull::new(raw as *mut _)
179 }
180
181 #[inline]
prev( &self, ptr: Self::LinkPtr, next: Option<Self::LinkPtr>, ) -> Option<Self::LinkPtr>182 unsafe fn prev(
183 &self,
184 ptr: Self::LinkPtr,
185 next: Option<Self::LinkPtr>,
186 ) -> Option<Self::LinkPtr> {
187 let packed = ptr
188 .as_ref()
189 .next
190 .get()
191 .map(|x| x.as_ptr() as usize)
192 .unwrap_or(0);
193 let raw = packed ^ next.map(|x| x.as_ptr() as usize).unwrap_or(0);
194 NonNull::new(raw as *mut _)
195 }
196
197 #[inline]
set( &mut self, ptr: Self::LinkPtr, prev: Option<Self::LinkPtr>, next: Option<Self::LinkPtr>, )198 unsafe fn set(
199 &mut self,
200 ptr: Self::LinkPtr,
201 prev: Option<Self::LinkPtr>,
202 next: Option<Self::LinkPtr>,
203 ) {
204 let new_packed = prev.map(|x| x.as_ptr() as usize).unwrap_or(0)
205 ^ next.map(|x| x.as_ptr() as usize).unwrap_or(0);
206
207 let new_next = NonNull::new(new_packed as *mut _);
208 ptr.as_ref().next.set(new_next);
209 }
210
211 #[inline]
replace_next_or_prev( &mut self, ptr: Self::LinkPtr, old: Option<Self::LinkPtr>, new: Option<Self::LinkPtr>, )212 unsafe fn replace_next_or_prev(
213 &mut self,
214 ptr: Self::LinkPtr,
215 old: Option<Self::LinkPtr>,
216 new: Option<Self::LinkPtr>,
217 ) {
218 let packed = ptr
219 .as_ref()
220 .next
221 .get()
222 .map(|x| x.as_ptr() as usize)
223 .unwrap_or(0);
224 let new_packed = packed
225 ^ old.map(|x| x.as_ptr() as usize).unwrap_or(0)
226 ^ new.map(|x| x.as_ptr() as usize).unwrap_or(0);
227
228 let new_next = NonNull::new(new_packed as *mut _);
229 ptr.as_ref().next.set(new_next);
230 }
231 }
232
233 #[inline]
link_between<T: SinglyLinkedListOps>( link_ops: &mut T, ptr: T::LinkPtr, prev: Option<T::LinkPtr>, next: Option<T::LinkPtr>, )234 unsafe fn link_between<T: SinglyLinkedListOps>(
235 link_ops: &mut T,
236 ptr: T::LinkPtr,
237 prev: Option<T::LinkPtr>,
238 next: Option<T::LinkPtr>,
239 ) {
240 if let Some(prev) = prev {
241 link_ops.set_next(prev, Some(ptr));
242 }
243 link_ops.set_next(ptr, next);
244 }
245
246 #[inline]
link_after<T: SinglyLinkedListOps>(link_ops: &mut T, ptr: T::LinkPtr, prev: T::LinkPtr)247 unsafe fn link_after<T: SinglyLinkedListOps>(link_ops: &mut T, ptr: T::LinkPtr, prev: T::LinkPtr) {
248 link_between(link_ops, ptr, Some(prev), link_ops.next(prev));
249 }
250
251 #[inline]
replace_with<T: SinglyLinkedListOps>( link_ops: &mut T, ptr: T::LinkPtr, prev: Option<T::LinkPtr>, new: T::LinkPtr, )252 unsafe fn replace_with<T: SinglyLinkedListOps>(
253 link_ops: &mut T,
254 ptr: T::LinkPtr,
255 prev: Option<T::LinkPtr>,
256 new: T::LinkPtr,
257 ) {
258 if let Some(prev) = prev {
259 link_ops.set_next(prev, Some(new));
260 }
261 link_ops.set_next(new, link_ops.next(ptr));
262 link_ops.release_link(ptr);
263 }
264
265 #[inline]
remove<T: SinglyLinkedListOps>( link_ops: &mut T, ptr: T::LinkPtr, prev: Option<T::LinkPtr>, )266 unsafe fn remove<T: SinglyLinkedListOps>(
267 link_ops: &mut T,
268 ptr: T::LinkPtr,
269 prev: Option<T::LinkPtr>,
270 ) {
271 if let Some(prev) = prev {
272 link_ops.set_next(prev, link_ops.next(ptr));
273 }
274 link_ops.release_link(ptr);
275 }
276
277 #[inline]
splice<T: SinglyLinkedListOps>( link_ops: &mut T, start: T::LinkPtr, end: T::LinkPtr, prev: Option<T::LinkPtr>, next: Option<T::LinkPtr>, )278 unsafe fn splice<T: SinglyLinkedListOps>(
279 link_ops: &mut T,
280 start: T::LinkPtr,
281 end: T::LinkPtr,
282 prev: Option<T::LinkPtr>,
283 next: Option<T::LinkPtr>,
284 ) {
285 link_ops.set_next(end, next);
286 if let Some(prev) = prev {
287 link_ops.set_next(prev, Some(start));
288 }
289 }
290
291 // =============================================================================
292 // Cursor, CursorMut
293 // =============================================================================
294
295 /// A cursor which provides read-only access to a `SinglyLinkedList`.
296 pub struct Cursor<'a, A: Adapter>
297 where
298 A::LinkOps: SinglyLinkedListOps,
299 {
300 current: Option<<A::LinkOps as link_ops::LinkOps>::LinkPtr>,
301 list: &'a SinglyLinkedList<A>,
302 }
303
304 impl<'a, A: Adapter> Clone for Cursor<'a, A>
305 where
306 A::LinkOps: SinglyLinkedListOps,
307 {
308 #[inline]
309 fn clone(&self) -> Cursor<'a, A> {
310 Cursor {
311 current: self.current,
312 list: self.list,
313 }
314 }
315 }
316
317 impl<'a, A: Adapter> Cursor<'a, A>
318 where
319 A::LinkOps: SinglyLinkedListOps,
320 {
321 /// Checks if the cursor is currently pointing to the null object.
322 #[inline]
323 pub fn is_null(&self) -> bool {
324 self.current.is_none()
325 }
326
327 /// Returns a reference to the object that the cursor is currently
328 /// pointing to.
329 ///
330 /// This returns `None` if the cursor is currently pointing to the null
331 /// object.
332 #[inline]
333 pub fn get(&self) -> Option<&'a <A::PointerOps as PointerOps>::Value> {
334 Some(unsafe { &*self.list.adapter.get_value(self.current?) })
335 }
336
337 /// Clones and returns the pointer that points to the element that the
338 /// cursor is referencing.
339 ///
340 /// This returns `None` if the cursor is currently pointing to the null
341 /// object.
342 #[inline]
343 pub fn clone_pointer(&self) -> Option<<A::PointerOps as PointerOps>::Pointer>
344 where
345 <A::PointerOps as PointerOps>::Pointer: Clone,
346 {
347 let raw_pointer = self.get()? as *const <A::PointerOps as PointerOps>::Value;
348 Some(unsafe {
349 crate::pointer_ops::clone_pointer_from_raw(self.list.adapter.pointer_ops(), raw_pointer)
350 })
351 }
352
353 /// Moves the cursor to the next element of the `SinglyLinkedList`.
354 ///
355 /// If the cursor is pointer to the null object then this will move it to
356 /// the first element of the `SinglyLinkedList`. If it is pointing to the
357 /// last element of the `SinglyLinkedList` then this will move it to the
358 /// null object.
359 #[inline]
360 pub fn move_next(&mut self) {
361 if let Some(current) = self.current {
362 self.current = unsafe { self.list.adapter.link_ops().next(current) };
363 } else {
364 self.current = self.list.head;
365 }
366 }
367
368 /// Returns a cursor pointing to the next element of the `SinglyLinkedList`.
369 ///
370 /// If the cursor is pointer to the null object then this will return the
371 /// first element of the `SinglyLinkedList`. If it is pointing to the last
372 /// element of the `SinglyLinkedList` then this will return a null cursor.
373 #[inline]
374 pub fn peek_next(&self) -> Cursor<'_, A> {
375 let mut next = self.clone();
376 next.move_next();
377 next
378 }
379 }
380
381 /// A cursor which provides mutable access to a `SinglyLinkedList`.
382 pub struct CursorMut<'a, A: Adapter>
383 where
384 A::LinkOps: SinglyLinkedListOps,
385 {
386 current: Option<<A::LinkOps as link_ops::LinkOps>::LinkPtr>,
387 list: &'a mut SinglyLinkedList<A>,
388 }
389
390 impl<'a, A: Adapter> CursorMut<'a, A>
391 where
392 A::LinkOps: SinglyLinkedListOps,
393 {
394 /// Checks if the cursor is currently pointing to the null object.
395 #[inline]
396 pub fn is_null(&self) -> bool {
397 self.current.is_none()
398 }
399
400 /// Returns a reference to the object that the cursor is currently
401 /// pointing to.
402 ///
403 /// This returns None if the cursor is currently pointing to the null
404 /// object.
405 #[inline]
406 pub fn get(&self) -> Option<&<A::PointerOps as PointerOps>::Value> {
407 Some(unsafe { &*self.list.adapter.get_value(self.current?) })
408 }
409
410 /// Returns a read-only cursor pointing to the current element.
411 ///
412 /// The lifetime of the returned `Cursor` is bound to that of the
413 /// `CursorMut`, which means it cannot outlive the `CursorMut` and that the
414 /// `CursorMut` is frozen for the lifetime of the `Cursor`.
415 #[inline]
416 pub fn as_cursor(&self) -> Cursor<'_, A> {
417 Cursor {
418 current: self.current,
419 list: self.list,
420 }
421 }
422
423 /// Moves the cursor to the next element of the `SinglyLinkedList`.
424 ///
425 /// If the cursor is pointer to the null object then this will move it to
426 /// the first element of the `SinglyLinkedList`. If it is pointing to the
427 /// last element of the `SinglyLinkedList` then this will move it to the
428 /// null object.
429 #[inline]
430 pub fn move_next(&mut self) {
431 if let Some(current) = self.current {
432 self.current = unsafe { self.list.adapter.link_ops().next(current) };
433 } else {
434 self.current = self.list.head;
435 }
436 }
437
438 /// Returns a cursor pointing to the next element of the `SinglyLinkedList`.
439 ///
440 /// If the cursor is pointer to the null object then this will return the
441 /// first element of the `SinglyLinkedList`. If it is pointing to the last
442 /// element of the `SinglyLinkedList` then this will return a null cursor.
443 #[inline]
444 pub fn peek_next(&self) -> Cursor<'_, A> {
445 let mut next = self.as_cursor();
446 next.move_next();
447 next
448 }
449
450 /// Removes the next element from the `SinglyLinkedList`.
451 ///
452 /// A pointer to the element that was removed is returned, and the cursor is
453 /// not moved.
454 ///
455 /// If the cursor is currently pointing to the last element of the
456 /// `SinglyLinkedList` then no element is removed and `None` is returned.
457 #[inline]
458 pub fn remove_next(&mut self) -> Option<<A::PointerOps as PointerOps>::Pointer> {
459 unsafe {
460 let next = if let Some(current) = self.current {
461 self.list.adapter.link_ops().next(current)
462 } else {
463 self.list.head
464 }?;
465
466 if self.is_null() {
467 self.list.head = self.list.adapter.link_ops().next(next);
468 }
469 remove(self.list.adapter.link_ops_mut(), next, self.current);
470
471 Some(
472 self.list
473 .adapter
474 .pointer_ops()
475 .from_raw(self.list.adapter.get_value(next)),
476 )
477 }
478 }
479
480 /// Removes the next element from the `SinglyLinkedList` and inserts
481 /// another object in its place.
482 ///
483 /// A pointer to the element that was removed is returned, and the cursor is
484 /// not moved.
485 ///
486 /// If the cursor is currently pointing to the last element of the
487 /// `SinglyLinkedList` then no element is added or removed and an error is
488 /// returned containing the given `val` parameter.
489 ///
490 /// # Panics
491 ///
492 /// Panics if the new element is already linked to a different intrusive
493 /// collection.
494 #[inline]
495 pub fn replace_next_with(
496 &mut self,
497 val: <A::PointerOps as PointerOps>::Pointer,
498 ) -> Result<<A::PointerOps as PointerOps>::Pointer, <A::PointerOps as PointerOps>::Pointer>
499 {
500 unsafe {
501 let next = if let Some(current) = self.current {
502 self.list.adapter.link_ops().next(current)
503 } else {
504 self.list.head
505 };
506 match next {
507 Some(next) => {
508 let new = self.list.node_from_value(val);
509 if self.is_null() {
510 self.list.head = Some(new);
511 }
512 replace_with(self.list.adapter.link_ops_mut(), next, self.current, new);
513 Ok(self
514 .list
515 .adapter
516 .pointer_ops()
517 .from_raw(self.list.adapter.get_value(next)))
518 }
519 None => Err(val),
520 }
521 }
522 }
523
524 /// Inserts a new element into the `SinglyLinkedList` after the current one.
525 ///
526 /// If the cursor is pointing at the null object then the new element is
527 /// inserted at the front of the `SinglyLinkedList`.
528 ///
529 /// # Panics
530 ///
531 /// Panics if the new element is already linked to a different intrusive
532 /// collection.
533 #[inline]
534 pub fn insert_after(&mut self, val: <A::PointerOps as PointerOps>::Pointer) {
535 unsafe {
536 let new = self.list.node_from_value(val);
537 if let Some(current) = self.current {
538 link_after(self.list.adapter.link_ops_mut(), new, current);
539 } else {
540 link_between(self.list.adapter.link_ops_mut(), new, None, self.list.head);
541 self.list.head = Some(new);
542 }
543 }
544 }
545
546 /// Inserts the elements from the given `SinglyLinkedList` after the current
547 /// one.
548 ///
549 /// If the cursor is pointing at the null object then the new elements are
550 /// inserted at the start of the `SinglyLinkedList`.
551 ///
552 /// Note that if the cursor is not pointing to the last element of the
553 /// `SinglyLinkedList` then the given list must be scanned to find its last
554 /// element. This has linear time complexity.
555 #[inline]
556 pub fn splice_after(&mut self, mut list: SinglyLinkedList<A>) {
557 if let Some(head) = list.head {
558 unsafe {
559 let next = if let Some(current) = self.current {
560 self.list.adapter.link_ops().next(current)
561 } else {
562 self.list.head
563 };
564 if let Some(next) = next {
565 let mut tail = head;
566 while let Some(x) = self.list.adapter.link_ops().next(tail) {
567 tail = x;
568 }
569 splice(
570 self.list.adapter.link_ops_mut(),
571 head,
572 tail,
573 self.current,
574 Some(next),
575 );
576 if self.is_null() {
577 self.list.head = list.head;
578 }
579 } else {
580 if let Some(current) = self.current {
581 self.list
582 .adapter
583 .link_ops_mut()
584 .set_next(current, list.head);
585 } else {
586 self.list.head = list.head;
587 }
588 }
589 list.head = None;
590 }
591 }
592 }
593
594 /// Splits the list into two after the current element. This will return a
595 /// new list consisting of everything after the cursor, with the original
596 /// list retaining everything before.
597 ///
598 /// If the cursor is pointing at the null object then the entire contents
599 /// of the `SinglyLinkedList` are moved.
600 #[inline]
601 pub fn split_after(&mut self) -> SinglyLinkedList<A>
602 where
603 A: Clone,
604 {
605 if let Some(current) = self.current {
606 unsafe {
607 let list = SinglyLinkedList {
608 head: self.list.adapter.link_ops().next(current),
609 adapter: self.list.adapter.clone(),
610 };
611 self.list.adapter.link_ops_mut().set_next(current, None);
612 list
613 }
614 } else {
615 let list = SinglyLinkedList {
616 head: self.list.head,
617 adapter: self.list.adapter.clone(),
618 };
619 self.list.head = None;
620 list
621 }
622 }
623 }
624
625 // =============================================================================
626 // SinglyLinkedList
627 // =============================================================================
628
629 /// An intrusive singly-linked list.
630 ///
631 /// When this collection is dropped, all elements linked into it will be
632 /// converted back to owned pointers and dropped.
633 pub struct SinglyLinkedList<A: Adapter>
634 where
635 A::LinkOps: SinglyLinkedListOps,
636 {
637 head: Option<<A::LinkOps as link_ops::LinkOps>::LinkPtr>,
638 adapter: A,
639 }
640
641 impl<A: Adapter> SinglyLinkedList<A>
642 where
643 A::LinkOps: SinglyLinkedListOps,
644 {
645 #[inline]
646 fn node_from_value(
647 &mut self,
648 val: <A::PointerOps as PointerOps>::Pointer,
649 ) -> <A::LinkOps as link_ops::LinkOps>::LinkPtr {
650 use link_ops::LinkOps;
651
652 unsafe {
653 let raw = self.adapter.pointer_ops().into_raw(val);
654 let link = self.adapter.get_link(raw);
655
656 if !self.adapter.link_ops_mut().acquire_link(link) {
657 // convert the node back into a pointer
658 self.adapter.pointer_ops().from_raw(raw);
659
660 panic!("attempted to insert an object that is already linked");
661 }
662
663 link
664 }
665 }
666
667 /// Creates an empty `SinglyLinkedList`.
668 #[cfg(not(feature = "nightly"))]
669 #[inline]
670 pub fn new(adapter: A) -> SinglyLinkedList<A> {
671 SinglyLinkedList {
672 head: None,
673 adapter,
674 }
675 }
676
677 /// Creates an empty `SinglyLinkedList`.
678 #[cfg(feature = "nightly")]
679 #[inline]
680 pub const fn new(adapter: A) -> SinglyLinkedList<A> {
681 SinglyLinkedList {
682 head: None,
683 adapter,
684 }
685 }
686
687 /// Returns `true` if the `SinglyLinkedList` is empty.
688 #[inline]
689 pub fn is_empty(&self) -> bool {
690 self.head.is_none()
691 }
692
693 /// Returns a null `Cursor` for this list.
694 #[inline]
695 pub fn cursor(&self) -> Cursor<'_, A> {
696 Cursor {
697 current: None,
698 list: self,
699 }
700 }
701
702 /// Returns a null `CursorMut` for this list.
703 #[inline]
704 pub fn cursor_mut(&mut self) -> CursorMut<'_, A> {
705 CursorMut {
706 current: None,
707 list: self,
708 }
709 }
710
711 /// Creates a `Cursor` from a pointer to an element.
712 ///
713 /// # Safety
714 ///
715 /// `ptr` must be a pointer to an object that is part of this list.
716 #[inline]
717 pub unsafe fn cursor_from_ptr(
718 &self,
719 ptr: *const <A::PointerOps as PointerOps>::Value,
720 ) -> Cursor<'_, A> {
721 Cursor {
722 current: Some(self.adapter.get_link(ptr)),
723 list: self,
724 }
725 }
726
727 /// Creates a `CursorMut` from a pointer to an element.
728 ///
729 /// # Safety
730 ///
731 /// `ptr` must be a pointer to an object that is part of this list.
732 #[inline]
733 pub unsafe fn cursor_mut_from_ptr(
734 &mut self,
735 ptr: *const <A::PointerOps as PointerOps>::Value,
736 ) -> CursorMut<'_, A> {
737 CursorMut {
738 current: Some(self.adapter.get_link(ptr)),
739 list: self,
740 }
741 }
742
743 /// Returns a `Cursor` pointing to the first element of the list. If the
744 /// list is empty then a null cursor is returned.
745 #[inline]
746 pub fn front(&self) -> Cursor<'_, A> {
747 let mut cursor = self.cursor();
748 cursor.move_next();
749 cursor
750 }
751
752 /// Returns a `CursorMut` pointing to the first element of the list. If the
753 /// the list is empty then a null cursor is returned.
754 #[inline]
755 pub fn front_mut(&mut self) -> CursorMut<'_, A> {
756 let mut cursor = self.cursor_mut();
757 cursor.move_next();
758 cursor
759 }
760
761 /// Gets an iterator over the objects in the `SinglyLinkedList`.
762 #[inline]
763 pub fn iter(&self) -> Iter<'_, A> {
764 Iter {
765 current: self.head,
766 list: self,
767 }
768 }
769
770 /// Removes all elements from the `SinglyLinkedList`.
771 ///
772 /// This will unlink all object currently in the list, which requires
773 /// iterating through all elements in the `SinglyLinkedList`. Each element is
774 /// converted back to an owned pointer and then dropped.
775 #[inline]
776 pub fn clear(&mut self) {
777 use link_ops::LinkOps;
778
779 let mut current = self.head;
780 self.head = None;
781 while let Some(x) = current {
782 unsafe {
783 let next = self.adapter.link_ops().next(x);
784 self.adapter.link_ops_mut().release_link(x);
785 self.adapter
786 .pointer_ops()
787 .from_raw(self.adapter.get_value(x));
788 current = next;
789 }
790 }
791 }
792
793 /// Empties the `SinglyLinkedList` without unlinking or freeing objects in it.
794 ///
795 /// Since this does not unlink any objects, any attempts to link these
796 /// objects into another `SinglyLinkedList` will fail but will not cause any
797 /// memory unsafety. To unlink those objects manually, you must call the
798 /// `force_unlink` function on them.
799 #[inline]
800 pub fn fast_clear(&mut self) {
801 self.head = None;
802 }
803
804 /// Takes all the elements out of the `SinglyLinkedList`, leaving it empty.
805 /// The taken elements are returned as a new `SinglyLinkedList`.
806 #[inline]
807 pub fn take(&mut self) -> SinglyLinkedList<A>
808 where
809 A: Clone,
810 {
811 let list = SinglyLinkedList {
812 head: self.head,
813 adapter: self.adapter.clone(),
814 };
815 self.head = None;
816 list
817 }
818
819 /// Inserts a new element at the start of the `SinglyLinkedList`.
820 #[inline]
821 pub fn push_front(&mut self, val: <A::PointerOps as PointerOps>::Pointer) {
822 self.cursor_mut().insert_after(val);
823 }
824
825 /// Removes the first element of the `SinglyLinkedList`.
826 ///
827 /// This returns `None` if the `SinglyLinkedList` is empty.
828 #[inline]
829 pub fn pop_front(&mut self) -> Option<<A::PointerOps as PointerOps>::Pointer> {
830 self.cursor_mut().remove_next()
831 }
832 }
833
834 // Allow read-only access to values from multiple threads
835 unsafe impl<A: Adapter + Sync> Sync for SinglyLinkedList<A>
836 where
837 <A::PointerOps as PointerOps>::Value: Sync,
838 A::LinkOps: SinglyLinkedListOps,
839 {
840 }
841
842 // Allow sending to another thread if the ownership (represented by the <A::PointerOps as PointerOps>::Pointer owned
843 // pointer type) can be transferred to another thread.
844 unsafe impl<A: Adapter + Send> Send for SinglyLinkedList<A>
845 where
846 <A::PointerOps as PointerOps>::Pointer: Send,
847 A::LinkOps: SinglyLinkedListOps,
848 {
849 }
850
851 // Drop all owned pointers if the collection is dropped
852 impl<A: Adapter> Drop for SinglyLinkedList<A>
853 where
854 A::LinkOps: SinglyLinkedListOps,
855 {
856 #[inline]
857 fn drop(&mut self) {
858 self.clear();
859 }
860 }
861
862 impl<A: Adapter> IntoIterator for SinglyLinkedList<A>
863 where
864 A::LinkOps: SinglyLinkedListOps,
865 {
866 type Item = <A::PointerOps as PointerOps>::Pointer;
867 type IntoIter = IntoIter<A>;
868
869 #[inline]
870 fn into_iter(self) -> IntoIter<A> {
871 IntoIter { list: self }
872 }
873 }
874
875 impl<'a, A: Adapter + 'a> IntoIterator for &'a SinglyLinkedList<A>
876 where
877 A::LinkOps: SinglyLinkedListOps,
878 {
879 type Item = &'a <A::PointerOps as PointerOps>::Value;
880 type IntoIter = Iter<'a, A>;
881
882 #[inline]
883 fn into_iter(self) -> Iter<'a, A> {
884 self.iter()
885 }
886 }
887
888 impl<A: Adapter + Default> Default for SinglyLinkedList<A>
889 where
890 A::LinkOps: SinglyLinkedListOps,
891 {
892 fn default() -> SinglyLinkedList<A> {
893 SinglyLinkedList::new(A::default())
894 }
895 }
896
897 impl<A: Adapter> fmt::Debug for SinglyLinkedList<A>
898 where
899 A::LinkOps: SinglyLinkedListOps,
900 <A::PointerOps as PointerOps>::Value: fmt::Debug,
901 {
902 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
903 f.debug_list().entries(self.iter()).finish()
904 }
905 }
906
907 // =============================================================================
908 // Iter
909 // =============================================================================
910
911 /// An iterator over references to the items of a `SinglyLinkedList`.
912 pub struct Iter<'a, A: Adapter>
913 where
914 A::LinkOps: SinglyLinkedListOps,
915 {
916 current: Option<<A::LinkOps as link_ops::LinkOps>::LinkPtr>,
917 list: &'a SinglyLinkedList<A>,
918 }
919 impl<'a, A: Adapter + 'a> Iterator for Iter<'a, A>
920 where
921 A::LinkOps: SinglyLinkedListOps,
922 {
923 type Item = &'a <A::PointerOps as PointerOps>::Value;
924
925 #[inline]
926 fn next(&mut self) -> Option<&'a <A::PointerOps as PointerOps>::Value> {
927 let current = self.current?;
928
929 self.current = unsafe { self.list.adapter.link_ops().next(current) };
930 Some(unsafe { &*self.list.adapter.get_value(current) })
931 }
932 }
933 impl<'a, A: Adapter + 'a> Clone for Iter<'a, A>
934 where
935 A::LinkOps: SinglyLinkedListOps,
936 {
937 #[inline]
938 fn clone(&self) -> Iter<'a, A> {
939 Iter {
940 current: self.current,
941 list: self.list,
942 }
943 }
944 }
945
946 // =============================================================================
947 // IntoIter
948 // =============================================================================
949
950 /// An iterator which consumes a `SinglyLinkedList`.
951 pub struct IntoIter<A: Adapter>
952 where
953 A::LinkOps: SinglyLinkedListOps,
954 {
955 list: SinglyLinkedList<A>,
956 }
957 impl<A: Adapter> Iterator for IntoIter<A>
958 where
959 A::LinkOps: SinglyLinkedListOps,
960 {
961 type Item = <A::PointerOps as PointerOps>::Pointer;
962
963 #[inline]
964 fn next(&mut self) -> Option<<A::PointerOps as PointerOps>::Pointer> {
965 self.list.pop_front()
966 }
967 }
968
969 // =============================================================================
970 // Tests
971 // =============================================================================
972
973 #[cfg(test)]
974 mod tests {
975 use super::{Link, SinglyLinkedList};
976 use std::fmt;
977 use std::format;
978 use std::rc::Rc;
979 use std::vec::Vec;
980
981 struct Obj {
982 link1: Link,
983 link2: Link,
984 value: u32,
985 }
986 impl fmt::Debug for Obj {
987 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
988 write!(f, "{}", self.value)
989 }
990 }
991 intrusive_adapter!(ObjAdapter1 = Rc<Obj>: Obj { link1: Link });
992 intrusive_adapter!(ObjAdapter2 = Rc<Obj>: Obj { link2: Link });
993 fn make_obj(value: u32) -> Rc<Obj> {
994 Rc::new(Obj {
995 link1: Link::new(),
996 link2: Link::default(),
997 value,
998 })
999 }
1000
1001 #[test]
1002 fn test_link() {
1003 let a = make_obj(1);
1004 assert!(!a.link1.is_linked());
1005 assert!(!a.link2.is_linked());
1006
1007 let mut b = SinglyLinkedList::<ObjAdapter1>::default();
1008 assert!(b.is_empty());
1009
1010 b.push_front(a.clone());
1011 assert!(!b.is_empty());
1012 assert!(a.link1.is_linked());
1013 assert!(!a.link2.is_linked());
1014 assert_eq!(format!("{:?}", a.link1), "linked");
1015 assert_eq!(format!("{:?}", a.link2), "unlinked");
1016
1017 assert_eq!(
1018 b.pop_front().unwrap().as_ref() as *const _,
1019 a.as_ref() as *const _
1020 );
1021 assert!(b.is_empty());
1022 assert!(!a.link1.is_linked());
1023 assert!(!a.link2.is_linked());
1024 }
1025
1026 #[test]
1027 fn test_cursor() {
1028 let a = make_obj(1);
1029 let b = make_obj(2);
1030 let c = make_obj(3);
1031
1032 let mut l = SinglyLinkedList::new(ObjAdapter1::new());
1033 let mut cur = l.cursor_mut();
1034 assert!(cur.is_null());
1035 assert!(cur.get().is_none());
1036 assert!(cur.remove_next().is_none());
1037 assert_eq!(
1038 cur.replace_next_with(a.clone()).unwrap_err().as_ref() as *const _,
1039 a.as_ref() as *const _
1040 );
1041
1042 cur.insert_after(c.clone());
1043 cur.insert_after(a.clone());
1044 cur.move_next();
1045 cur.insert_after(b.clone());
1046 cur.move_next();
1047 cur.move_next();
1048 assert!(cur.peek_next().is_null());
1049 cur.move_next();
1050 assert!(cur.is_null());
1051
1052 cur.move_next();
1053 assert!(!cur.is_null());
1054 assert_eq!(cur.get().unwrap() as *const _, a.as_ref() as *const _);
1055
1056 {
1057 let mut cur2 = cur.as_cursor();
1058 assert_eq!(cur2.get().unwrap() as *const _, a.as_ref() as *const _);
1059 assert_eq!(cur2.peek_next().get().unwrap().value, 2);
1060 cur2.move_next();
1061 assert_eq!(cur2.get().unwrap().value, 2);
1062 cur2.move_next();
1063 assert_eq!(cur2.get().unwrap() as *const _, c.as_ref() as *const _);
1064 cur2.move_next();
1065 assert!(cur2.is_null());
1066 assert!(cur2.clone().get().is_none());
1067 }
1068 assert_eq!(cur.get().unwrap() as *const _, a.as_ref() as *const _);
1069
1070 assert_eq!(
1071 cur.remove_next().unwrap().as_ref() as *const _,
1072 b.as_ref() as *const _
1073 );
1074 assert_eq!(cur.get().unwrap() as *const _, a.as_ref() as *const _);
1075 cur.insert_after(b.clone());
1076 assert_eq!(cur.get().unwrap() as *const _, a.as_ref() as *const _);
1077 cur.move_next();
1078 assert_eq!(cur.get().unwrap() as *const _, b.as_ref() as *const _);
1079 assert_eq!(
1080 cur.remove_next().unwrap().as_ref() as *const _,
1081 c.as_ref() as *const _
1082 );
1083 assert!(!c.link1.is_linked());
1084 assert!(a.link1.is_linked());
1085 assert_eq!(cur.get().unwrap() as *const _, b.as_ref() as *const _);
1086 cur.move_next();
1087 assert!(cur.is_null());
1088 assert_eq!(
1089 cur.replace_next_with(c.clone()).unwrap().as_ref() as *const _,
1090 a.as_ref() as *const _
1091 );
1092 assert!(!a.link1.is_linked());
1093 assert!(c.link1.is_linked());
1094 assert!(cur.is_null());
1095 cur.move_next();
1096 assert_eq!(cur.get().unwrap() as *const _, c.as_ref() as *const _);
1097 assert_eq!(
1098 cur.replace_next_with(a.clone()).unwrap().as_ref() as *const _,
1099 b.as_ref() as *const _
1100 );
1101 assert!(a.link1.is_linked());
1102 assert!(!b.link1.is_linked());
1103 assert!(c.link1.is_linked());
1104 assert_eq!(cur.get().unwrap() as *const _, c.as_ref() as *const _);
1105 }
1106
1107 #[test]
1108 fn test_split_splice() {
1109 let mut l1 = SinglyLinkedList::new(ObjAdapter1::new());
1110 let mut l2 = SinglyLinkedList::new(ObjAdapter1::new());
1111 let mut l3 = SinglyLinkedList::new(ObjAdapter1::new());
1112
1113 let a = make_obj(1);
1114 let b = make_obj(2);
1115 let c = make_obj(3);
1116 let d = make_obj(4);
1117 l1.cursor_mut().insert_after(d.clone());
1118 l1.cursor_mut().insert_after(c.clone());
1119 l1.cursor_mut().insert_after(b.clone());
1120 l1.cursor_mut().insert_after(a.clone());
1121 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), [1, 2, 3, 4]);
1122 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1123 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1124 {
1125 let mut cur = l1.front_mut();
1126 cur.move_next();
1127 l2 = cur.split_after();
1128 }
1129 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), [1, 2]);
1130 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), [3, 4]);
1131 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1132 {
1133 let mut cur = l2.front_mut();
1134 l3 = cur.split_after();
1135 }
1136 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), [1, 2]);
1137 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), [3]);
1138 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), [4]);
1139 {
1140 let mut cur = l1.front_mut();
1141 cur.splice_after(l2.take());
1142 }
1143 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), [1, 3, 2]);
1144 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1145 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), [4]);
1146 {
1147 let mut cur = l1.cursor_mut();
1148 cur.splice_after(l3.take());
1149 }
1150 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), [4, 1, 3, 2]);
1151 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1152 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1153 {
1154 let mut cur = l1.cursor_mut();
1155 l2 = cur.split_after();
1156 }
1157 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1158 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), [4, 1, 3, 2]);
1159 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1160 {
1161 let mut cur = l2.front_mut();
1162 cur.move_next();
1163 l3 = cur.split_after();
1164 }
1165 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1166 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), [4, 1]);
1167 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), [3, 2]);
1168 {
1169 let mut cur = l2.front_mut();
1170 cur.splice_after(l3.take());
1171 }
1172 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1173 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), [4, 3, 2, 1]);
1174 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1175 {
1176 let mut cur = l3.cursor_mut();
1177 cur.splice_after(l2.take());
1178 }
1179 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1180 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1181 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), [4, 3, 2, 1]);
1182 {
1183 let mut cur = l3.front_mut();
1184 cur.move_next();
1185 l2 = cur.split_after();
1186 }
1187 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1188 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), [2, 1]);
1189 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), [4, 3]);
1190 {
1191 let mut cur = l2.front_mut();
1192 cur.move_next();
1193 cur.splice_after(l3.take());
1194 }
1195 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1196 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), [2, 1, 4, 3]);
1197 assert_eq!(l3.iter().map(|x| x.value).collect::<Vec<_>>(), []);
1198 }
1199
1200 #[test]
1201 fn test_iter() {
1202 let mut l = SinglyLinkedList::new(ObjAdapter1::new());
1203 let a = make_obj(1);
1204 let b = make_obj(2);
1205 let c = make_obj(3);
1206 let d = make_obj(4);
1207 l.cursor_mut().insert_after(d.clone());
1208 l.cursor_mut().insert_after(c.clone());
1209 l.cursor_mut().insert_after(b.clone());
1210 l.cursor_mut().insert_after(a.clone());
1211
1212 assert_eq!(l.front().get().unwrap().value, 1);
1213 unsafe {
1214 assert_eq!(l.cursor_from_ptr(b.as_ref()).get().unwrap().value, 2);
1215 assert_eq!(l.cursor_mut_from_ptr(c.as_ref()).get().unwrap().value, 3);
1216 }
1217
1218 let mut v = Vec::new();
1219 for x in &l {
1220 v.push(x.value);
1221 }
1222 assert_eq!(v, [1, 2, 3, 4]);
1223 assert_eq!(
1224 l.iter().clone().map(|x| x.value).collect::<Vec<_>>(),
1225 [1, 2, 3, 4]
1226 );
1227 assert_eq!(l.iter().map(|x| x.value).collect::<Vec<_>>(), [1, 2, 3, 4]);
1228
1229 assert_eq!(format!("{:?}", l), "[1, 2, 3, 4]");
1230
1231 let mut v = Vec::new();
1232 for x in l.take() {
1233 v.push(x.value);
1234 }
1235 assert_eq!(v, [1, 2, 3, 4]);
1236 assert!(l.is_empty());
1237 assert!(!a.link1.is_linked());
1238 assert!(!b.link1.is_linked());
1239 assert!(!c.link1.is_linked());
1240 assert!(!d.link1.is_linked());
1241
1242 l.cursor_mut().insert_after(d.clone());
1243 l.cursor_mut().insert_after(c.clone());
1244 l.cursor_mut().insert_after(b.clone());
1245 l.cursor_mut().insert_after(a.clone());
1246 l.clear();
1247 assert!(l.is_empty());
1248 assert!(!a.link1.is_linked());
1249 assert!(!b.link1.is_linked());
1250 assert!(!c.link1.is_linked());
1251 assert!(!d.link1.is_linked());
1252 }
1253
1254 #[test]
1255 fn test_multi_list() {
1256 let mut l1 = SinglyLinkedList::new(ObjAdapter1::new());
1257 let mut l2 = SinglyLinkedList::new(ObjAdapter2::new());
1258 let a = make_obj(1);
1259 let b = make_obj(2);
1260 let c = make_obj(3);
1261 let d = make_obj(4);
1262 l1.cursor_mut().insert_after(d.clone());
1263 l1.cursor_mut().insert_after(c.clone());
1264 l1.cursor_mut().insert_after(b.clone());
1265 l1.cursor_mut().insert_after(a.clone());
1266 l2.cursor_mut().insert_after(a.clone());
1267 l2.cursor_mut().insert_after(b.clone());
1268 l2.cursor_mut().insert_after(c.clone());
1269 l2.cursor_mut().insert_after(d.clone());
1270 assert_eq!(l1.iter().map(|x| x.value).collect::<Vec<_>>(), [1, 2, 3, 4]);
1271 assert_eq!(l2.iter().map(|x| x.value).collect::<Vec<_>>(), [4, 3, 2, 1]);
1272 }
1273
1274 #[test]
1275 fn test_fast_clear() {
1276 let mut l = SinglyLinkedList::new(ObjAdapter1::new());
1277 let a = make_obj(1);
1278 let b = make_obj(2);
1279 let c = make_obj(3);
1280 l.cursor_mut().insert_after(a.clone());
1281 l.cursor_mut().insert_after(b.clone());
1282 l.cursor_mut().insert_after(c.clone());
1283
1284 l.fast_clear();
1285 assert!(l.is_empty());
1286 assert!(a.link1.is_linked());
1287 assert!(b.link1.is_linked());
1288 assert!(c.link1.is_linked());
1289 unsafe {
1290 a.link1.force_unlink();
1291 b.link1.force_unlink();
1292 c.link1.force_unlink();
1293 }
1294 assert!(l.is_empty());
1295 assert!(!a.link1.is_linked());
1296 assert!(!b.link1.is_linked());
1297 assert!(!c.link1.is_linked());
1298 }
1299
1300 #[test]
1301 fn test_non_static() {
1302 #[derive(Clone)]
1303 struct Obj<'a, T> {
1304 link: Link,
1305 value: &'a T,
1306 }
1307 intrusive_adapter!(ObjAdapter<'a, T> = &'a Obj<'a, T>: Obj<'a, T> {link: Link} where T: 'a);
1308
1309 let v = 5;
1310 let a = Obj {
1311 link: Link::new(),
1312 value: &v,
1313 };
1314 let b = a.clone();
1315 let mut l = SinglyLinkedList::new(ObjAdapter::new());
1316 l.cursor_mut().insert_after(&a);
1317 l.cursor_mut().insert_after(&b);
1318 assert_eq!(*l.front().get().unwrap().value, 5);
1319 assert_eq!(*l.front().get().unwrap().value, 5);
1320 }
1321
1322 macro_rules! test_clone_pointer {
1323 ($ptr: ident, $ptr_import: path) => {
1324 use $ptr_import;
1325
1326 #[derive(Clone)]
1327 struct Obj {
1328 link: Link,
1329 value: usize,
1330 }
1331 intrusive_adapter!(ObjAdapter = $ptr<Obj>: Obj { link: Link });
1332
1333 let a = $ptr::new(Obj {
1334 link: Link::new(),
1335 value: 5,
1336 });
1337 let mut l = SinglyLinkedList::new(ObjAdapter::new());
1338 l.cursor_mut().insert_after(a.clone());
1339 assert_eq!(2, $ptr::strong_count(&a));
1340
1341 let pointer = l.front().clone_pointer().unwrap();
1342 assert_eq!(pointer.value, 5);
1343 assert_eq!(3, $ptr::strong_count(&a));
1344
1345 l.clear();
1346 assert!(l.front().clone_pointer().is_none());
1347 };
1348 }
1349
1350 #[test]
1351 fn test_clone_pointer_rc() {
1352 test_clone_pointer!(Rc, std::rc::Rc);
1353 }
1354
1355 #[test]
1356 fn test_clone_pointer_arc() {
1357 test_clone_pointer!(Arc, std::sync::Arc);
1358 }
1359 }
1360