gear_common/storage/complex/
queue.rs1use crate::storage::{Counted, Dequeue, DequeueError, IterableMap, KeyFor};
25use core::marker::PhantomData;
26
27pub trait Queue {
29 type Value;
31 type Error: DequeueError;
33 type OutputError: From<Self::Error>;
35
36 fn dequeue() -> Result<Option<Self::Value>, Self::OutputError>;
39
40 fn mutate_values<F: FnMut(Self::Value) -> Self::Value>(f: F);
42
43 fn queue(value: Self::Value) -> Result<(), Self::OutputError>;
45
46 fn clear();
48
49 fn requeue(value: Self::Value) -> Result<(), Self::OutputError>;
54}
55
56pub struct QueueImpl<T, OutputError, KeyGen>(PhantomData<(T, OutputError, KeyGen)>)
60where
61 T: Dequeue,
62 OutputError: From<T::Error>,
63 KeyGen: KeyFor<Key = T::Key, Value = T::Value>;
64
65impl<T, OutputError, KeyGen> Queue for QueueImpl<T, OutputError, KeyGen>
67where
68 T: Dequeue,
69 OutputError: From<T::Error>,
70 T::Error: DequeueError,
71 KeyGen: KeyFor<Key = T::Key, Value = T::Value>,
72{
73 type Value = T::Value;
74 type Error = T::Error;
75 type OutputError = OutputError;
76
77 fn dequeue() -> Result<Option<Self::Value>, Self::OutputError> {
78 T::pop_front().map_err(Into::into)
79 }
80
81 fn mutate_values<F: FnMut(Self::Value) -> Self::Value>(f: F) {
82 T::mutate_values(f)
83 }
84
85 fn queue(value: Self::Value) -> Result<(), Self::OutputError> {
86 let key = KeyGen::key_for(&value);
87 T::push_back(key, value).map_err(Into::into)
88 }
89
90 fn clear() {
91 T::clear()
92 }
93
94 fn requeue(value: Self::Value) -> Result<(), Self::OutputError> {
95 let key = KeyGen::key_for(&value);
96 T::push_front(key, value).map_err(Into::into)
97 }
98}
99
100impl<T, OutputError, KeyGen> Counted for QueueImpl<T, OutputError, KeyGen>
103where
104 T: Dequeue + Counted,
105 OutputError: From<T::Error>,
106 KeyGen: KeyFor<Key = T::Key, Value = T::Value>,
107{
108 type Length = T::Length;
109
110 fn len() -> Self::Length {
111 T::len()
112 }
113}
114
115pub struct QueueDrainIter<T, OutputError>(T::DrainIter, PhantomData<OutputError>)
119where
120 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
121 OutputError: From<T::Error>;
122
123impl<T, OutputError> Iterator for QueueDrainIter<T, OutputError>
125where
126 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
127 OutputError: From<T::Error>,
128{
129 type Item = Result<T::Value, OutputError>;
130
131 fn next(&mut self) -> Option<Self::Item> {
132 self.0.next().map(|res| res.map_err(Into::into))
133 }
134}
135
136pub struct QueueIter<T, OutputError>(T::Iter, PhantomData<OutputError>)
138where
139 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
140 OutputError: From<T::Error>;
141
142impl<T, OutputError> Iterator for QueueIter<T, OutputError>
144where
145 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
146 OutputError: From<T::Error>,
147{
148 type Item = Result<T::Value, OutputError>;
149
150 fn next(&mut self) -> Option<Self::Item> {
151 self.0.next().map(|res| res.map_err(Into::into))
152 }
153}
154
155impl<T, OutputError, KeyGen> IterableMap<Result<T::Value, OutputError>>
158 for QueueImpl<T, OutputError, KeyGen>
159where
160 T: Dequeue + IterableMap<Result<T::Value, T::Error>>,
161 OutputError: From<T::Error>,
162 KeyGen: KeyFor<Key = T::Key, Value = T::Value>,
163{
164 type DrainIter = QueueDrainIter<T, OutputError>;
165 type Iter = QueueIter<T, OutputError>;
166
167 fn drain() -> Self::DrainIter {
168 QueueDrainIter(T::drain(), PhantomData::<OutputError>)
169 }
170
171 fn iter() -> Self::Iter {
172 QueueIter(T::iter(), PhantomData::<OutputError>)
173 }
174}