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
use crate::abi::TypeAbi;
use alloc::{string::String, vec::Vec};
use elrond_codec::*;
pub struct Queue<T> {
vec: Vec<T>,
start: usize,
}
impl<T> Queue<T> {
#[inline]
pub fn new() -> Self {
Queue {
vec: Vec::new(),
start: 0,
}
}
}
impl<T> Default for Queue<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> Queue<T> {
#[inline]
pub fn len(&self) -> usize {
self.vec.len() - self.start
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.vec[self.start..]
}
#[inline]
pub fn push(&mut self, value: T) {
self.vec.push(value);
}
pub fn peek(&self) -> Option<&T> {
if self.start == self.vec.len() {
return None;
}
let head_ref = &self.vec[self.start];
Some(head_ref)
}
pub fn peek_mut(&mut self) -> Option<&mut T> {
if self.start == self.vec.len() {
return None;
}
let head_ref = &mut self.vec[self.start];
Some(head_ref)
}
pub fn pop(&mut self) -> Option<&T> {
if self.start == self.vec.len() {
return None;
}
let head_ref = &self.vec[self.start];
self.start += 1;
Some(head_ref)
}
}
impl<T: NestedEncode> NestedEncode for Queue<T> {
#[inline]
fn dep_encode<O: NestedEncodeOutput>(&self, dest: &mut O) -> Result<(), EncodeError> {
self.as_slice().dep_encode(dest)
}
fn dep_encode_or_exit<O: NestedEncodeOutput, ExitCtx: Clone>(
&self,
dest: &mut O,
c: ExitCtx,
exit: fn(ExitCtx, EncodeError) -> !,
) {
self.as_slice().dep_encode_or_exit(dest, c, exit);
}
}
impl<T: NestedEncode> TopEncode for Queue<T> {
#[inline]
fn top_encode<O: TopEncodeOutput>(&self, output: O) -> Result<(), EncodeError> {
self.as_slice().top_encode(output)
}
#[inline]
fn top_encode_or_exit<O: TopEncodeOutput, ExitCtx: Clone>(
&self,
output: O,
c: ExitCtx,
exit: fn(ExitCtx, EncodeError) -> !,
) {
self.as_slice().top_encode_or_exit(output, c, exit)
}
}
impl<T: NestedDecode> NestedDecode for Queue<T> {
#[inline]
fn dep_decode<I: NestedDecodeInput>(input: &mut I) -> Result<Self, DecodeError> {
Ok(Queue {
vec: Vec::<T>::dep_decode(input)?,
start: 0,
})
}
#[inline]
fn dep_decode_or_exit<I: NestedDecodeInput, ExitCtx: Clone>(
input: &mut I,
c: ExitCtx,
exit: fn(ExitCtx, DecodeError) -> !,
) -> Self {
Queue {
vec: Vec::<T>::dep_decode_or_exit(input, c, exit),
start: 0,
}
}
}
impl<T: NestedDecode> TopDecode for Queue<T> {
fn top_decode<I: TopDecodeInput>(input: I) -> Result<Self, DecodeError> {
Ok(Queue {
vec: Vec::<T>::top_decode(input)?,
start: 0,
})
}
fn top_decode_or_exit<I: TopDecodeInput, ExitCtx: Clone>(
input: I,
c: ExitCtx,
exit: fn(ExitCtx, DecodeError) -> !,
) -> Self {
Queue {
vec: Vec::<T>::top_decode_or_exit(input, c, exit),
start: 0,
}
}
}
impl<T: TypeAbi> TypeAbi for Queue<T> {
fn type_name() -> String {
let mut repr = String::from("Queue<");
repr.push_str(T::type_name().as_str());
repr.push('>');
repr
}
}