-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathany_buffer.rs
More file actions
1288 lines (1098 loc) · 41.3 KB
/
any_buffer.rs
File metadata and controls
1288 lines (1098 loc) · 41.3 KB
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
/*
* Copyright (C) 2025 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// TODO(@mxgrey): Add module-level documentation describing how to use AnyBuffer
use std::{
any::{Any, TypeId},
collections::{hash_map::Entry, HashMap},
ops::RangeBounds,
sync::{Mutex, OnceLock},
};
use bevy_ecs::{
prelude::{Commands, Entity, EntityRef, EntityWorldMut, Mut, World},
system::SystemState,
};
use thiserror::Error as ThisError;
use smallvec::SmallVec;
use crate::{
add_listener_to_source, Accessed, Buffer, BufferAccessMut, BufferAccessors, BufferError,
BufferKey, BufferKeyTag, BufferLocation, BufferStorage, Bufferable, Buffered, Builder,
DrainBuffer, Gate, GateState, InspectBuffer, Joined, ManageBuffer, NotifyBufferUpdate,
OperationError, OperationResult, OperationRoster, OrBroken,
};
/// A [`Buffer`] whose message type has been anonymized. Joining with this buffer
/// type will yield an [`AnyMessageBox`].
#[derive(Clone, Copy)]
pub struct AnyBuffer {
pub(crate) location: BufferLocation,
pub(crate) interface: &'static (dyn AnyBufferAccessInterface + Send + Sync),
}
impl AnyBuffer {
/// The buffer ID for this key.
pub fn id(&self) -> Entity {
self.location.source
}
/// ID of the workflow that this buffer is associated with.
pub fn scope(&self) -> Entity {
self.location.scope
}
/// Get the type ID of the messages that this buffer supports.
pub fn message_type_id(&self) -> TypeId {
self.interface.message_type_id()
}
pub fn message_type_name(&self) -> &'static str {
self.interface.message_type_name()
}
/// Get the [`AnyBufferAccessInterface`] for this specific instance of [`AnyBuffer`].
pub fn get_interface(&self) -> &'static (dyn AnyBufferAccessInterface + Send + Sync) {
self.interface
}
/// Get the [`AnyBufferAccessInterface`] for a concrete message type.
pub fn interface_for<T: 'static + Send + Sync>(
) -> &'static (dyn AnyBufferAccessInterface + Send + Sync) {
static INTERFACE_MAP: OnceLock<
Mutex<HashMap<TypeId, &'static (dyn AnyBufferAccessInterface + Send + Sync)>>,
> = OnceLock::new();
let interfaces = INTERFACE_MAP.get_or_init(|| Mutex::default());
let mut interfaces_mut = interfaces.lock().unwrap();
*interfaces_mut
.entry(TypeId::of::<T>())
.or_insert_with(|| Box::leak(Box::new(AnyBufferAccessImpl::<T>::new())))
}
}
impl std::fmt::Debug for AnyBuffer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AnyBuffer")
.field("scope", &self.location.scope)
.field("source", &self.location.source)
.field("message_type_name", &self.interface.message_type_name())
.finish()
}
}
impl AnyBuffer {
/// Downcast this into a concrete [`Buffer`] for the specified message type.
///
/// To downcast into a specialized kind of buffer, use [`Self::downcast_buffer`] instead.
pub fn downcast_for_message<Message: 'static>(&self) -> Option<Buffer<Message>> {
if TypeId::of::<Message>() == self.interface.message_type_id() {
Some(Buffer {
location: self.location,
_ignore: Default::default(),
})
} else {
None
}
}
/// Downcast this into a different special buffer representation, such as a
/// `JsonBuffer`.
pub fn downcast_buffer<BufferType: 'static>(&self) -> Option<BufferType> {
self.interface.buffer_downcast(TypeId::of::<BufferType>())?(self.location)
.downcast::<BufferType>()
.ok()
.map(|x| *x)
}
pub fn as_any_buffer(&self) -> Self {
self.clone().into()
}
}
impl<T: 'static + Send + Sync + Any> From<Buffer<T>> for AnyBuffer {
fn from(value: Buffer<T>) -> Self {
let interface = AnyBuffer::interface_for::<T>();
AnyBuffer {
location: value.location,
interface,
}
}
}
/// Similar to a [`BufferKey`] except it can be used for any buffer without
/// knowing the buffer's message type at compile time.
///
/// This can key be used with a [`World`][1] to directly view or manipulate the
/// contents of a buffer through the [`AnyBufferWorldAccess`] interface.
///
/// [1]: bevy_ecs::prelude::World
#[derive(Clone)]
pub struct AnyBufferKey {
pub(crate) tag: BufferKeyTag,
pub(crate) interface: &'static (dyn AnyBufferAccessInterface + Send + Sync),
}
impl AnyBufferKey {
/// Downcast this into a concrete [`BufferKey`] for the specified message type.
///
/// To downcast to a specialized kind of key, use [`Self::downcast_buffer_key`] instead.
pub fn downcast_for_message<Message: 'static>(self) -> Option<BufferKey<Message>> {
if TypeId::of::<Message>() == self.interface.message_type_id() {
Some(BufferKey {
tag: self.tag,
_ignore: Default::default(),
})
} else {
None
}
}
/// Downcast this into a different special buffer key representation, such
/// as a `JsonBufferKey`.
pub fn downcast_buffer_key<KeyType: 'static>(self) -> Option<KeyType> {
self.interface.key_downcast(TypeId::of::<KeyType>())?(self.tag)
.downcast::<KeyType>()
.ok()
.map(|x| *x)
}
/// The buffer ID of this key.
pub fn id(&self) -> Entity {
self.tag.buffer
}
/// The session that this key belongs to.
pub fn session(&self) -> Entity {
self.tag.session
}
pub(crate) fn is_in_use(&self) -> bool {
self.tag.is_in_use()
}
pub(crate) fn deep_clone(&self) -> Self {
Self {
tag: self.tag.deep_clone(),
interface: self.interface,
}
}
}
impl std::fmt::Debug for AnyBufferKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AnyBufferKey")
.field("message_type_name", &self.interface.message_type_name())
.field("tag", &self.tag)
.finish()
}
}
impl<T: 'static + Send + Sync + Any> From<BufferKey<T>> for AnyBufferKey {
fn from(value: BufferKey<T>) -> Self {
let interface = AnyBuffer::interface_for::<T>();
AnyBufferKey {
tag: value.tag,
interface,
}
}
}
/// Similar to [`BufferView`][crate::BufferView], but this can be unlocked with
/// an [`AnyBufferKey`], so it can work for any buffer whose message types
/// support serialization and deserialization.
pub struct AnyBufferView<'a> {
storage: Box<dyn AnyBufferViewing + 'a>,
gate: &'a GateState,
session: Entity,
}
impl<'a> AnyBufferView<'a> {
/// Look at the oldest message in the buffer.
pub fn oldest(&self) -> Option<AnyMessageRef<'_>> {
self.storage.any_oldest(self.session)
}
/// Look at the newest message in the buffer.
pub fn newest(&self) -> Option<AnyMessageRef<'_>> {
self.storage.any_newest(self.session)
}
/// Borrow a message from the buffer. Index 0 is the oldest message in the buffer
/// while the highest index is the newest message in the buffer.
pub fn get(&self, index: usize) -> Option<AnyMessageRef<'_>> {
self.storage.any_get(self.session, index)
}
/// Get how many messages are in this buffer.
pub fn len(&self) -> usize {
self.storage.any_count(self.session)
}
/// Check if the buffer is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Check whether the gate of this buffer is open or closed.
pub fn gate(&self) -> Gate {
self.gate
.map
.get(&self.session)
.copied()
.unwrap_or(Gate::Open)
}
}
/// Similar to [`BufferMut`][crate::BufferMut], but this can be unlocked with an
/// [`AnyBufferKey`], so it can work for any buffer regardless of the data type
/// inside.
pub struct AnyBufferMut<'w, 's, 'a> {
storage: Box<dyn AnyBufferManagement + 'a>,
gate: Mut<'a, GateState>,
buffer: Entity,
session: Entity,
accessor: Option<Entity>,
commands: &'a mut Commands<'w, 's>,
modified: bool,
}
impl<'w, 's, 'a> AnyBufferMut<'w, 's, 'a> {
/// Same as [BufferMut::allow_closed_loops][1].
///
/// [1]: crate::BufferMut::allow_closed_loops
pub fn allow_closed_loops(mut self) -> Self {
self.accessor = None;
self
}
/// Look at the oldest message in the buffer.
pub fn oldest(&self) -> Option<AnyMessageRef<'_>> {
self.storage.any_oldest(self.session)
}
/// Look at the newest message in the buffer.
pub fn newest(&self) -> Option<AnyMessageRef<'_>> {
self.storage.any_newest(self.session)
}
/// Borrow a message from the buffer. Index 0 is the oldest message in the buffer
/// while the highest index is the newest message in the buffer.
pub fn get(&self, index: usize) -> Option<AnyMessageRef<'_>> {
self.storage.any_get(self.session, index)
}
/// Get how many messages are in this buffer.
pub fn len(&self) -> usize {
self.storage.any_count(self.session)
}
/// Check if the buffer is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Check whether the gate of this buffer is open or closed.
pub fn gate(&self) -> Gate {
self.gate
.map
.get(&self.session)
.copied()
.unwrap_or(Gate::Open)
}
/// Modify the oldest message in the buffer.
pub fn oldest_mut(&mut self) -> Option<AnyMessageMut<'_>> {
self.modified = true;
self.storage.any_oldest_mut(self.session)
}
/// Modify the newest message in the buffer.
pub fn newest_mut(&mut self) -> Option<AnyMessageMut<'_>> {
self.modified = true;
self.storage.any_newest_mut(self.session)
}
/// Modify a message in the buffer. Index 0 is the oldest message in the buffer
/// with the highest index being the newest message in the buffer.
pub fn get_mut(&mut self, index: usize) -> Option<AnyMessageMut<'_>> {
self.modified = true;
self.storage.any_get_mut(self.session, index)
}
/// Drain a range of messages out of the buffer.
pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> DrainAnyBuffer<'_> {
self.modified = true;
DrainAnyBuffer {
interface: self.storage.any_drain(self.session, AnyRange::new(range)),
}
}
/// Pull the oldest message from the buffer.
pub fn pull(&mut self) -> Option<AnyMessageBox> {
self.modified = true;
self.storage.any_pull(self.session)
}
/// Pull the message that was most recently put into the buffer (instead of the
/// oldest, which is what [`Self::pull`] gives).
pub fn pull_newest(&mut self) -> Option<AnyMessageBox> {
self.modified = true;
self.storage.any_pull_newest(self.session)
}
/// Attempt to push a new value into the buffer.
///
/// If the input value matches the message type of the buffer, this will
/// return [`Ok`]. If the buffer is at its limit before a successful push, this
/// will return the value that needed to be removed.
///
/// If the input value does not match the message type of the buffer, this
/// will return [`Err`] and give back the message that you tried to push.
pub fn push<T: 'static + Send + Sync + Any>(&mut self, value: T) -> Result<Option<T>, T> {
if TypeId::of::<T>() != self.storage.any_message_type() {
return Err(value);
}
self.modified = true;
// SAFETY: We checked that T matches the message type for this buffer,
// so pushing and downcasting should not exhibit any errors.
let removed = self
.storage
.any_push(self.session, Box::new(value))
.unwrap()
.map(|value| *value.downcast::<T>().unwrap());
Ok(removed)
}
/// Attempt to push a new value of any message type into the buffer.
///
/// If the input value matches the message type of the buffer, this will
/// return [`Ok`]. If the buffer is at its limit before a successful push, this
/// will return the value that needed to be removed.
///
/// If the input value does not match the message type of the buffer, this
/// will return [`Err`] and give back an error with the message that you
/// tried to push and the type information for the expected message type.
pub fn push_any(
&mut self,
value: AnyMessageBox,
) -> Result<Option<AnyMessageBox>, AnyMessageError> {
self.storage.any_push(self.session, value)
}
/// Attempt to push a value into the buffer as if it is the oldest value of
/// the buffer.
///
/// The result follows the same rules as [`Self::push`].
pub fn push_as_oldest<T: 'static + Send + Sync + Any>(
&mut self,
value: T,
) -> Result<Option<T>, T> {
if TypeId::of::<T>() != self.storage.any_message_type() {
return Err(value);
}
self.modified = true;
// SAFETY: We checked that T matches the message type for this buffer,
// so pushing and downcasting should not exhibit any errors.
let removed = self
.storage
.any_push_as_oldest(self.session, Box::new(value))
.unwrap()
.map(|value| *value.downcast::<T>().unwrap());
Ok(removed)
}
/// Attempt to push a value into the buffer as if it is the oldest value of
/// the buffer.
///
/// The result follows the same rules as [`Self::push_any`].
pub fn push_any_as_oldest(
&mut self,
value: AnyMessageBox,
) -> Result<Option<AnyMessageBox>, AnyMessageError> {
self.storage.any_push_as_oldest(self.session, value)
}
/// Tell the buffer [`Gate`] to open.
pub fn open_gate(&mut self) {
if let Some(gate) = self.gate.map.get_mut(&self.session) {
if *gate != Gate::Open {
*gate = Gate::Open;
self.modified = true;
}
}
}
/// Tell the buffer [`Gate`] to close.
pub fn close_gate(&mut self) {
if let Some(gate) = self.gate.map.get_mut(&self.session) {
*gate = Gate::Closed;
// There is no need to to indicate that a modification happened
// because listeners do not get notified about gates closing.
}
}
/// Perform an action on the gate of the buffer.
pub fn gate_action(&mut self, action: Gate) {
match action {
Gate::Open => self.open_gate(),
Gate::Closed => self.close_gate(),
}
}
/// Trigger the listeners for this buffer to wake up even if nothing in the
/// buffer has changed. This could be used for timers or timeout elements
/// in a workflow.
pub fn pulse(&mut self) {
self.modified = true;
}
}
impl<'w, 's, 'a> Drop for AnyBufferMut<'w, 's, 'a> {
fn drop(&mut self) {
if self.modified {
self.commands.add(NotifyBufferUpdate::new(
self.buffer,
self.session,
self.accessor,
));
}
}
}
/// This trait allows [`World`] to give you access to any buffer using an
/// [`AnyBufferKey`].
pub trait AnyBufferWorldAccess {
/// Call this to get read-only access to any buffer.
///
/// For technical reasons this requires direct [`World`] access, but you can
/// do other read-only queries on the world while holding onto the
/// [`AnyBufferView`].
fn any_buffer_view<'a>(&self, key: &AnyBufferKey) -> Result<AnyBufferView<'_>, BufferError>;
/// Call this to get mutable access to any buffer.
///
/// Pass in a callback that will receive a [`AnyBufferMut`], allowing it to
/// view and modify the contents of the buffer.
fn any_buffer_mut<U>(
&mut self,
key: &AnyBufferKey,
f: impl FnOnce(AnyBufferMut) -> U,
) -> Result<U, BufferError>;
}
impl AnyBufferWorldAccess for World {
fn any_buffer_view<'a>(&self, key: &AnyBufferKey) -> Result<AnyBufferView<'_>, BufferError> {
key.interface.create_any_buffer_view(key, self)
}
fn any_buffer_mut<U>(
&mut self,
key: &AnyBufferKey,
f: impl FnOnce(AnyBufferMut) -> U,
) -> Result<U, BufferError> {
let interface = key.interface;
let mut state = interface.create_any_buffer_access_mut_state(self);
let mut access = state.get_any_buffer_access_mut(self);
let buffer_mut = access.as_any_buffer_mut(key)?;
Ok(f(buffer_mut))
}
}
trait AnyBufferViewing {
fn any_count(&self, session: Entity) -> usize;
fn any_oldest<'a>(&'a self, session: Entity) -> Option<AnyMessageRef<'a>>;
fn any_newest<'a>(&'a self, session: Entity) -> Option<AnyMessageRef<'a>>;
fn any_get<'a>(&'a self, session: Entity, index: usize) -> Option<AnyMessageRef<'a>>;
fn any_message_type(&self) -> TypeId;
}
trait AnyBufferManagement: AnyBufferViewing {
fn any_push(&mut self, session: Entity, value: AnyMessageBox) -> AnyMessagePushResult;
fn any_push_as_oldest(&mut self, session: Entity, value: AnyMessageBox)
-> AnyMessagePushResult;
fn any_pull(&mut self, session: Entity) -> Option<AnyMessageBox>;
fn any_pull_newest(&mut self, session: Entity) -> Option<AnyMessageBox>;
fn any_oldest_mut<'a>(&'a mut self, session: Entity) -> Option<AnyMessageMut<'a>>;
fn any_newest_mut<'a>(&'a mut self, session: Entity) -> Option<AnyMessageMut<'a>>;
fn any_get_mut<'a>(&'a mut self, session: Entity, index: usize) -> Option<AnyMessageMut<'a>>;
fn any_drain<'a>(
&'a mut self,
session: Entity,
range: AnyRange,
) -> Box<dyn DrainAnyBufferInterface + 'a>;
}
pub(crate) struct AnyRange {
start_bound: std::ops::Bound<usize>,
end_bound: std::ops::Bound<usize>,
}
impl AnyRange {
pub(crate) fn new<T: std::ops::RangeBounds<usize>>(range: T) -> Self {
AnyRange {
start_bound: deref_bound(range.start_bound()),
end_bound: deref_bound(range.end_bound()),
}
}
}
fn deref_bound(bound: std::ops::Bound<&usize>) -> std::ops::Bound<usize> {
match bound {
std::ops::Bound::Included(v) => std::ops::Bound::Included(*v),
std::ops::Bound::Excluded(v) => std::ops::Bound::Excluded(*v),
std::ops::Bound::Unbounded => std::ops::Bound::Unbounded,
}
}
impl std::ops::RangeBounds<usize> for AnyRange {
fn start_bound(&self) -> std::ops::Bound<&usize> {
self.start_bound.as_ref()
}
fn end_bound(&self) -> std::ops::Bound<&usize> {
self.end_bound.as_ref()
}
fn contains<U>(&self, item: &U) -> bool
where
usize: PartialOrd<U>,
U: ?Sized + PartialOrd<usize>,
{
match self.start_bound {
std::ops::Bound::Excluded(lower) => {
if *item <= lower {
return false;
}
}
std::ops::Bound::Included(lower) => {
if *item < lower {
return false;
}
}
_ => {}
}
match self.end_bound {
std::ops::Bound::Excluded(upper) => {
if upper <= *item {
return false;
}
}
std::ops::Bound::Included(upper) => {
if upper < *item {
return false;
}
}
_ => {}
}
return true;
}
}
pub type AnyMessageRef<'a> = &'a (dyn Any + 'static + Send + Sync);
impl<T: 'static + Send + Sync + Any> AnyBufferViewing for &'_ BufferStorage<T> {
fn any_count(&self, session: Entity) -> usize {
self.count(session)
}
fn any_oldest<'a>(&'a self, session: Entity) -> Option<AnyMessageRef<'a>> {
self.oldest(session).map(to_any_ref)
}
fn any_newest<'a>(&'a self, session: Entity) -> Option<AnyMessageRef<'a>> {
self.newest(session).map(to_any_ref)
}
fn any_get<'a>(&'a self, session: Entity, index: usize) -> Option<AnyMessageRef<'a>> {
self.get(session, index).map(to_any_ref)
}
fn any_message_type(&self) -> TypeId {
TypeId::of::<T>()
}
}
impl<T: 'static + Send + Sync + Any> AnyBufferViewing for Mut<'_, BufferStorage<T>> {
fn any_count(&self, session: Entity) -> usize {
self.count(session)
}
fn any_oldest<'a>(&'a self, session: Entity) -> Option<AnyMessageRef<'a>> {
self.oldest(session).map(to_any_ref)
}
fn any_newest<'a>(&'a self, session: Entity) -> Option<AnyMessageRef<'a>> {
self.newest(session).map(to_any_ref)
}
fn any_get<'a>(&'a self, session: Entity, index: usize) -> Option<AnyMessageRef<'a>> {
self.get(session, index).map(to_any_ref)
}
fn any_message_type(&self) -> TypeId {
TypeId::of::<T>()
}
}
pub type AnyMessageMut<'a> = &'a mut (dyn Any + 'static + Send + Sync);
pub type AnyMessageBox = Box<dyn Any + 'static + Send + Sync>;
#[derive(ThisError, Debug)]
#[error("failed to convert a message")]
pub struct AnyMessageError {
/// The original value provided
pub value: AnyMessageBox,
/// The ID of the type expected by the buffer
pub type_id: TypeId,
/// The name of the type expected by the buffer
pub type_name: &'static str,
}
pub type AnyMessagePushResult = Result<Option<AnyMessageBox>, AnyMessageError>;
impl<T: 'static + Send + Sync + Any> AnyBufferManagement for Mut<'_, BufferStorage<T>> {
fn any_push(&mut self, session: Entity, value: AnyMessageBox) -> AnyMessagePushResult {
let value = from_any_message::<T>(value)?;
Ok(self.push(session, value).map(to_any_message))
}
fn any_push_as_oldest(
&mut self,
session: Entity,
value: AnyMessageBox,
) -> AnyMessagePushResult {
let value = from_any_message::<T>(value)?;
Ok(self.push_as_oldest(session, value).map(to_any_message))
}
fn any_pull(&mut self, session: Entity) -> Option<AnyMessageBox> {
self.pull(session).map(to_any_message)
}
fn any_pull_newest(&mut self, session: Entity) -> Option<AnyMessageBox> {
self.pull_newest(session).map(to_any_message)
}
fn any_oldest_mut<'a>(&'a mut self, session: Entity) -> Option<AnyMessageMut<'a>> {
self.oldest_mut(session).map(to_any_mut)
}
fn any_newest_mut<'a>(&'a mut self, session: Entity) -> Option<AnyMessageMut<'a>> {
self.newest_mut(session).map(to_any_mut)
}
fn any_get_mut<'a>(&'a mut self, session: Entity, index: usize) -> Option<AnyMessageMut<'a>> {
self.get_mut(session, index).map(to_any_mut)
}
fn any_drain<'a>(
&'a mut self,
session: Entity,
range: AnyRange,
) -> Box<dyn DrainAnyBufferInterface + 'a> {
Box::new(self.drain(session, range))
}
}
fn to_any_ref<'a, T: 'static + Send + Sync + Any>(x: &'a T) -> AnyMessageRef<'a> {
x
}
fn to_any_mut<'a, T: 'static + Send + Sync + Any>(x: &'a mut T) -> AnyMessageMut<'a> {
x
}
fn to_any_message<T: 'static + Send + Sync + Any>(x: T) -> AnyMessageBox {
Box::new(x)
}
fn from_any_message<T: 'static + Send + Sync + Any>(
value: AnyMessageBox,
) -> Result<T, AnyMessageError>
where
T: 'static,
{
let value = value.downcast::<T>().map_err(|value| AnyMessageError {
value,
type_id: TypeId::of::<T>(),
type_name: std::any::type_name::<T>(),
})?;
Ok(*value)
}
pub trait AnyBufferAccessMutState {
fn get_any_buffer_access_mut<'s, 'w: 's>(
&'s mut self,
world: &'w mut World,
) -> Box<dyn AnyBufferAccessMut<'w, 's> + 's>;
}
impl<T: 'static + Send + Sync + Any> AnyBufferAccessMutState
for SystemState<BufferAccessMut<'static, 'static, T>>
{
fn get_any_buffer_access_mut<'s, 'w: 's>(
&'s mut self,
world: &'w mut World,
) -> Box<dyn AnyBufferAccessMut<'w, 's> + 's> {
Box::new(self.get_mut(world))
}
}
pub trait AnyBufferAccessMut<'w, 's> {
fn as_any_buffer_mut<'a>(
&'a mut self,
key: &AnyBufferKey,
) -> Result<AnyBufferMut<'w, 's, 'a>, BufferError>;
}
impl<'w, 's, T: 'static + Send + Sync + Any> AnyBufferAccessMut<'w, 's>
for BufferAccessMut<'w, 's, T>
{
fn as_any_buffer_mut<'a>(
&'a mut self,
key: &AnyBufferKey,
) -> Result<AnyBufferMut<'w, 's, 'a>, BufferError> {
let BufferAccessMut { query, commands } = self;
let (storage, gate) = query
.get_mut(key.tag.buffer)
.map_err(|_| BufferError::BufferMissing)?;
Ok(AnyBufferMut {
storage: Box::new(storage),
gate,
buffer: key.tag.buffer,
session: key.tag.session,
accessor: Some(key.tag.accessor),
commands,
modified: false,
})
}
}
pub trait AnyBufferAccessInterface {
fn message_type_id(&self) -> TypeId;
fn message_type_name(&self) -> &'static str;
fn buffered_count(&self, entity: &EntityRef, session: Entity) -> Result<usize, OperationError>;
fn ensure_session(&self, entity_mut: &mut EntityWorldMut, session: Entity) -> OperationResult;
fn register_buffer_downcast(&self, buffer_type: TypeId, f: BufferDowncastBox);
fn buffer_downcast(&self, buffer_type: TypeId) -> Option<BufferDowncastRef>;
fn register_key_downcast(&self, key_type: TypeId, f: KeyDowncastBox);
fn key_downcast(&self, key_type: TypeId) -> Option<KeyDowncastRef>;
fn pull(
&self,
entity_mut: &mut EntityWorldMut,
session: Entity,
) -> Result<AnyMessageBox, OperationError>;
fn create_any_buffer_view<'a>(
&self,
key: &AnyBufferKey,
world: &'a World,
) -> Result<AnyBufferView<'a>, BufferError>;
fn create_any_buffer_access_mut_state(
&self,
world: &mut World,
) -> Box<dyn AnyBufferAccessMutState>;
}
pub type BufferDowncastBox = Box<dyn Fn(BufferLocation) -> Box<dyn Any> + Send + Sync>;
pub type BufferDowncastRef = &'static (dyn Fn(BufferLocation) -> Box<dyn Any> + Send + Sync);
pub type KeyDowncastBox = Box<dyn Fn(BufferKeyTag) -> Box<dyn Any> + Send + Sync>;
pub type KeyDowncastRef = &'static (dyn Fn(BufferKeyTag) -> Box<dyn Any> + Send + Sync);
struct AnyBufferAccessImpl<T> {
buffer_downcasts: Mutex<HashMap<TypeId, BufferDowncastRef>>,
key_downcasts: Mutex<HashMap<TypeId, KeyDowncastRef>>,
_ignore: std::marker::PhantomData<fn(T)>,
}
impl<T: 'static + Send + Sync> AnyBufferAccessImpl<T> {
fn new() -> Self {
let mut buffer_downcasts: HashMap<_, BufferDowncastRef> = HashMap::new();
// SAFETY: These leaks are okay because we will only ever instantiate
// AnyBufferAccessImpl once per generic argument T, which puts a firm
// ceiling on how many of these callbacks will get leaked.
// Automatically register a downcast into AnyBuffer
buffer_downcasts.insert(
TypeId::of::<AnyBuffer>(),
Box::leak(Box::new(|location| -> Box<dyn Any> {
Box::new(AnyBuffer {
location,
interface: AnyBuffer::interface_for::<T>(),
})
})),
);
let mut key_downcasts: HashMap<_, KeyDowncastRef> = HashMap::new();
// Automatically register a downcast to AnyBufferKey
key_downcasts.insert(
TypeId::of::<AnyBufferKey>(),
Box::leak(Box::new(|tag| -> Box<dyn Any> {
Box::new(AnyBufferKey {
tag,
interface: AnyBuffer::interface_for::<T>(),
})
})),
);
Self {
buffer_downcasts: Mutex::new(buffer_downcasts),
key_downcasts: Mutex::new(key_downcasts),
_ignore: Default::default(),
}
}
}
impl<T: 'static + Send + Sync + Any> AnyBufferAccessInterface for AnyBufferAccessImpl<T> {
fn message_type_id(&self) -> TypeId {
TypeId::of::<T>()
}
fn message_type_name(&self) -> &'static str {
std::any::type_name::<T>()
}
fn buffered_count(&self, entity: &EntityRef, session: Entity) -> Result<usize, OperationError> {
entity.buffered_count::<T>(session)
}
fn ensure_session(&self, entity_mut: &mut EntityWorldMut, session: Entity) -> OperationResult {
entity_mut.ensure_session::<T>(session)
}
fn register_buffer_downcast(&self, buffer_type: TypeId, f: BufferDowncastBox) {
let mut downcasts = self.buffer_downcasts.lock().unwrap();
if let Entry::Vacant(entry) = downcasts.entry(buffer_type) {
// We should only leak this into the register once per type
entry.insert(Box::leak(f));
}
}
fn buffer_downcast(&self, buffer_type: TypeId) -> Option<BufferDowncastRef> {
self.buffer_downcasts
.lock()
.unwrap()
.get(&buffer_type)
.copied()
}
fn register_key_downcast(&self, key_type: TypeId, f: KeyDowncastBox) {
let mut downcasts = self.key_downcasts.lock().unwrap();
if let Entry::Vacant(entry) = downcasts.entry(key_type) {
// We should only leak this in to the register once per type
entry.insert(Box::leak(f));
}
}
fn key_downcast(&self, key_type: TypeId) -> Option<KeyDowncastRef> {
self.key_downcasts.lock().unwrap().get(&key_type).copied()
}
fn pull(
&self,
entity_mut: &mut EntityWorldMut,
session: Entity,
) -> Result<AnyMessageBox, OperationError> {
entity_mut
.pull_from_buffer::<T>(session)
.map(to_any_message)
}
fn create_any_buffer_view<'a>(
&self,
key: &AnyBufferKey,
world: &'a World,
) -> Result<AnyBufferView<'a>, BufferError> {
let buffer_ref = world
.get_entity(key.tag.buffer)
.ok_or(BufferError::BufferMissing)?;
let storage = buffer_ref
.get::<BufferStorage<T>>()
.ok_or(BufferError::BufferMissing)?;
let gate = buffer_ref
.get::<GateState>()
.ok_or(BufferError::BufferMissing)?;
Ok(AnyBufferView {
storage: Box::new(storage),
gate,
session: key.tag.session,
})
}
fn create_any_buffer_access_mut_state(
&self,
world: &mut World,
) -> Box<dyn AnyBufferAccessMutState> {
Box::new(SystemState::<BufferAccessMut<T>>::new(world))
}
}
pub struct DrainAnyBuffer<'a> {
interface: Box<dyn DrainAnyBufferInterface + 'a>,
}
impl<'a> Iterator for DrainAnyBuffer<'a> {
type Item = AnyMessageBox;
fn next(&mut self) -> Option<Self::Item> {
self.interface.any_next()
}
}
trait DrainAnyBufferInterface {
fn any_next(&mut self) -> Option<AnyMessageBox>;
}
impl<T: 'static + Send + Sync + Any> DrainAnyBufferInterface for DrainBuffer<'_, T> {
fn any_next(&mut self) -> Option<AnyMessageBox> {
self.next().map(to_any_message)
}
}
impl Bufferable for AnyBuffer {
type BufferType = Self;
fn into_buffer(self, builder: &mut Builder) -> Self::BufferType {
assert_eq!(self.scope(), builder.scope());
self
}
}