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
use crate::abi::{TypeAbi, TypeDescriptionContainer};
use crate::io::{ArgId, ContractCallArg, DynArg, DynArgInput};
use crate::types::{ArgBuffer, SCError};
use crate::{api::EndpointFinishApi, EndpointResult};
use alloc::string::String;
use alloc::vec::Vec;
use core::iter::FromIterator;
use dharitri_codec::TopDecodeInput;

/// Structure that allows taking a variable number of arguments
/// or returning a variable number of results in a smart contract endpoint.
#[derive(Clone)]
pub struct MultiArgVec<T>(pub Vec<T>);

/// Used for taking a variable number of arguments in an endpoint,
/// it is synonymous with `MultiResultVec`/`MultiArgVec`.
pub type VarArgs<T> = MultiArgVec<T>;

/// Used for returning a variable number of results from an endpoint,
/// it is synonymous with `MultiResult`.
pub type MultiResultVec<T> = VarArgs<T>;

impl<T> From<Vec<T>> for MultiArgVec<T> {
	fn from(v: Vec<T>) -> Self {
		MultiArgVec(v)
	}
}

impl<T> MultiArgVec<T> {
	#[inline]
	pub fn new() -> Self {
		MultiArgVec(Vec::new())
	}
}

impl<T> Default for MultiArgVec<T> {
	#[inline]
	fn default() -> Self {
		Self::new()
	}
}

impl<T> MultiArgVec<T> {
	#[inline]
	pub fn into_vec(self) -> Vec<T> {
		self.0
	}

	#[inline]
	pub fn as_slice(&self) -> &[T] {
		self.0.as_slice()
	}

	#[inline]
	pub fn push(&mut self, value: T) {
		self.0.push(value);
	}

	#[inline]
	pub fn len(&self) -> usize {
		self.0.len()
	}

	#[inline]
	pub fn is_empty(&self) -> bool {
		self.0.is_empty()
	}

	#[inline]
	pub fn iter(&self) -> core::slice::Iter<'_, T> {
		self.0.iter()
	}
}

impl<T> FromIterator<T> for MultiArgVec<T> {
	fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
		let v = Vec::<T>::from_iter(iter);
		MultiArgVec(v)
	}
}

impl<T> DynArg for MultiArgVec<T>
where
	T: DynArg,
{
	// #[inline(never)]
	fn dyn_load<I, D>(loader: &mut D, arg_id: ArgId) -> Self
	where
		I: TopDecodeInput,
		D: DynArgInput<I>,
	{
		let mut result_vec: Vec<T> = Vec::new();
		while loader.has_next() {
			result_vec.push(T::dyn_load(loader, arg_id));
		}
		MultiArgVec(result_vec)
	}
}

impl<T> EndpointResult for MultiArgVec<T>
where
	T: EndpointResult,
{
	type DecodeAs = MultiArgVec<T::DecodeAs>;

	#[inline]
	fn finish<FA>(&self, api: FA)
	where
		FA: EndpointFinishApi + Clone + 'static,
	{
		for elem in self.0.iter() {
			elem.finish(api.clone());
		}
	}
}

impl<T> ContractCallArg for &MultiArgVec<T>
where
	T: ContractCallArg,
{
	fn push_async_arg(&self, serializer: &mut ArgBuffer) -> Result<(), SCError> {
		for elem in self.0.iter() {
			elem.push_async_arg(serializer)?;
		}
		Ok(())
	}
}

impl<T> ContractCallArg for MultiArgVec<T>
where
	T: ContractCallArg,
{
	fn push_async_arg(&self, serializer: &mut ArgBuffer) -> Result<(), SCError> {
		(&self).push_async_arg(serializer)
	}
}

impl<T: TypeAbi> TypeAbi for MultiArgVec<T> {
	fn type_name() -> String {
		let mut repr = String::from("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
	}
}