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
use core::borrow::Borrow;
use super::{ManagedVec, ManagedVecItem};
use crate::{
abi::{TypeAbi, TypeDescriptionContainer},
api::{EndpointFinishApi, ManagedTypeApi},
finish_all, ArgId, ContractCallArg, DynArg, DynArgInput, DynArgOutput, EndpointResult,
};
use alloc::string::String;
use elrond_codec::TopEncode;
#[derive(Clone, Default)]
pub struct ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem,
{
pub(super) contents: ManagedVec<M, T>,
}
pub type ManagedCountedVarArgs<M, T> = ManagedCountedMultiResultVec<M, T>;
impl<M, T> ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem,
{
#[inline]
pub fn new() -> Self {
ManagedCountedMultiResultVec::from(ManagedVec::new())
}
}
impl<M, T> ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem,
{
#[inline]
pub fn len(&self) -> usize {
self.contents.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.contents.is_empty()
}
}
impl<M, T> ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem,
{
#[inline]
pub fn push(&mut self, item: T) {
self.contents.push(item);
}
#[inline]
pub fn into_vec(self) -> ManagedVec<M, T> {
self.contents
}
}
impl<M, T> From<ManagedVec<M, T>> for ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem,
{
#[inline]
#[rustfmt::skip]
fn from(v: ManagedVec<M, T>) -> Self {
ManagedCountedMultiResultVec {
contents: v,
}
}
}
impl<M, T> DynArg for ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem + DynArg,
{
fn dyn_load<I: DynArgInput>(loader: &mut I, arg_id: ArgId) -> Self {
let mut result = ManagedCountedMultiResultVec::new();
let count = usize::dyn_load(loader, arg_id);
for _ in 0..count {
result.contents.push(T::dyn_load(loader, arg_id));
}
result
}
}
impl<M, T> EndpointResult for ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem + EndpointResult,
{
type DecodeAs = ManagedCountedMultiResultVec<M, T>;
#[inline]
fn finish<FA>(&self)
where
FA: ManagedTypeApi + EndpointFinishApi,
{
self.len().finish::<FA>();
finish_all::<FA, _, _>(self.contents.into_iter());
}
}
impl<M, T> ContractCallArg for &ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem + ContractCallArg + TopEncode,
{
fn push_dyn_arg<O: DynArgOutput>(&self, output: &mut O) {
self.len().push_dyn_arg(output);
for item in self.contents.iter() {
item.borrow().push_dyn_arg(output);
}
}
}
impl<M, T> ContractCallArg for ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem + ContractCallArg + TopEncode,
{
fn push_dyn_arg<O: DynArgOutput>(&self, output: &mut O) {
ContractCallArg::push_dyn_arg(&self, output)
}
}
impl<M, T> TypeAbi for ManagedCountedMultiResultVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem + TypeAbi,
{
fn type_name() -> String {
let mut repr = String::from("counted-variadic<");
repr.push_str(T::type_name().as_str());
repr.push('>');
repr
}
fn provide_type_descriptions<TDC: TypeDescriptionContainer>(accumulator: &mut TDC) {
T::provide_type_descriptions(accumulator);
}
fn is_multi_arg_or_result() -> bool {
true
}
}