1use core::{fmt, mem::MaybeUninit, ptr::NonNull};
2
3use super::{
4    EntityListItem, EntityMut, EntityParent, EntityRef, EntityWithParent, IntrusiveLink,
5    RawEntityMetadata, RawEntityRef, UnsafeIntrusiveEntityRef,
6};
7
8pub struct EntityList<T> {
9    list: intrusive_collections::linked_list::LinkedList<EntityAdapter<T>>,
10}
11impl<T> Default for EntityList<T> {
12    fn default() -> Self {
13        Self {
14            list: Default::default(),
15        }
16    }
17}
18impl<T> EntityList<T> {
19    pub fn new() -> Self {
21        Self::default()
22    }
23
24    #[inline]
26    pub fn is_empty(&self) -> bool {
27        self.list.is_empty()
28    }
29
30    pub fn len(&self) -> usize {
32        let mut cursor = self.list.front();
33        let mut usize = 0;
34        while !cursor.is_null() {
35            usize += 1;
36            cursor.move_next();
37        }
38        usize
39    }
40
41    #[doc(hidden)]
42    pub fn cursor(&self) -> EntityCursor<'_, T> {
43        EntityCursor {
44            cursor: self.list.cursor(),
45        }
46    }
47
48    pub fn front(&self) -> EntityCursor<'_, T> {
51        EntityCursor {
52            cursor: self.list.front(),
53        }
54    }
55
56    pub fn back(&self) -> EntityCursor<'_, T> {
59        EntityCursor {
60            cursor: self.list.back(),
61        }
62    }
63
64    pub fn iter(&self) -> EntityIter<'_, T> {
69        EntityIter {
70            cursor: self.cursor(),
71            started: false,
72        }
73    }
74
75    pub unsafe fn cursor_from_ptr(&self, ptr: UnsafeIntrusiveEntityRef<T>) -> EntityCursor<'_, T> {
83        unsafe {
84            let raw = UnsafeIntrusiveEntityRef::into_inner(ptr).as_ptr();
85            EntityCursor {
86                cursor: self.list.cursor_from_ptr(raw),
87            }
88        }
89    }
90}
91
92impl<T> EntityList<T>
93where
94    T: EntityWithParent,
95{
96    pub(crate) fn parent(&self) -> UnsafeIntrusiveEntityRef<<T as EntityWithParent>::Parent> {
97        let offset = <<T as EntityWithParent>::Parent as EntityParent<T>>::offset();
98        let ptr = self as *const EntityList<T>;
99        unsafe {
100            let parent = ptr.byte_sub(offset).cast::<<T as EntityWithParent>::Parent>();
101            UnsafeIntrusiveEntityRef::from_raw(parent)
102        }
103    }
104}
105
106trait EntityListTraits<T>: Sized {
107    fn cursor_mut(&mut self) -> EntityCursorMut<'_, T>;
108
109    unsafe fn cursor_mut_from_ptr(
117        &mut self,
118        ptr: UnsafeIntrusiveEntityRef<T>,
119    ) -> EntityCursorMut<'_, T>;
120
121    fn front_mut(&mut self) -> EntityCursorMut<'_, T>;
124
125    fn back_mut(&mut self) -> EntityCursorMut<'_, T>;
128
129    fn remove(cursor: &mut EntityCursorMut<'_, T>) -> Option<UnsafeIntrusiveEntityRef<T>>;
130
131    fn replace_with(
132        cursor: &mut EntityCursorMut<'_, T>,
133        entity: UnsafeIntrusiveEntityRef<T>,
134    ) -> Result<UnsafeIntrusiveEntityRef<T>, UnsafeIntrusiveEntityRef<T>>;
135
136    fn push_front(list: &mut EntityList<T>, entity: UnsafeIntrusiveEntityRef<T>);
138
139    fn push_back(list: &mut EntityList<T>, entity: UnsafeIntrusiveEntityRef<T>);
141
142    fn insert_after(cursor: &mut EntityCursorMut<'_, T>, entity: UnsafeIntrusiveEntityRef<T>);
143
144    fn insert_before(cursor: &mut EntityCursorMut<'_, T>, entity: UnsafeIntrusiveEntityRef<T>);
145
146    fn splice_after(cursor: &mut EntityCursorMut<'_, T>, list: EntityList<T>);
166
167    fn splice_before(cursor: &mut EntityCursorMut<'_, T>, list: EntityList<T>);
187
188    fn split_after(cursor: &mut EntityCursorMut<'_, T>) -> EntityList<T>;
196
197    fn split_before(cursor: &mut EntityCursorMut<'_, T>) -> EntityList<T>;
205
206    fn pop_front(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>>;
210
211    fn pop_back(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>>;
215
216    fn clear(&mut self);
222
223    fn take(&mut self) -> Self;
227}
228
229impl<T: EntityListItem> EntityListTraits<T> for EntityList<T> {
230    default fn cursor_mut(&mut self) -> EntityCursorMut<'_, T> {
231        EntityCursorMut {
232            cursor: self.list.cursor_mut(),
233            parent: core::ptr::null(),
234        }
235    }
236
237    default unsafe fn cursor_mut_from_ptr(
238        &mut self,
239        ptr: UnsafeIntrusiveEntityRef<T>,
240    ) -> EntityCursorMut<'_, T> {
241        let raw = UnsafeIntrusiveEntityRef::into_inner(ptr).as_ptr();
242        unsafe {
243            EntityCursorMut {
244                cursor: self.list.cursor_mut_from_ptr(raw),
245                parent: core::ptr::null(),
246            }
247        }
248    }
249
250    default fn front_mut(&mut self) -> EntityCursorMut<'_, T> {
253        EntityCursorMut {
254            cursor: self.list.front_mut(),
255            parent: core::ptr::null(),
256        }
257    }
258
259    default fn back_mut(&mut self) -> EntityCursorMut<'_, T> {
262        EntityCursorMut {
263            cursor: self.list.back_mut(),
264            parent: core::ptr::null(),
265        }
266    }
267
268    default fn remove(cursor: &mut EntityCursorMut<'_, T>) -> Option<UnsafeIntrusiveEntityRef<T>> {
269        let entity = cursor.cursor.remove()?;
270        <T as EntityListItem>::on_removed(entity, cursor);
271        Some(entity)
272    }
273
274    default fn replace_with(
275        cursor: &mut EntityCursorMut<'_, T>,
276        entity: UnsafeIntrusiveEntityRef<T>,
277    ) -> Result<UnsafeIntrusiveEntityRef<T>, UnsafeIntrusiveEntityRef<T>> {
278        let removed = cursor.cursor.replace_with(entity)?;
279        <T as EntityListItem>::on_removed(removed, cursor);
280        <T as EntityListItem>::on_inserted(entity, cursor);
281        Ok(removed)
282    }
283
284    default fn push_front(list: &mut EntityList<T>, entity: UnsafeIntrusiveEntityRef<T>) {
285        list.list.push_front(entity);
286        <T as EntityListItem>::on_inserted(entity, &mut list.front_mut());
287    }
288
289    default fn push_back(list: &mut EntityList<T>, entity: UnsafeIntrusiveEntityRef<T>) {
290        list.list.push_back(entity);
291        <T as EntityListItem>::on_inserted(entity, &mut list.back_mut());
292    }
293
294    default fn insert_after(
295        cursor: &mut EntityCursorMut<'_, T>,
296        entity: UnsafeIntrusiveEntityRef<T>,
297    ) {
298        cursor.cursor.insert_after(entity);
299        <T as EntityListItem>::on_inserted(entity, cursor);
300    }
301
302    default fn insert_before(
303        cursor: &mut EntityCursorMut<'_, T>,
304        entity: UnsafeIntrusiveEntityRef<T>,
305    ) {
306        cursor.cursor.insert_before(entity);
307        <T as EntityListItem>::on_inserted(entity, cursor);
308    }
309
310    default fn splice_after(cursor: &mut EntityCursorMut<'_, T>, mut list: EntityList<T>) {
311        while let Some(entity) = list.list.pop_back() {
312            Self::insert_after(cursor, entity)
313        }
314    }
315
316    default fn splice_before(cursor: &mut EntityCursorMut<'_, T>, mut list: EntityList<T>) {
317        while let Some(entity) = list.list.pop_front() {
318            Self::insert_before(cursor, entity)
319        }
320    }
321
322    default fn split_after(cursor: &mut EntityCursorMut<'_, T>) -> EntityList<T> {
323        let list = cursor.cursor.split_after();
324        let mut list_cursor = list.front();
325        while let Some(entity) = list_cursor.clone_pointer() {
326            <T as EntityListItem>::on_removed(entity, cursor);
327            list_cursor.move_next();
328        }
329        Self { list }
330    }
331
332    default fn split_before(cursor: &mut EntityCursorMut<'_, T>) -> EntityList<T> {
333        let list = cursor.cursor.split_before();
334        let mut list_cursor = list.front();
335        while let Some(entity) = list_cursor.clone_pointer() {
336            <T as EntityListItem>::on_removed(entity, cursor);
337            list_cursor.move_next();
338        }
339        Self { list }
340    }
341
342    default fn pop_front(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>> {
343        let removed = self.list.pop_front()?;
344        <T as EntityListItem>::on_removed(removed, &mut self.front_mut());
345        Some(removed)
346    }
347
348    default fn pop_back(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>> {
349        let removed = self.list.pop_back()?;
350        <T as EntityListItem>::on_removed(removed, &mut self.back_mut());
351        Some(removed)
352    }
353
354    default fn clear(&mut self) {
355        while self.pop_front().is_some() {}
356    }
357
358    default fn take(&mut self) -> Self {
359        let list = self.list.take();
360        let mut list_cursor = list.front();
361        while let Some(entity) = list_cursor.clone_pointer() {
362            <T as EntityListItem>::on_removed(entity, &mut self.front_mut());
363            list_cursor.move_next();
364        }
365        Self { list }
366    }
367}
368
369impl<T> EntityListTraits<T> for EntityList<T>
370where
371    T: EntityListItem + EntityWithParent,
372    <T as EntityWithParent>::Parent: EntityParent<T>,
373{
374    fn cursor_mut(&mut self) -> EntityCursorMut<'_, T> {
375        let parent = UnsafeIntrusiveEntityRef::into_raw(self.parent()).cast();
376        EntityCursorMut {
377            cursor: self.list.cursor_mut(),
378            parent,
379        }
380    }
381
382    unsafe fn cursor_mut_from_ptr(
383        &mut self,
384        ptr: UnsafeIntrusiveEntityRef<T>,
385    ) -> EntityCursorMut<'_, T> {
386        let parent = UnsafeIntrusiveEntityRef::into_raw(self.parent()).cast();
387        let raw = UnsafeIntrusiveEntityRef::into_inner(ptr).as_ptr();
388        EntityCursorMut {
389            cursor: self.list.cursor_mut_from_ptr(raw),
390            parent,
391        }
392    }
393
394    fn back_mut(&mut self) -> EntityCursorMut<'_, T> {
395        let parent = UnsafeIntrusiveEntityRef::into_raw(self.parent()).cast();
396        EntityCursorMut {
397            cursor: self.list.back_mut(),
398            parent,
399        }
400    }
401
402    fn front_mut(&mut self) -> EntityCursorMut<'_, T> {
403        let parent = UnsafeIntrusiveEntityRef::into_raw(self.parent()).cast();
404        EntityCursorMut {
405            cursor: self.list.front_mut(),
406            parent,
407        }
408    }
409
410    #[track_caller]
411    fn remove(cursor: &mut EntityCursorMut<'_, T>) -> Option<UnsafeIntrusiveEntityRef<T>> {
412        let entity = cursor.cursor.remove()?;
413        entity.set_parent(None);
414        <T as EntityListItem>::on_removed(entity, cursor);
415        Some(entity)
416    }
417
418    fn replace_with(
419        cursor: &mut EntityCursorMut<'_, T>,
420        entity: UnsafeIntrusiveEntityRef<T>,
421    ) -> Result<UnsafeIntrusiveEntityRef<T>, UnsafeIntrusiveEntityRef<T>> {
422        let removed = cursor.cursor.replace_with(entity)?;
423        removed.set_parent(None);
424        <T as EntityListItem>::on_removed(removed, cursor);
425        let parent = cursor.parent().expect("cannot insert items in an orphaned entity list");
426        entity.set_parent(Some(parent));
427        <T as EntityListItem>::on_inserted(entity, cursor);
428        Ok(removed)
429    }
430
431    fn push_front(list: &mut EntityList<T>, entity: UnsafeIntrusiveEntityRef<T>) {
432        let parent = list.parent();
433        list.list.push_front(entity);
434        entity.set_parent(Some(parent));
435        <T as EntityListItem>::on_inserted(entity, &mut list.front_mut());
436    }
437
438    fn push_back(list: &mut EntityList<T>, entity: UnsafeIntrusiveEntityRef<T>) {
439        let parent = list.parent();
440        list.list.push_back(entity);
441        entity.set_parent(Some(parent));
442        <T as EntityListItem>::on_inserted(entity, &mut list.back_mut());
443    }
444
445    fn insert_after(cursor: &mut EntityCursorMut<'_, T>, entity: UnsafeIntrusiveEntityRef<T>) {
446        cursor.cursor.insert_after(entity);
447        let parent = cursor.parent().expect("cannot insert items in an orphaned entity list");
448        entity.set_parent(Some(parent));
449        <T as EntityListItem>::on_inserted(entity, cursor);
450    }
451
452    fn insert_before(cursor: &mut EntityCursorMut<'_, T>, entity: UnsafeIntrusiveEntityRef<T>) {
453        cursor.cursor.insert_before(entity);
454        let parent = cursor.parent().expect("cannot insert items in an orphaned entity list");
455        entity.set_parent(Some(parent));
456        <T as EntityListItem>::on_inserted(entity, cursor);
457    }
458
459    fn splice_after(cursor: &mut EntityCursorMut<'_, T>, mut list: EntityList<T>) {
460        let parent = cursor.parent().expect("cannot insert items in an orphaned entity list");
461        while let Some(entity) = list.list.pop_back() {
462            cursor.cursor.insert_after(entity);
463            entity.set_parent(Some(parent));
464            <T as EntityListItem>::on_inserted(entity, cursor);
465        }
466    }
467
468    fn splice_before(cursor: &mut EntityCursorMut<'_, T>, mut list: EntityList<T>) {
469        let parent = cursor.parent().expect("cannot insert items in an orphaned entity list");
470        while let Some(entity) = list.list.pop_front() {
471            cursor.cursor.insert_before(entity);
472            entity.set_parent(Some(parent));
473            <T as EntityListItem>::on_inserted(entity, cursor);
474        }
475    }
476
477    fn split_after(cursor: &mut EntityCursorMut<'_, T>) -> EntityList<T> {
478        let list = cursor.cursor.split_after();
479        let mut list_cursor = list.front();
480        while let Some(entity) = list_cursor.clone_pointer() {
481            entity.set_parent(None);
482            <T as EntityListItem>::on_removed(entity, cursor);
483            list_cursor.move_next();
484        }
485        Self { list }
486    }
487
488    fn split_before(cursor: &mut EntityCursorMut<'_, T>) -> EntityList<T> {
489        let list = cursor.cursor.split_before();
490        let mut list_cursor = list.front();
491        while let Some(entity) = list_cursor.clone_pointer() {
492            entity.set_parent(None);
493            <T as EntityListItem>::on_removed(entity, cursor);
494            list_cursor.move_next();
495        }
496        Self { list }
497    }
498
499    fn pop_front(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>> {
500        let removed = self.list.pop_front()?;
501        removed.set_parent(None);
502        <T as EntityListItem>::on_removed(removed, &mut self.front_mut());
503        Some(removed)
504    }
505
506    fn pop_back(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>> {
507        let removed = self.list.pop_back()?;
508        removed.set_parent(None);
509        <T as EntityListItem>::on_removed(removed, &mut self.back_mut());
510        Some(removed)
511    }
512
513    fn clear(&mut self) {
514        while self.pop_front().is_some() {}
515    }
516
517    #[track_caller]
518    fn take(&mut self) -> Self {
519        let list = self.list.take();
520        let mut list_cursor = list.front();
521        while let Some(entity) = list_cursor.clone_pointer() {
522            entity.set_parent(None);
523            <T as EntityListItem>::on_removed(entity, &mut self.front_mut());
524            list_cursor.move_next();
525        }
526        Self { list }
527    }
528}
529
530impl<T: EntityListItem> EntityList<T> {
531    #[doc(hidden)]
532    pub fn cursor_mut(&mut self) -> EntityCursorMut<'_, T> {
533        <Self as EntityListTraits<T>>::cursor_mut(self)
534    }
535
536    pub unsafe fn cursor_mut_from_ptr(
544        &mut self,
545        ptr: UnsafeIntrusiveEntityRef<T>,
546    ) -> EntityCursorMut<'_, T> {
547        <Self as EntityListTraits<T>>::cursor_mut_from_ptr(self, ptr)
548    }
549
550    pub fn front_mut(&mut self) -> EntityCursorMut<'_, T> {
553        <Self as EntityListTraits<T>>::front_mut(self)
554    }
555
556    pub fn back_mut(&mut self) -> EntityCursorMut<'_, T> {
559        <Self as EntityListTraits<T>>::back_mut(self)
560    }
561
562    pub fn push_front(&mut self, entity: UnsafeIntrusiveEntityRef<T>) {
564        <Self as EntityListTraits<T>>::push_front(self, entity)
565    }
566
567    pub fn push_back(&mut self, entity: UnsafeIntrusiveEntityRef<T>) {
569        <Self as EntityListTraits<T>>::push_back(self, entity)
570    }
571
572    pub fn pop_front(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>> {
576        <Self as EntityListTraits<T>>::pop_front(self)
577    }
578
579    pub fn pop_back(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>> {
583        <Self as EntityListTraits<T>>::pop_back(self)
584    }
585
586    pub fn clear(&mut self) {
592        <Self as EntityListTraits<T>>::clear(self)
593    }
594
595    pub fn fast_clear(&mut self) {
601        self.list.fast_clear();
602    }
603
604    #[track_caller]
608    pub fn take(&mut self) -> Self {
609        <Self as EntityListTraits<T>>::take(self)
610    }
611}
612
613impl<T: fmt::Debug> fmt::Debug for EntityList<T> {
614    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
615        let mut builder = f.debug_list();
616        for entity in self.iter() {
617            builder.entry(&entity);
618        }
619        builder.finish()
620    }
621}
622
623impl<T: EntityListItem> FromIterator<UnsafeIntrusiveEntityRef<T>> for EntityList<T> {
624    fn from_iter<I>(iter: I) -> Self
625    where
626        I: IntoIterator<Item = UnsafeIntrusiveEntityRef<T>>,
627    {
628        let mut list = EntityList::<T>::default();
629        for handle in iter {
630            list.push_back(handle);
631        }
632        list
633    }
634}
635
636impl<T> IntoIterator for EntityList<T> {
637    type IntoIter = intrusive_collections::linked_list::IntoIter<EntityAdapter<T>>;
638    type Item = UnsafeIntrusiveEntityRef<T>;
639
640    fn into_iter(self) -> Self::IntoIter {
641        self.list.into_iter()
642    }
643}
644
645impl<'a, T> IntoIterator for &'a EntityList<T> {
646    type IntoIter = EntityIter<'a, T>;
647    type Item = EntityRef<'a, T>;
648
649    fn into_iter(self) -> Self::IntoIter {
650        self.iter()
651    }
652}
653
654pub struct EntityCursor<'a, T> {
656    cursor: intrusive_collections::linked_list::Cursor<'a, EntityAdapter<T>>,
657}
658impl<'a, T> EntityCursor<'a, T> {
659    #[inline]
661    pub fn is_null(&self) -> bool {
662        self.cursor.is_null()
663    }
664
665    #[track_caller]
672    pub fn get(&self) -> Option<EntityRef<'a, T>> {
673        Some(self.cursor.get()?.entity.borrow())
674    }
675
676    #[inline]
680    pub fn as_pointer(&self) -> Option<UnsafeIntrusiveEntityRef<T>> {
681        self.cursor.clone_pointer()
682    }
683
684    #[inline]
686    #[track_caller]
687    pub fn into_borrow(self) -> Option<EntityRef<'a, T>> {
688        Some(self.cursor.get()?.borrow())
689    }
690
691    #[inline]
697    pub fn move_next(&mut self) {
698        self.cursor.move_next();
699    }
700
701    #[inline]
707    pub fn move_prev(&mut self) {
708        self.cursor.move_prev();
709    }
710
711    #[inline]
717    pub fn peek_next(&self) -> EntityCursor<'_, T> {
718        EntityCursor {
719            cursor: self.cursor.peek_next(),
720        }
721    }
722
723    #[inline]
729    pub fn peek_prev(&self) -> EntityCursor<'_, T> {
730        EntityCursor {
731            cursor: self.cursor.peek_prev(),
732        }
733    }
734}
735
736pub struct EntityCursorMut<'a, T> {
738    cursor: intrusive_collections::linked_list::CursorMut<'a, EntityAdapter<T>>,
739    parent: *const (),
740}
741
742impl<T> EntityCursorMut<'_, T>
743where
744    T: EntityWithParent,
745{
746    fn parent(&self) -> Option<UnsafeIntrusiveEntityRef<<T as EntityWithParent>::Parent>> {
747        if self.parent.is_null() {
748            None
749        } else {
750            Some(unsafe { UnsafeIntrusiveEntityRef::from_raw(self.parent.cast()) })
751        }
752    }
753}
754
755impl<'a, T: EntityListItem> EntityCursorMut<'a, T> {
756    #[inline]
758    pub fn is_null(&self) -> bool {
759        self.cursor.is_null()
760    }
761
762    pub fn get(&self) -> Option<EntityRef<'_, T>> {
770        self.cursor.get().map(|obj| obj.entity.borrow())
771    }
772
773    pub fn get_mut(&mut self) -> Option<EntityMut<'_, T>> {
782        self.cursor.get().map(|obj| obj.entity.borrow_mut())
783    }
784
785    pub fn as_cursor(&self) -> EntityCursor<'_, T> {
791        EntityCursor {
792            cursor: self.cursor.as_cursor(),
793        }
794    }
795
796    #[inline]
800    pub fn as_pointer(&self) -> Option<UnsafeIntrusiveEntityRef<T>> {
801        self.cursor.as_cursor().clone_pointer()
802    }
803
804    #[inline]
806    pub fn into_borrow(self) -> Option<EntityRef<'a, T>> {
807        self.cursor.into_ref().map(|item| item.borrow())
808    }
809
810    #[inline]
812    pub fn into_borrow_mut(self) -> Option<EntityMut<'a, T>> {
813        self.cursor.into_ref().map(|item| item.borrow_mut())
814    }
815
816    #[inline]
822    pub fn move_next(&mut self) {
823        self.cursor.move_next();
824    }
825
826    #[inline]
832    pub fn move_prev(&mut self) {
833        self.cursor.move_prev();
834    }
835
836    #[inline]
842    pub fn peek_next(&self) -> EntityCursor<'_, T> {
843        EntityCursor {
844            cursor: self.cursor.peek_next(),
845        }
846    }
847
848    #[inline]
854    pub fn peek_prev(&self) -> EntityCursor<'_, T> {
855        EntityCursor {
856            cursor: self.cursor.peek_prev(),
857        }
858    }
859
860    #[inline]
868    #[track_caller]
869    pub fn remove(&mut self) -> Option<UnsafeIntrusiveEntityRef<T>> {
870        <EntityList<T> as EntityListTraits<T>>::remove(self)
871    }
872
873    #[inline]
884    pub fn replace_with(
885        &mut self,
886        value: UnsafeIntrusiveEntityRef<T>,
887    ) -> Result<UnsafeIntrusiveEntityRef<T>, UnsafeIntrusiveEntityRef<T>> {
888        <EntityList<T> as EntityListTraits<T>>::replace_with(self, value)
889    }
890
891    #[inline]
900    pub fn insert_after(&mut self, value: UnsafeIntrusiveEntityRef<T>) {
901        <EntityList<T> as EntityListTraits<T>>::insert_after(self, value)
902    }
903
904    #[inline]
913    pub fn insert_before(&mut self, value: UnsafeIntrusiveEntityRef<T>) {
914        <EntityList<T> as EntityListTraits<T>>::insert_before(self, value)
915    }
916
917    #[inline]
937    pub fn splice_after(&mut self, list: EntityList<T>) {
938        <EntityList<T> as EntityListTraits<T>>::splice_after(self, list)
939    }
940
941    #[inline]
961    pub fn splice_before(&mut self, list: EntityList<T>) {
962        <EntityList<T> as EntityListTraits<T>>::splice_before(self, list)
963    }
964
965    pub fn split_after(&mut self) -> EntityList<T> {
973        <EntityList<T> as EntityListTraits<T>>::split_after(self)
974    }
975
976    pub fn split_before(&mut self) -> EntityList<T> {
984        <EntityList<T> as EntityListTraits<T>>::split_before(self)
985    }
986}
987
988pub struct EntityIter<'a, T> {
989    cursor: EntityCursor<'a, T>,
990    started: bool,
991}
992impl<T> core::iter::FusedIterator for EntityIter<'_, T> {}
993impl<'a, T> Iterator for EntityIter<'a, T> {
994    type Item = EntityRef<'a, T>;
995
996    fn next(&mut self) -> Option<Self::Item> {
997        if !self.started {
1000            self.started = true;
1001            self.cursor.move_next();
1002        }
1003        let item = self.cursor.get()?;
1004        self.cursor.move_next();
1005        Some(item)
1006    }
1007}
1008impl<T> DoubleEndedIterator for EntityIter<'_, T> {
1009    fn next_back(&mut self) -> Option<Self::Item> {
1010        if !self.started {
1013            self.started = true;
1014            self.cursor.move_prev();
1015        }
1016        let item = self.cursor.get()?;
1017        self.cursor.move_prev();
1018        Some(item)
1019    }
1020}
1021
1022pub struct MaybeDefaultEntityIter<'a, T> {
1023    iter: Option<EntityIter<'a, T>>,
1024}
1025impl<T> Default for MaybeDefaultEntityIter<'_, T> {
1026    fn default() -> Self {
1027        Self { iter: None }
1028    }
1029}
1030impl<T> core::iter::FusedIterator for MaybeDefaultEntityIter<'_, T> {}
1031impl<'a, T> Iterator for MaybeDefaultEntityIter<'a, T> {
1032    type Item = EntityRef<'a, T>;
1033
1034    #[inline]
1035    fn next(&mut self) -> Option<Self::Item> {
1036        self.iter.as_mut().and_then(|iter| iter.next())
1037    }
1038}
1039impl<T> DoubleEndedIterator for MaybeDefaultEntityIter<'_, T> {
1040    #[inline]
1041    fn next_back(&mut self) -> Option<Self::Item> {
1042        self.iter.as_mut().and_then(|iter| iter.next_back())
1043    }
1044}
1045
1046impl<T: 'static> RawEntityRef<T, IntrusiveLink> {
1047    pub fn new(value: T, arena: &blink_alloc::Blink) -> Self {
1053        RawEntityRef::new_with_metadata(value, IntrusiveLink::default(), arena)
1054    }
1055
1056    pub fn new_uninit(arena: &blink_alloc::Blink) -> RawEntityRef<MaybeUninit<T>, IntrusiveLink> {
1057        RawEntityRef::new_uninit_with_metadata(IntrusiveLink::default(), arena)
1058    }
1059}
1060
1061impl<T> RawEntityRef<T, IntrusiveLink>
1062where
1063    T: EntityWithParent,
1064{
1065    pub fn parent(&self) -> Option<UnsafeIntrusiveEntityRef<<T as EntityWithParent>::Parent>> {
1067        unsafe {
1068            let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
1069            let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
1070            current.as_ref().parent()
1071        }
1072    }
1073
1074    pub(self) fn set_parent(
1075        &self,
1076        parent: Option<UnsafeIntrusiveEntityRef<<T as EntityWithParent>::Parent>>,
1077    ) {
1078        unsafe {
1079            let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
1080            let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
1081            current.as_ref().set_parent(parent);
1082        }
1083    }
1084}
1085
1086impl<T> RawEntityRef<T, IntrusiveLink>
1087where
1088    T: EntityWithParent,
1089    <T as EntityWithParent>::Parent: EntityWithParent,
1090{
1091    pub fn grandparent(
1092        &self,
1093    ) -> Option<
1094        UnsafeIntrusiveEntityRef<<<T as EntityWithParent>::Parent as EntityWithParent>::Parent>,
1095    > {
1096        self.parent().and_then(|parent| parent.parent())
1097    }
1098}
1099
1100impl<T> RawEntityRef<T, IntrusiveLink> {
1101    pub fn is_linked(&self) -> bool {
1103        unsafe {
1104            let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
1105            let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
1106            current.as_ref().is_linked()
1107        }
1108    }
1109
1110    pub fn prev(&self) -> Option<Self> {
1115        use intrusive_collections::linked_list::{LinkOps, LinkedListOps};
1116        unsafe {
1117            let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
1118            let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
1119            if !current.as_ref().is_linked() {
1120                return None;
1121            }
1122            LinkOps.prev(current.cast()).map(|link_ptr| {
1123                let offset = core::mem::offset_of!(IntrusiveLink, link);
1124                let link_ptr = link_ptr.byte_sub(offset).cast::<IntrusiveLink>();
1125                Self::from_link_ptr(link_ptr)
1126            })
1127        }
1128    }
1129
1130    pub fn next(&self) -> Option<Self> {
1135        use intrusive_collections::linked_list::{LinkOps, LinkedListOps};
1136        unsafe {
1137            let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
1138            let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
1139            if !current.as_ref().is_linked() {
1140                return None;
1141            }
1142            LinkOps.next(current.cast()).map(|link_ptr| {
1143                let offset = core::mem::offset_of!(IntrusiveLink, link);
1144                let link_ptr = link_ptr.byte_sub(offset).cast::<IntrusiveLink>();
1145                Self::from_link_ptr(link_ptr)
1146            })
1147        }
1148    }
1149
1150    #[inline]
1151    unsafe fn from_link_ptr(link: NonNull<IntrusiveLink>) -> Self {
1152        let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
1153        let ptr = link.byte_sub(offset).cast::<RawEntityMetadata<T, IntrusiveLink>>();
1154        Self { inner: ptr }
1155    }
1156}
1157
1158#[doc(hidden)]
1159pub struct DefaultPointerOps<T: ?Sized>(core::marker::PhantomData<T>);
1160impl<T: ?Sized> Copy for DefaultPointerOps<T> {}
1161impl<T: ?Sized> Clone for DefaultPointerOps<T> {
1162    fn clone(&self) -> Self {
1163        *self
1164    }
1165}
1166impl<T: ?Sized> Default for DefaultPointerOps<T> {
1167    fn default() -> Self {
1168        Self::new()
1169    }
1170}
1171impl<T: ?Sized> DefaultPointerOps<T> {
1172    const fn new() -> Self {
1173        Self(core::marker::PhantomData)
1174    }
1175}
1176
1177unsafe impl<T> intrusive_collections::PointerOps
1178    for DefaultPointerOps<UnsafeIntrusiveEntityRef<T>>
1179{
1180    type Pointer = UnsafeIntrusiveEntityRef<T>;
1181    type Value = RawEntityMetadata<T, IntrusiveLink>;
1182
1183    #[inline]
1184    unsafe fn from_raw(&self, value: *const Self::Value) -> Self::Pointer {
1185        debug_assert!(!value.is_null() && value.is_aligned());
1186        UnsafeIntrusiveEntityRef::from_ptr(value.cast_mut())
1187    }
1188
1189    #[inline]
1190    fn into_raw(&self, ptr: Self::Pointer) -> *const Self::Value {
1191        UnsafeIntrusiveEntityRef::into_inner(ptr).as_ptr().cast_const()
1192    }
1193}
1194
1195pub struct EntityAdapter<T> {
1197    link_ops: intrusive_collections::linked_list::LinkOps,
1198    ptr_ops: DefaultPointerOps<UnsafeIntrusiveEntityRef<T>>,
1199    marker: core::marker::PhantomData<T>,
1200}
1201impl<T> Copy for EntityAdapter<T> {}
1202impl<T> Clone for EntityAdapter<T> {
1203    fn clone(&self) -> Self {
1204        *self
1205    }
1206}
1207impl<T> Default for EntityAdapter<T> {
1208    fn default() -> Self {
1209        Self::new()
1210    }
1211}
1212impl<T> EntityAdapter<T> {
1213    pub const fn new() -> Self {
1214        Self {
1215            link_ops: intrusive_collections::linked_list::LinkOps,
1216            ptr_ops: DefaultPointerOps::new(),
1217            marker: core::marker::PhantomData,
1218        }
1219    }
1220}
1221
1222unsafe impl<T> intrusive_collections::Adapter for EntityAdapter<T> {
1223    type LinkOps = intrusive_collections::linked_list::LinkOps;
1224    type PointerOps = DefaultPointerOps<UnsafeIntrusiveEntityRef<T>>;
1225
1226    unsafe fn get_value(
1227        &self,
1228        link: <Self::LinkOps as intrusive_collections::LinkOps>::LinkPtr,
1229    ) -> *const <Self::PointerOps as intrusive_collections::PointerOps>::Value {
1230        let offset = core::mem::offset_of!(IntrusiveLink, link);
1231        let link_ptr = link.byte_sub(offset).cast::<IntrusiveLink>();
1232        let raw_entity_ref = UnsafeIntrusiveEntityRef::<T>::from_link_ptr(link_ptr);
1233        raw_entity_ref.inner.as_ptr().cast_const()
1234    }
1235
1236    unsafe fn get_link(
1237        &self,
1238        value: *const <Self::PointerOps as intrusive_collections::PointerOps>::Value,
1239    ) -> <Self::LinkOps as intrusive_collections::LinkOps>::LinkPtr {
1240        let raw_entity_ref = UnsafeIntrusiveEntityRef::from_ptr(value.cast_mut());
1241        let offset = RawEntityMetadata::<T, IntrusiveLink>::metadata_offset();
1242        raw_entity_ref.inner.byte_add(offset).cast()
1243    }
1244
1245    fn link_ops(&self) -> &Self::LinkOps {
1246        &self.link_ops
1247    }
1248
1249    fn link_ops_mut(&mut self) -> &mut Self::LinkOps {
1250        &mut self.link_ops
1251    }
1252
1253    fn pointer_ops(&self) -> &Self::PointerOps {
1254        &self.ptr_ops
1255    }
1256}