1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
use alloc::borrow::Borrow;
use core::{cell::Cell, fmt, hash::Hash, mem::MaybeUninit, ptr::NonNull};
use intrusive_collections::KeyAdapter;
use super::{
EntityMut, EntityParent, EntityRef, EntityWithParent, RawEntityMetadata, RawEntityRef,
UnsafeIntrusiveMapEntityRef,
};
type EntityAdapter<T> =
super::adapter::EntityAdapter<T, IntrusiveLink, intrusive_collections::rbtree::LinkOps>;
pub trait EntityWithKey: super::Entity {
type Key: Eq + Ord + Hash + Clone + 'static;
type Value;
fn key(&self) -> Self::Key;
fn value(&self) -> &Self::Value;
}
impl<'a, T: EntityWithKey> KeyAdapter<'a> for EntityAdapter<T> {
type Key = <T as EntityWithKey>::Key;
fn get_key(&self, s: &'a RawEntityMetadata<T, IntrusiveLink>) -> Self::Key {
s.borrow().key()
}
}
pub struct IntrusiveLink {
link: intrusive_collections::rbtree::Link,
parent: Cell<*const ()>,
}
impl Default for IntrusiveLink {
fn default() -> Self {
Self {
link: Default::default(),
parent: Cell::new(core::ptr::null()),
}
}
}
impl IntrusiveLink {
#[inline]
pub fn is_linked(&self) -> bool {
self.link.is_linked()
}
}
impl IntrusiveLink {
pub(self) fn set_parent<T>(&self, parent: Option<UnsafeIntrusiveMapEntityRef<T>>) {
if let Some(parent) = parent {
assert!(
self.link.is_linked(),
"must add entity to parent entity map before setting parent"
);
self.parent.set(UnsafeIntrusiveMapEntityRef::as_ptr(&parent).cast());
} else if self.parent.get().is_null() {
panic!("no parent previously set");
} else {
self.parent.set(core::ptr::null());
}
}
pub fn parent<T>(&self) -> Option<UnsafeIntrusiveMapEntityRef<T>> {
let parent = self.parent.get();
if parent.is_null() {
// EntityMap is orphaned
None
} else {
Some(unsafe { UnsafeIntrusiveMapEntityRef::from_raw(parent.cast()) })
}
}
}
pub struct EntityMap<T> {
map: intrusive_collections::rbtree::RBTree<EntityAdapter<T>>,
}
impl<T: EntityWithKey> Eq for EntityMap<T>
where
<T as EntityWithKey>::Value: Eq,
for<'a> EntityAdapter<T>: KeyAdapter<'a, Key = <T as EntityWithKey>::Key>,
{
}
impl<T: EntityWithKey> PartialEq for EntityMap<T>
where
<T as EntityWithKey>::Value: PartialEq,
for<'a> EntityAdapter<T>: KeyAdapter<'a, Key = <T as EntityWithKey>::Key>,
{
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
let mut lhs = self.front();
while let Some(lentry) = lhs.get() {
let key = lentry.key();
let rentry = { other.find(&key).as_pointer() };
if let Some(rentry) = rentry {
let rentry = rentry.borrow();
let lvalue = <T as EntityWithKey>::value(&lentry);
let rvalue = <T as EntityWithKey>::value(&rentry);
if !lvalue.eq(rvalue) {
return false;
}
} else {
return false;
}
lhs.move_next();
}
true
}
}
impl<T: core::hash::Hash> core::hash::Hash for EntityMap<T> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
for item in self.map.iter() {
item.borrow().hash(state);
}
}
}
impl<T> Default for EntityMap<T> {
fn default() -> Self {
Self {
map: Default::default(),
}
}
}
impl<T> EntityMap<T> {
/// Construct a new, empty [EntityMap]
pub fn new() -> Self {
Self::default()
}
/// Returns true if this map is empty
#[inline]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
/// Returns the number of entities in this map
pub fn len(&self) -> usize {
let mut cursor = self.map.front();
let mut usize = 0;
while !cursor.is_null() {
usize += 1;
cursor.move_next();
}
usize
}
#[doc(hidden)]
pub fn cursor(&self) -> EntityMapCursor<'_, T> {
EntityMapCursor {
cursor: self.map.cursor(),
}
}
/// Get an [EntityMapCursor] pointing to the first entity in the map, or the null object if
/// the map is empty
pub fn front(&self) -> EntityMapCursor<'_, T> {
EntityMapCursor {
cursor: self.map.front(),
}
}
/// Get an [EntityMapCursor] pointing to the last entity in the map, or the null object if
/// the map is empty
pub fn back(&self) -> EntityMapCursor<'_, T> {
EntityMapCursor {
cursor: self.map.back(),
}
}
/// Get an iterator over the entities in this map
///
/// The iterator returned produces [EntityRef]s for each item in the map, with their lifetime
/// bound to the map itself, not the iterator.
pub fn iter(&self) -> EntityMapIter<'_, T> {
EntityMapIter {
cursor: self.cursor(),
started: false,
}
}
/// Get a cursor to the item pointed to by `ptr`.
///
/// # Safety
///
/// This function may only be called when it is known that `ptr` refers to an entity which is
/// linked into this map. This operation will panic if the entity is not linked into any map,
/// and may result in undefined behavior if the operation is linked into a different map.
pub unsafe fn cursor_from_ptr(
&self,
ptr: UnsafeIntrusiveMapEntityRef<T>,
) -> EntityMapCursor<'_, T> {
unsafe {
let raw = UnsafeIntrusiveMapEntityRef::into_inner(ptr).as_ptr();
EntityMapCursor {
cursor: self.map.cursor_from_ptr(raw),
}
}
}
}
impl<T> EntityMap<T>
where
T: EntityWithParent,
{
pub(crate) fn parent(&self) -> UnsafeIntrusiveMapEntityRef<<T as EntityWithParent>::Parent> {
let offset = <<T as EntityWithParent>::Parent as EntityParent<T>>::offset();
let ptr = self as *const EntityMap<T>;
unsafe {
let parent = ptr.byte_sub(offset).cast::<<T as EntityWithParent>::Parent>();
UnsafeIntrusiveMapEntityRef::from_raw(parent)
}
}
}
trait EntityMapTraits<T: EntityWithKey>: Sized {
fn cursor_mut(&mut self) -> EntityMapCursorMut<'_, T>;
/// Get a mutable cursor to the item pointed to by `ptr`.
///
/// # Safety
///
/// This function may only be called when it is known that `ptr` refers to an entity which is
/// linked into this map. This operation will panic if the entity is not linked into any map,
/// and may result in undefined behavior if the operation is linked into a different map.
unsafe fn cursor_mut_from_ptr(
&mut self,
ptr: UnsafeIntrusiveMapEntityRef<T>,
) -> EntityMapCursorMut<'_, T>;
/// Get an [EntityMapCursorMut] pointing to the first entity in the map, or the null object if
/// the map is empty
fn front_mut(&mut self) -> EntityMapCursorMut<'_, T>;
/// Get an [EntityMapCursorMut] pointing to the last entity in the map, or the null object if
/// the map is empty
fn back_mut(&mut self) -> EntityMapCursorMut<'_, T>;
fn find<'b, 'a: 'b, Q>(&'a self, key: &Q) -> EntityMapCursor<'a, T>
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'b>>::Key: Borrow<Q>;
fn find_mut<'b, 'a: 'b, Q>(&'a mut self, key: &Q) -> EntityMapCursorMut<'a, T>
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'b>>::Key: Borrow<Q>;
fn remove(cursor: &mut EntityMapCursorMut<'_, T>) -> Option<UnsafeIntrusiveMapEntityRef<T>>;
fn replace_with(
cursor: &mut EntityMapCursorMut<'_, T>,
entity: UnsafeIntrusiveMapEntityRef<T>,
) -> Result<UnsafeIntrusiveMapEntityRef<T>, UnsafeIntrusiveMapEntityRef<T>>;
/// Insert `entity` into this map
fn insert(map: &mut EntityMap<T>, entity: UnsafeIntrusiveMapEntityRef<T>);
/// Removes all items from this map.
///
/// This will unlink all entities currently in the map, which requires iterating through all
/// elements in the map. If the entities may be used again, this ensures that their intrusive
/// link is properly unlinked.
fn clear(&mut self);
/// Takes all the elements out of the [EntityMap], leaving it empty.
///
/// The taken elements are returned as a new [EntityMap].
fn take(&mut self) -> Self;
}
impl<T: EntityMapItem> EntityMapTraits<T> for EntityMap<T> {
default fn cursor_mut(&mut self) -> EntityMapCursorMut<'_, T> {
EntityMapCursorMut {
cursor: self.map.cursor_mut(),
parent: core::ptr::null(),
}
}
default unsafe fn cursor_mut_from_ptr(
&mut self,
ptr: UnsafeIntrusiveMapEntityRef<T>,
) -> EntityMapCursorMut<'_, T> {
let raw = UnsafeIntrusiveMapEntityRef::into_inner(ptr).as_ptr();
unsafe {
EntityMapCursorMut {
cursor: self.map.cursor_mut_from_ptr(raw),
parent: core::ptr::null(),
}
}
}
/// Get an [EntityMapCursorMut] pointing to the first entity in the map, or the null object if
/// the map is empty
default fn front_mut(&mut self) -> EntityMapCursorMut<'_, T> {
EntityMapCursorMut {
cursor: self.map.front_mut(),
parent: core::ptr::null(),
}
}
/// Get an [EntityMapCursorMut] pointing to the last entity in the map, or the null object if
/// the map is empty
default fn back_mut(&mut self) -> EntityMapCursorMut<'_, T> {
EntityMapCursorMut {
cursor: self.map.back_mut(),
parent: core::ptr::null(),
}
}
default fn find<'b, 'a: 'b, Q>(&'a self, key: &Q) -> EntityMapCursor<'a, T>
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'b>>::Key: Borrow<Q>,
{
EntityMapCursor {
cursor: self.map.find(key),
}
}
default fn find_mut<'b, 'a: 'b, Q>(&'a mut self, key: &Q) -> EntityMapCursorMut<'a, T>
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'b>>::Key: Borrow<Q>,
{
EntityMapCursorMut {
cursor: self.map.find_mut(key),
parent: core::ptr::null(),
}
}
default fn remove(
cursor: &mut EntityMapCursorMut<'_, T>,
) -> Option<UnsafeIntrusiveMapEntityRef<T>> {
let entity = cursor.cursor.remove()?;
<T as EntityMapItem>::on_removed(entity, cursor);
Some(entity)
}
default fn replace_with(
cursor: &mut EntityMapCursorMut<'_, T>,
entity: UnsafeIntrusiveMapEntityRef<T>,
) -> Result<UnsafeIntrusiveMapEntityRef<T>, UnsafeIntrusiveMapEntityRef<T>> {
let removed = cursor.cursor.replace_with(entity)?;
<T as EntityMapItem>::on_removed(removed, cursor);
<T as EntityMapItem>::on_inserted(entity, cursor);
Ok(removed)
}
default fn insert(map: &mut EntityMap<T>, entity: UnsafeIntrusiveMapEntityRef<T>) {
let cursor = map.map.insert(entity);
let mut cursor = EntityMapCursorMut {
cursor,
parent: core::ptr::null(),
};
<T as EntityMapItem>::on_inserted(entity, &mut cursor);
}
default fn clear(&mut self) {
let mut cursor = self.front_mut();
while !cursor.is_null() {
Self::remove(&mut cursor);
}
}
default fn take(&mut self) -> Self {
let map = self.map.take();
let mut cursor = map.front();
while let Some(entity) = cursor.clone_pointer() {
<T as EntityMapItem>::on_removed(entity, &mut self.front_mut());
cursor.move_next();
}
Self { map }
}
}
impl<T> EntityMapTraits<T> for EntityMap<T>
where
T: EntityMapItem + EntityWithParent,
<T as EntityWithParent>::Parent: EntityParent<T>,
{
fn cursor_mut(&mut self) -> EntityMapCursorMut<'_, T> {
let parent = UnsafeIntrusiveMapEntityRef::into_raw(self.parent()).cast();
EntityMapCursorMut {
cursor: self.map.cursor_mut(),
parent,
}
}
unsafe fn cursor_mut_from_ptr(
&mut self,
ptr: UnsafeIntrusiveMapEntityRef<T>,
) -> EntityMapCursorMut<'_, T> {
let parent = UnsafeIntrusiveMapEntityRef::into_raw(self.parent()).cast();
let raw = UnsafeIntrusiveMapEntityRef::into_inner(ptr).as_ptr();
EntityMapCursorMut {
cursor: unsafe { self.map.cursor_mut_from_ptr(raw) },
parent,
}
}
fn back_mut(&mut self) -> EntityMapCursorMut<'_, T> {
let parent = UnsafeIntrusiveMapEntityRef::into_raw(self.parent()).cast();
EntityMapCursorMut {
cursor: self.map.back_mut(),
parent,
}
}
fn front_mut(&mut self) -> EntityMapCursorMut<'_, T> {
let parent = UnsafeIntrusiveMapEntityRef::into_raw(self.parent()).cast();
EntityMapCursorMut {
cursor: self.map.front_mut(),
parent,
}
}
fn find<'b, 'a: 'b, Q>(&'a self, key: &Q) -> EntityMapCursor<'a, T>
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'b>>::Key: Borrow<Q>,
{
EntityMapCursor {
cursor: self.map.find(key),
}
}
fn find_mut<'b, 'a: 'b, Q>(&'a mut self, key: &Q) -> EntityMapCursorMut<'a, T>
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'b>>::Key: Borrow<Q>,
{
let parent = UnsafeIntrusiveMapEntityRef::into_raw(self.parent()).cast();
EntityMapCursorMut {
cursor: self.map.find_mut(key),
parent,
}
}
#[track_caller]
fn remove(cursor: &mut EntityMapCursorMut<'_, T>) -> Option<UnsafeIntrusiveMapEntityRef<T>> {
let entity = cursor.cursor.remove()?;
entity.set_parent(None);
<T as EntityMapItem>::on_removed(entity, cursor);
Some(entity)
}
fn replace_with(
cursor: &mut EntityMapCursorMut<'_, T>,
entity: UnsafeIntrusiveMapEntityRef<T>,
) -> Result<UnsafeIntrusiveMapEntityRef<T>, UnsafeIntrusiveMapEntityRef<T>> {
let removed = cursor.cursor.replace_with(entity)?;
removed.set_parent(None);
<T as EntityMapItem>::on_removed(removed, cursor);
let parent = cursor.parent().expect("cannot insert items in an orphaned entity map");
entity.set_parent(Some(parent));
<T as EntityMapItem>::on_inserted(entity, cursor);
Ok(removed)
}
fn insert(map: &mut EntityMap<T>, entity: UnsafeIntrusiveMapEntityRef<T>) {
let parent = map.parent();
let cursor = map.map.insert(entity);
entity.set_parent(Some(parent));
let parent = UnsafeIntrusiveMapEntityRef::into_raw(parent).cast();
let mut cursor = EntityMapCursorMut { cursor, parent };
<T as EntityMapItem>::on_inserted(entity, &mut cursor);
}
fn clear(&mut self) {
let mut cursor = self.front_mut();
while !cursor.is_null() {
Self::remove(&mut cursor);
}
}
#[track_caller]
fn take(&mut self) -> Self {
let map = self.map.take();
let mut map_cursor = map.front();
while let Some(entity) = map_cursor.clone_pointer() {
entity.set_parent(None);
<T as EntityMapItem>::on_removed(entity, &mut self.front_mut());
map_cursor.move_next();
}
Self { map }
}
}
impl<T: EntityMapItem> EntityMap<T> {
#[doc(hidden)]
pub fn cursor_mut(&mut self) -> EntityMapCursorMut<'_, T> {
<Self as EntityMapTraits<T>>::cursor_mut(self)
}
/// Get a mutable cursor to the item pointed to by `ptr`.
///
/// # Safety
///
/// This function may only be called when it is known that `ptr` refers to an entity which is
/// linked into this map. This operation will panic if the entity is not linked into any map,
/// and may result in undefined behavior if the operation is linked into a different map.
pub unsafe fn cursor_mut_from_ptr(
&mut self,
ptr: UnsafeIntrusiveMapEntityRef<T>,
) -> EntityMapCursorMut<'_, T> {
unsafe { <Self as EntityMapTraits<T>>::cursor_mut_from_ptr(self, ptr) }
}
/// Get an [EntityMapCursorMut] pointing to the first entity in the map, or the null object if
/// the map is empty
pub fn front_mut(&mut self) -> EntityMapCursorMut<'_, T> {
<Self as EntityMapTraits<T>>::front_mut(self)
}
/// Get an [EntityMapCursorMut] pointing to the last entity in the map, or the null object if
/// the map is empty
pub fn back_mut(&mut self) -> EntityMapCursorMut<'_, T> {
<Self as EntityMapTraits<T>>::back_mut(self)
}
/// Insert `entity` into this map
pub fn insert(&mut self, entity: UnsafeIntrusiveMapEntityRef<T>) {
<Self as EntityMapTraits<T>>::insert(self, entity)
}
/// Removes all items from this map.
///
/// This will unlink all entities currently in the map, which requires iterating through all
/// elements in the map. If the entities may be used again, this ensures that their intrusive
/// link is properly unlinked.
pub fn clear(&mut self) {
<Self as EntityMapTraits<T>>::clear(self)
}
/// Empties the map without properly unlinking the intrusive links of the items in the map.
///
/// Since this does not unlink any objects, any attempts to link these objects into another
/// [EntityMap] will fail but will not cause any memory unsafety. To unlink those objects
/// manually, you must call the `force_unlink` function on the link.
pub fn fast_clear(&mut self) {
self.map.fast_clear();
}
/// Takes all the elements out of the [EntityMap], leaving it empty.
///
/// The taken elements are returned as a new [EntityMap].
#[track_caller]
pub fn take(&mut self) -> Self {
<Self as EntityMapTraits<T>>::take(self)
}
/// Returns true if `key` is present in this map
pub fn contains<'a, Q>(&self, key: &Q) -> bool
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'a>>::Key: Borrow<Q>,
{
!self.map.find(key).is_null()
}
/// Get a cursor to the entry in this map stored under `key`.
///
/// If no such entry exists, the null cursor is returned.
pub fn find<'b, 'a: 'b, Q>(&'a self, key: &Q) -> EntityMapCursor<'a, T>
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'b>>::Key: Borrow<Q>,
{
<Self as EntityMapTraits<T>>::find(self, key)
}
/// Get a mutable cursor to the entry in this map stored under `key`.
///
/// If no such entry exists, the null cursor is returned.
pub fn find_mut<'b, 'a: 'b, Q>(&'a mut self, key: &Q) -> EntityMapCursorMut<'a, T>
where
Q: ?Sized + Ord,
<EntityAdapter<T> as KeyAdapter<'b>>::Key: Borrow<Q>,
{
<Self as EntityMapTraits<T>>::find_mut(self, key)
}
}
impl<T: EntityWithKey> fmt::Debug for EntityMap<T>
where
<T as EntityWithKey>::Key: fmt::Debug,
<T as EntityWithKey>::Value: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut builder = f.debug_map();
for entity in self.iter() {
builder.entry(&entity.key(), &entity.value());
}
builder.finish()
}
}
impl<T: EntityMapItem> FromIterator<UnsafeIntrusiveMapEntityRef<T>> for EntityMap<T> {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = UnsafeIntrusiveMapEntityRef<T>>,
{
let mut map = EntityMap::<T>::default();
for handle in iter {
map.insert(handle);
}
map
}
}
impl<T> IntoIterator for EntityMap<T> {
type IntoIter = intrusive_collections::rbtree::IntoIter<EntityAdapter<T>>;
type Item = UnsafeIntrusiveMapEntityRef<T>;
fn into_iter(self) -> Self::IntoIter {
self.map.into_iter()
}
}
impl<'a, T> IntoIterator for &'a EntityMap<T> {
type IntoIter = EntityMapIter<'a, T>;
type Item = EntityRef<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
/// A cursor which provides read-only access to an [EntityMap].
pub struct EntityMapCursor<'a, T> {
cursor: intrusive_collections::rbtree::Cursor<'a, EntityAdapter<T>>,
}
impl<'a, T> EntityMapCursor<'a, T> {
/// Returns true if this cursor is pointing to the null object
#[inline]
pub fn is_null(&self) -> bool {
self.cursor.is_null()
}
/// Get a shared reference to the entity under the cursor.
///
/// Returns `None` if the cursor is currently pointing to the null object.
///
/// NOTE: This returns an [EntityRef] whose lifetime is bound to the underlying [EntityMap],
/// _not_ the [EntityMapCursor], since the cursor cannot mutate the map.
#[track_caller]
pub fn get(&self) -> Option<EntityRef<'a, T>> {
Some(self.cursor.get()?.entity.borrow())
}
/// Get the [UnsafeIntrusiveMapEntityRef] corresponding to the entity under the cursor.
///
/// Returns `None` if the cursor is pointing to the null object.
#[inline]
pub fn as_pointer(&self) -> Option<UnsafeIntrusiveMapEntityRef<T>> {
self.cursor.clone_pointer()
}
/// Consume the cursor and convert it into a borrow of the current entity, or `None` if null.
#[inline]
#[track_caller]
pub fn into_borrow(self) -> Option<EntityRef<'a, T>> {
Some(self.cursor.get()?.borrow())
}
/// Moves the cursor to the next element of the [EntityMap].
///
/// If the cursor is pointing to the null object then this will move it to the front of the
/// [EntityMap]. If it is pointing to the back of the [EntityMap] then this will move it to
/// the null object.
#[inline]
pub fn move_next(&mut self) {
self.cursor.move_next();
}
/// Moves the cursor to the previous element of the [EntityMap].
///
/// If the cursor is pointing to the null object then this will move it to the back of the
/// [EntityMap]. If it is pointing to the front of the [EntityMap] then this will move it to
/// the null object.
#[inline]
pub fn move_prev(&mut self) {
self.cursor.move_prev();
}
/// Returns a cursor pointing to the next element of the [EntityMap].
///
/// If the cursor is pointing to the null object then this will return a cursor pointing to the
/// front of the [EntityMap]. If it is pointing to the last entity of the [EntityMap] then
/// this will return a null cursor.
#[inline]
pub fn peek_next(&self) -> EntityMapCursor<'_, T> {
EntityMapCursor {
cursor: self.cursor.peek_next(),
}
}
/// Returns a cursor pointing to the previous element of the [EntityMap].
///
/// If the cursor is pointing to the null object then this will return a cursor pointing to
/// the last entity in the [EntityMap]. If it is pointing to the front of the [EntityMap] then
/// this will return a null cursor.
#[inline]
pub fn peek_prev(&self) -> EntityMapCursor<'_, T> {
EntityMapCursor {
cursor: self.cursor.peek_prev(),
}
}
}
/// A cursor which provides mutable access to an [EntityMap].
pub struct EntityMapCursorMut<'a, T> {
cursor: intrusive_collections::rbtree::CursorMut<'a, EntityAdapter<T>>,
parent: *const (),
}
impl<T> EntityMapCursorMut<'_, T>
where
T: EntityWithParent,
{
fn parent(&self) -> Option<UnsafeIntrusiveMapEntityRef<<T as EntityWithParent>::Parent>> {
if self.parent.is_null() {
None
} else {
Some(unsafe { UnsafeIntrusiveMapEntityRef::from_raw(self.parent.cast()) })
}
}
}
impl<'a, T: EntityMapItem> EntityMapCursorMut<'a, T> {
/// Returns true if this cursor is pointing to the null object
#[inline]
pub fn is_null(&self) -> bool {
self.cursor.is_null()
}
/// Get a shared reference to the entity under the cursor.
///
/// Returns `None` if the cursor is currently pointing to the null object.
///
/// NOTE: This binds the lifetime of the [EntityRef] to the cursor, to ensure that the cursor
/// is frozen while the entity is being borrowed. This ensures that only one reference at a
/// time is being handed out by this cursor.
pub fn get(&self) -> Option<EntityRef<'_, T>> {
self.cursor.get().map(|obj| obj.entity.borrow())
}
/// Get a mutable reference to the entity under the cursor.
///
/// Returns `None` if the cursor is currently pointing to the null object.
///
/// Not only does this mutably borrow the cursor, the lifetime of the [EntityMut] is bound to
/// that of the cursor, which means it cannot outlive the cursor, and also prevents the cursor
/// from being accessed in any way until the mutable reference is dropped. This makes it
/// impossible to try and alias the underlying entity using the cursor.
pub fn get_mut(&mut self) -> Option<EntityMut<'_, T>> {
self.cursor.get().map(|obj| obj.entity.borrow_mut())
}
/// Returns a read-only cursor pointing to the current element.
///
/// The lifetime of the returned [EntityMapCursor] is bound to that of the [EntityMapCursorMut],
/// which means it cannot outlive the [EntityMapCursorMut] and that the [EntityMapCursorMut] is
/// frozen for the lifetime of the [EntityMapCursor].
pub fn as_cursor(&self) -> EntityMapCursor<'_, T> {
EntityMapCursor {
cursor: self.cursor.as_cursor(),
}
}
/// Get the [UnsafeIntrusiveMapEntityRef] corresponding to the entity under the cursor.
///
/// Returns `None` if the cursor is pointing to the null object.
#[inline]
pub fn as_pointer(&self) -> Option<UnsafeIntrusiveMapEntityRef<T>> {
self.cursor.as_cursor().clone_pointer()
}
/// Consume the cursor and convert it into a borrow of the current entity, or `None` if null.
#[inline]
pub fn into_borrow(self) -> Option<EntityRef<'a, T>> {
self.cursor.into_ref().map(|item| item.borrow())
}
/// Consume the cursor and convert it into a mutable borrow of the current entity, or `None` if null.
#[inline]
pub fn into_borrow_mut(self) -> Option<EntityMut<'a, T>> {
self.cursor.into_ref().map(|item| item.borrow_mut())
}
/// Moves the cursor to the next element of the [EntityMap].
///
/// If the cursor is pointing to the null object then this will move it to the front of the
/// [EntityMap]. If it is pointing to the back of the [EntityMap] then this will move it to
/// the null object.
#[inline]
pub fn move_next(&mut self) {
self.cursor.move_next();
}
/// Moves the cursor to the previous element of the [EntityMap].
///
/// If the cursor is pointing to the null object then this will move it to the back of the
/// [EntityMap]. If it is pointing to the front of the [EntityMap] then this will move it to
/// the null object.
#[inline]
pub fn move_prev(&mut self) {
self.cursor.move_prev();
}
/// Returns a cursor pointing to the next element of the [EntityMap].
///
/// If the cursor is pointing to the null object then this will return a cursor pointing to the
/// front of the [EntityMap]. If it is pointing to the last entity of the [EntityMap] then
/// this will return a null cursor.
#[inline]
pub fn peek_next(&self) -> EntityMapCursor<'_, T> {
EntityMapCursor {
cursor: self.cursor.peek_next(),
}
}
/// Returns a cursor pointing to the previous element of the [EntityMap].
///
/// If the cursor is pointing to the null object then this will return a cursor pointing to
/// the last entity in the [EntityMap]. If it is pointing to the front of the [EntityMap] then
/// this will return a null cursor.
#[inline]
pub fn peek_prev(&self) -> EntityMapCursor<'_, T> {
EntityMapCursor {
cursor: self.cursor.peek_prev(),
}
}
/// Removes the current entity from the [EntityMap].
///
/// A pointer to the element that was removed is returned, and the cursor is moved to point to
/// the next element in the [EntityMap].
///
/// If the cursor is currently pointing to the null object then nothing is removed and `None` is
/// returned.
#[inline]
#[track_caller]
pub fn remove(&mut self) -> Option<UnsafeIntrusiveMapEntityRef<T>> {
<EntityMap<T> as EntityMapTraits<T>>::remove(self)
}
/// Removes the current entity from the [EntityMap] and inserts another one in its place.
///
/// A pointer to the entity that was removed is returned, and the cursor is modified to point to
/// the newly added entity.
///
/// If the cursor is currently pointing to the null object then `Err` is returned containing the
/// entity we failed to insert.
///
/// # Panics
/// Panics if the new entity is already linked to a different intrusive collection.
#[inline]
pub fn replace_with(
&mut self,
value: UnsafeIntrusiveMapEntityRef<T>,
) -> Result<UnsafeIntrusiveMapEntityRef<T>, UnsafeIntrusiveMapEntityRef<T>> {
<EntityMap<T> as EntityMapTraits<T>>::replace_with(self, value)
}
}
pub struct EntityMapIter<'a, T> {
cursor: EntityMapCursor<'a, T>,
started: bool,
}
impl<T> core::iter::FusedIterator for EntityMapIter<'_, T> {}
impl<'a, T> Iterator for EntityMapIter<'a, T> {
type Item = EntityRef<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
// If we haven't started iterating yet, then we're on the null cursor, so move to the
// front of the map now that we have started iterating.
if !self.started {
self.started = true;
self.cursor.move_next();
}
let item = self.cursor.get()?;
self.cursor.move_next();
Some(item)
}
}
impl<T> DoubleEndedIterator for EntityMapIter<'_, T> {
fn next_back(&mut self) -> Option<Self::Item> {
// If we haven't started iterating yet, then we're on the null cursor, so move to the
// back of the map now that we have started iterating.
if !self.started {
self.started = true;
self.cursor.move_prev();
}
let item = self.cursor.get()?;
self.cursor.move_prev();
Some(item)
}
}
pub struct MaybeDefaultEntityMapIter<'a, T> {
iter: Option<EntityMapIter<'a, T>>,
}
impl<T> Default for MaybeDefaultEntityMapIter<'_, T> {
fn default() -> Self {
Self { iter: None }
}
}
impl<T> core::iter::FusedIterator for MaybeDefaultEntityMapIter<'_, T> {}
impl<'a, T> Iterator for MaybeDefaultEntityMapIter<'a, T> {
type Item = EntityRef<'a, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.as_mut().and_then(|iter| iter.next())
}
}
impl<T> DoubleEndedIterator for MaybeDefaultEntityMapIter<'_, T> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.as_mut().and_then(|iter| iter.next_back())
}
}
impl<T: 'static> RawEntityRef<T, IntrusiveLink> {
/// Create a new [UnsafeIntrusiveMapEntityRef] by allocating `value` in `arena`
///
/// # SAFETY
///
/// This function has the same requirements around safety as [RawEntityRef::new].
pub fn new(value: T, arena: &blink_alloc::Blink) -> Self {
RawEntityRef::new_with_metadata(value, IntrusiveLink::default(), arena)
}
pub fn new_uninit(arena: &blink_alloc::Blink) -> RawEntityRef<MaybeUninit<T>, IntrusiveLink> {
RawEntityRef::new_uninit_with_metadata(IntrusiveLink::default(), arena)
}
}
impl<T> RawEntityRef<T, IntrusiveLink>
where
T: EntityWithParent,
{
/// Returns the parent entity this entity is linked to, if linked.
pub fn parent(&self) -> Option<UnsafeIntrusiveMapEntityRef<<T as EntityWithParent>::Parent>> {
unsafe {
let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
current.as_ref().parent()
}
}
pub(self) fn set_parent(
&self,
parent: Option<UnsafeIntrusiveMapEntityRef<<T as EntityWithParent>::Parent>>,
) {
unsafe {
let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
current.as_ref().set_parent(parent);
}
}
}
impl<T> RawEntityRef<T, IntrusiveLink>
where
T: EntityWithParent,
<T as EntityWithParent>::Parent: EntityWithParent,
{
pub fn grandparent(
&self,
) -> Option<
UnsafeIntrusiveMapEntityRef<<<T as EntityWithParent>::Parent as EntityWithParent>::Parent>,
> {
self.parent().and_then(|parent| parent.parent())
}
}
impl<T> RawEntityRef<T, IntrusiveLink> {
/// Returns true if this entity is linked into an intrusive map
pub fn is_linked(&self) -> bool {
unsafe {
let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
current.as_ref().is_linked()
}
}
/*
/// Get the previous entity in the map of `T` containing the current entity
///
/// For example, in a map of `Operation` in a `Block`, this would return the handle of the
/// previous operation in the block, or `None` if there are no other ops before this one.
pub fn prev(&self) -> Option<Self> {
use intrusive_collections::rbtree::LinkOps;
unsafe {
let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
if !current.as_ref().is_linked() {
return None;
}
LinkOps.prev(current.cast()).map(|link_ptr| {
let offset = core::mem::offset_of!(IntrusiveLink, link);
let link_ptr = link_ptr.byte_sub(offset).cast::<IntrusiveLink>();
Self::from_link_ptr(link_ptr)
})
}
}
/// Get the next entity in the map of `T` containing the current entity
///
/// For example, in a map of `Operation` in a `Block`, this would return the handle of the
/// next operation in the block, or `None` if there are no other ops after this one.
pub fn next(&self) -> Option<Self> {
use intrusive_collections::rbtree::LinkOps;
unsafe {
let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
let current = self.inner.byte_add(offset).cast::<IntrusiveLink>();
if !current.as_ref().is_linked() {
return None;
}
LinkOps.next(current.cast()).map(|link_ptr| {
let offset = core::mem::offset_of!(IntrusiveLink, link);
let link_ptr = link_ptr.byte_sub(offset).cast::<IntrusiveLink>();
Self::from_link_ptr(link_ptr)
})
}
}
*/
#[inline]
unsafe fn from_link_ptr(link: NonNull<IntrusiveLink>) -> Self {
let offset = core::mem::offset_of!(RawEntityMetadata<T, IntrusiveLink>, metadata);
let ptr = unsafe { link.byte_sub(offset).cast::<RawEntityMetadata<T, IntrusiveLink>>() };
Self { inner: ptr }
}
}
/// A trait implemented by any [super::Entity] that is storeable in an [EntityMap].
///
/// This trait defines callbacks that are executed any time the entity is added, removed, or
/// transferred between collections.
///
/// By default, these callbacks are no-ops.
#[allow(unused_variables)]
pub trait EntityMapItem: Sized + EntityWithKey {
/// Invoked when this entity type is inserted into an intrusive map
#[inline]
fn on_inserted(
this: UnsafeIntrusiveMapEntityRef<Self>,
cursor: &mut EntityMapCursorMut<'_, Self>,
) {
}
/// Invoked when this entity type is removed from an intrusive map
#[inline]
fn on_removed(this: UnsafeIntrusiveMapEntityRef<Self>, map: &mut EntityMapCursorMut<'_, Self>) {
}
/// Invoked when a set of entities is moved from one intrusive map to another
#[inline]
fn on_transfer(
this: UnsafeIntrusiveMapEntityRef<Self>,
from: &mut EntityMap<Self>,
to: &mut EntityMap<Self>,
) {
}
}
impl<T: Sized + EntityWithKey> EntityMapItem for T {
default fn on_inserted(
_this: UnsafeIntrusiveMapEntityRef<Self>,
_map: &mut EntityMapCursorMut<'_, Self>,
) {
}
default fn on_removed(
_this: UnsafeIntrusiveMapEntityRef<Self>,
_map: &mut EntityMapCursorMut<'_, Self>,
) {
}
default fn on_transfer(
_this: UnsafeIntrusiveMapEntityRef<Self>,
_from: &mut EntityMap<Self>,
_to: &mut EntityMap<Self>,
) {
}
}
unsafe impl<T> intrusive_collections::Adapter
for super::adapter::EntityAdapter<T, IntrusiveLink, intrusive_collections::rbtree::LinkOps>
{
type LinkOps = intrusive_collections::rbtree::LinkOps;
type PointerOps = super::adapter::DefaultPointerOps<RawEntityRef<T, IntrusiveLink>>;
unsafe fn get_value(
&self,
link: <Self::LinkOps as intrusive_collections::LinkOps>::LinkPtr,
) -> *const <Self::PointerOps as intrusive_collections::PointerOps>::Value {
let offset = core::mem::offset_of!(IntrusiveLink, link);
unsafe {
let link_ptr = link.byte_sub(offset).cast::<IntrusiveLink>();
let raw_entity_ref = RawEntityRef::<T, IntrusiveLink>::from_link_ptr(link_ptr);
raw_entity_ref.inner.as_ptr().cast_const()
}
}
unsafe fn get_link(
&self,
value: *const <Self::PointerOps as intrusive_collections::PointerOps>::Value,
) -> <Self::LinkOps as intrusive_collections::LinkOps>::LinkPtr {
let raw_entity_ref = unsafe { RawEntityRef::from_ptr(value.cast_mut()) };
let offset = RawEntityMetadata::<T, IntrusiveLink>::metadata_offset();
unsafe { raw_entity_ref.inner.byte_add(offset).cast() }
}
fn link_ops(&self) -> &Self::LinkOps {
&self.link_ops
}
fn link_ops_mut(&mut self) -> &mut Self::LinkOps {
&mut self.link_ops
}
fn pointer_ops(&self) -> &Self::PointerOps {
&self.ptr_ops
}
}