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
use crate::abi::{TypeAbi, TypeDescriptionContainer};
use crate::io::{ArgId, DynArg, DynArgInput, DynArgMulti};
use alloc::string::String;
use alloc::vec::Vec;
use dharitri_codec::TopDecodeInput;

/// Structure that allows taking a variable number of arguments in a smart contract function.
pub struct VarArgs<T>(pub Vec<T>);

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

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

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

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

impl<I, D, T> DynArgMulti<I, D> for VarArgs<T>
where
	I: TopDecodeInput,
	D: DynArgInput<I>,
	T: DynArg<I, D>,
{
	fn dyn_load_multi(loader: &mut D, arg_id: ArgId, num: usize) -> Self {
		let mut result_vec: Vec<T> = Vec::new();
		let mut i = 0usize;
		while loader.has_next() && i < num {
			result_vec.push(T::dyn_load(loader, arg_id));
			i += 1;
		}
		if i < num {
			loader.signal_arg_wrong_number();
		}
		VarArgs(result_vec)
	}
}

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