gear_common/storage/complex/
queue.rs1use crate::storage::{Counted, Dequeue, DequeueError, IterableMap, KeyFor};
10use core::marker::PhantomData;
11
12pub trait Queue {
14 type Value;
16 type Error: DequeueError;
18 type OutputError: From<Self::Error>;
20
21 fn dequeue() -> Result<Option<Self::Value>, Self::OutputError>;
24
25 fn mutate_values<F: FnMut(Self::Value) -> Self::Value>(f: F);
27
28 fn queue(value: Self::Value) -> Result<(), Self::OutputError>;
30
31 fn clear();
33
34 fn requeue(value: Self::Value) -> Result<(), Self::OutputError>;
39}
40
41pub struct QueueImpl<T, OutputError, KeyGen>(PhantomData<(T, OutputError, KeyGen)>)
45where
46 T: Dequeue,
47 OutputError: From<T::Error>,
48 KeyGen: KeyFor<Key = T::Key, Value = T::Value>;
49
50impl<T, OutputError, KeyGen> Queue for QueueImpl<T, OutputError, KeyGen>
52where
53 T: Dequeue,
54 OutputError: From<T::Error>,
55 T::Error: DequeueError,
56 KeyGen: KeyFor<Key = T::Key, Value = T::Value>,
57{
58 type Value = T::Value;
59 type Error = T::Error;
60 type OutputError = OutputError;
61
62 fn dequeue() -> Result<Option<Self::Value>, Self::OutputError> {
63 T::pop_front().map_err(Into::into)
64 }
65
66 fn mutate_values<F: FnMut(Self::Value) -> Self::Value>(f: F) {
67 T::mutate_values(f)
68 }
69
70 fn queue(value: Self::Value) -> Result<(), Self::OutputError> {
71 let key = KeyGen::key_for(&value);
72 T::push_back(key, value).map_err(Into::into)
73 }
74
75 fn clear() {
76 T::clear()
77 }
78
79 fn requeue(value: Self::Value) -> Result<(), Self::OutputError> {
80 let key = KeyGen::key_for(&value);
81 T::push_front(key, value).map_err(Into::into)
82 }
83}
84
85impl<T, OutputError, KeyGen> Counted for QueueImpl<T, OutputError, KeyGen>
88where
89 T: Dequeue + Counted,
90 OutputError: From<T::Error>,
91 KeyGen: KeyFor<Key = T::Key, Value = T::Value>,
92{
93 type Length = T::Length;
94
95 fn len() -> Self::Length {
96 T::len()
97 }
98}
99
100pub struct QueueDrainIter<T, OutputError>(T::DrainIter, PhantomData<OutputError>)
104where
105 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
106 OutputError: From<T::Error>;
107
108impl<T, OutputError> Iterator for QueueDrainIter<T, OutputError>
110where
111 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
112 OutputError: From<T::Error>,
113{
114 type Item = Result<T::Value, OutputError>;
115
116 fn next(&mut self) -> Option<Self::Item> {
117 self.0.next().map(|res| res.map_err(Into::into))
118 }
119}
120
121pub struct QueueIter<T, OutputError>(T::Iter, PhantomData<OutputError>)
123where
124 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
125 OutputError: From<T::Error>;
126
127impl<T, OutputError> Iterator for QueueIter<T, OutputError>
129where
130 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
131 OutputError: From<T::Error>,
132{
133 type Item = Result<T::Value, OutputError>;
134
135 fn next(&mut self) -> Option<Self::Item> {
136 self.0.next().map(|res| res.map_err(Into::into))
137 }
138}
139
140impl<T, OutputError, KeyGen> IterableMap<Result<T::Value, OutputError>>
143 for QueueImpl<T, OutputError, KeyGen>
144where
145 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
146 OutputError: From<T::Error>,
147 KeyGen: KeyFor<Key = T::Key, Value = T::Value>,
148{
149 type DrainIter = QueueDrainIter<T, OutputError>;
150 type Iter = QueueIter<T, OutputError>;
151
152 fn drain() -> Self::DrainIter {
153 QueueDrainIter(T::drain(), PhantomData::<OutputError>)
154 }
155
156 fn iter() -> Self::Iter {
157 QueueIter(T::iter(), PhantomData::<OutputError>)
158 }
159}