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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#![ allow (clippy::inline_always) ]

mod collections;
mod iter;
pub mod prelude;

mod result {

	pub use crate::prelude::*;

	pub use crate::ok_or;
	pub use crate::ok_or_else;
	pub use crate::assert_is_err;
	pub use crate::assert_is_ok;
	pub use crate::assert_eq_ok;
	pub use crate::assert_err;

	pub type GenResult <Ok> = Result <Ok, GenError>;

	pub trait ResultEither <Val> {
		fn either (self) -> Val;
	}

	impl <Val> ResultEither <Val> for Result <Val, Val> {
		#[ inline (always) ]
		#[ must_use ]
		fn either (self) -> Val {
			match self { Ok (val) => val, Err (val) => val }
		}
	}

	#[ allow (clippy::missing_const_for_fn) ]
	#[ inline (always) ]
	#[ must_use ]
	pub fn ok_or_err <Val> (result: Result <Val, Val>) -> Val {
		match result {
			Ok (val) => val,
			Err (val) => val,
		}
	}

	pub trait ResultMapRef <Val, Error: Copy> {
		fn map_ref <Out> (& self, map_fn: impl FnMut (& Val) -> Out) -> Result <Out, Error>;
	}

	impl <Val, Error: Copy> ResultMapRef <Val, Error> for Result <Val, Error> {
		#[ inline (always) ]
		fn map_ref <Out> (& self, map_fn: impl FnMut (& Val) -> Out) -> Result <Out, Error> {
			self.as_ref ().map_err (|& err| err).map (map_fn)
		}
	}

	#[ macro_export ]
	macro_rules! ok_or {
		( $val:expr, $if_err:expr $(,)? ) => {
			match ($val) { Ok (val) => val, Err (_) => $if_err }
		};
	}

	#[ macro_export ]
	macro_rules! ok_or_else {
		( $val:expr, |$arg:ident| $if_err:expr $(,)? ) => {
			match ($val) { Ok (val) => val, Err ($arg) => $if_err }
		};
	}

	#[ macro_export ]
	macro_rules! assert_is_ok {
		( $actual:expr ) => {
			assert! ($actual.is_ok ());
		};
	}

	#[ macro_export ]
	macro_rules! assert_eq_ok {
		( $expect:expr , $actual:expr ) => {
			let actual = $actual;
			assert! (actual.is_ok (), "Expected Ok but got {:?}", actual);
			assert_eq! ($expect, actual.unwrap ());
		};
		( $expect: expr , $actual:expr , $($rest:tt)* ) => {
			let actual = $actual;
			assert! (actual.is_ok (), $($rest)*);
			assert_eq! ($expect, actual.unwrap ());
		};
	}

	#[ macro_export ]
	macro_rules! assert_err {
		( $expect:expr , $actual:expr ) => {
			assert_eq! ($expect, $actual.unwrap_err ().to_string ());
		};
	}

	#[ macro_export ]
	macro_rules! assert_is_err {
		( $actual:expr ) => {
			let actual = $actual;
			assert! (actual.is_err (), "Expected Err (_) but got {actual:?}");
		};
	}

}

mod error {

	use crate::prelude::*;

	pub type GenError = Box <dyn Error>;

	#[ allow (non_snake_case) ]
	#[ inline ]
	pub fn GenOk <Val> (val: Val) -> GenResult <Val> {
		Ok (val)
	}

}

mod default {

	#[ inline (always) ]
	#[ must_use ]
	pub fn default <T: Default> () -> T {
		Default::default ()
	}

}

mod option {

	pub use crate::some_or;

	#[ macro_export ]
	macro_rules! some_or {
		( $val:expr, $if_err:expr $(,)? ) => {
			match ($val) { Some (val) => val, None => $if_err }
		};
	}

}

mod array_vec {

	pub use crate::array_vec;

	#[ macro_export ]
	macro_rules! array_vec {
		() => { ArrayVec::new () };
		( $($val:expr),* $(,)? ) => {
			{
				let mut result = ArrayVec::new ();
				$( result.push ($val); )*
				result
			}
		};
		( $val:expr; $num:expr ) => {
			[$val; $num].into_iter ().collect ()
		};
	}

}

mod deref {

	pub use crate::wrapper_deref;
	pub use crate::wrapper_deref_mut;

	#[ macro_export ]
	macro_rules! wrapper_deref {
		(
			$(#[$struct_meta:meta])*
			$struct_vis:vis struct $struct_name:ident $(<$($struct_param:tt),*>)? {
				$field_vis:vis $field_name:ident: $field_type:ty,
			}
		) => {

			$(#[$struct_meta])*
			$struct_vis struct $struct_name $(<$($struct_param),*>)? {
				$field_vis $field_name: $field_type,
			}

			impl $(<$($struct_param),*>)? ::std::ops::Deref for $struct_name $(<$($struct_param),*>)? {
				type Target = $field_type;
				#[ inline ]
				fn deref (& self) -> & $field_type {
					& self.$field_name
				}
			}

		};
	}

	#[ macro_export ]
	macro_rules! wrapper_deref_mut {
		(
			$(#[$struct_meta:meta])*
			$struct_vis:vis struct $struct_name:ident $(<$($struct_param:tt),*>)? {
				$field_vis:vis $field_name:ident: $field_type:ty,
			}
		) => {

			$(#[$struct_meta])*
			$struct_vis struct $struct_name $(<$($struct_param),*>)? {
				$field_vis $field_name: $field_type,
			}

			impl $(<$($struct_param),*>)? ::std::ops::Deref for $struct_name $(<$($struct_param),*>)? {
				type Target = $field_type;
				#[ inline ]
				fn deref (& self) -> & $field_type {
					& self.$field_name
				}
			}

			impl $(<$($struct_param),*>)? ::std::ops::DerefMut for $struct_name $(<$($struct_param),*>)? {
				#[ inline ]
				fn deref_mut (& mut self) -> & mut $field_type {
					& mut self.$field_name
				}
			}

		};
	}

}