mixnet/request_manager/post_queues.rs
1// Copyright 2022 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use super::super::core::RelSessionIndex;
22use std::collections::VecDeque;
23
24pub struct PostQueues<T> {
25 /// Post queue for the current session.
26 pub current: VecDeque<T>,
27 /// Post queue for the previous session.
28 pub prev: VecDeque<T>,
29 /// Additional post queue for the default session (either the previous or the current session,
30 /// depending on the current session phase).
31 pub default: VecDeque<T>,
32}
33
34impl<T> PostQueues<T> {
35 pub fn new(capacity: usize) -> Self {
36 Self {
37 current: VecDeque::with_capacity(capacity),
38 prev: VecDeque::with_capacity(capacity),
39 default: VecDeque::with_capacity(capacity),
40 }
41 }
42
43 pub fn iter(&self) -> impl Iterator<Item = &VecDeque<T>> {
44 [&self.current, &self.prev, &self.default].into_iter()
45 }
46
47 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut VecDeque<T>> {
48 [&mut self.current, &mut self.prev, &mut self.default].into_iter()
49 }
50}
51
52impl<T> std::ops::Index<Option<RelSessionIndex>> for PostQueues<T> {
53 type Output = VecDeque<T>;
54
55 fn index(&self, index: Option<RelSessionIndex>) -> &Self::Output {
56 match index {
57 Some(RelSessionIndex::Current) => &self.current,
58 Some(RelSessionIndex::Prev) => &self.prev,
59 None => &self.default,
60 }
61 }
62}
63
64impl<T> std::ops::IndexMut<Option<RelSessionIndex>> for PostQueues<T> {
65 fn index_mut(&mut self, index: Option<RelSessionIndex>) -> &mut Self::Output {
66 match index {
67 Some(RelSessionIndex::Current) => &mut self.current,
68 Some(RelSessionIndex::Prev) => &mut self.prev,
69 None => &mut self.default,
70 }
71 }
72}