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
//local shortcuts
//third-party shortcuts
use bevy::prelude::*;
//standard shortcuts
use std::collections::VecDeque;
//-------------------------------------------------------------------------------------------------------------------
/// Buffers queued cobweb commands of type `T`.
#[derive(Resource)]
pub(crate) struct CobwebCommandQueue<T: Send + Sync + 'static>
{
/// Queued commands.
commands: VecDeque<T>,
/// Cached buffers for storing commands.
buffers: Vec<VecDeque<T>>,
}
impl<T: Send + Sync + 'static> CobwebCommandQueue<T>
{
/// Removes the inner command queue.
pub(crate) fn remove(&mut self) -> VecDeque<T>
{
let replacement = self.buffers.pop().unwrap_or_default();
std::mem::replace(&mut self.commands, replacement)
}
/// Adds a cobweb command to the end of the queue.
pub(crate) fn push(&mut self, command: T)
{
self.commands.push_back(command);
}
/// Removes a command from the front of the queue.
pub(crate) fn pop_front(&mut self) -> Option<T>
{
self.commands.pop_front()
}
/// Pushes a list of cobweb commands to the end of the command queue.
pub(crate) fn append(&mut self, mut new: VecDeque<T>)
{
if new.len() > 0
{
self.commands.append(&mut new);
}
self.buffers.push(new);
}
/// Pushes a list of cobweb commands to the end of the command queue then returns the inner queue.
pub(crate) fn _append_and_remove(&mut self, mut new: VecDeque<T>) -> VecDeque<T>
{
if new.len() > 0
{
self.commands.append(&mut new);
}
self.buffers.push(new);
self.remove()
}
}
impl<T: Send + Sync + 'static> Default for CobwebCommandQueue<T>
{
fn default() -> Self
{
Self{
commands: VecDeque::default(),
buffers: Vec::default()
}
}
}
//-------------------------------------------------------------------------------------------------------------------