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
use crate::uses::{ops::*, *};

pub type Res<T> = Result<T, String>;
pub type STR = &'static str;

pub fn lambda<'a, T: Fn(A) -> R + 'a, A, R>(f: T) -> Box<dyn Fn(A) -> R + 'a> {
	Box(f)
}

pub trait UnwrapValid<T> {
	fn valid(self) -> T;
}
impl<T> UnwrapValid<T> for Option<T> {
	fn valid(self) -> T {
		#[cfg(debug_assertions)]
		{
			self.unwrap()
		}
		#[cfg(not(debug_assertions))]
		{
			unsafe { self.unwrap_unchecked() }
		}
	}
}
impl<T, E: Debug> UnwrapValid<T> for Result<T, E> {
	fn valid(self) -> T {
		#[cfg(debug_assertions)]
		{
			self.unwrap()
		}
		#[cfg(not(debug_assertions))]
		{
			unsafe { self.unwrap_unchecked() }
		}
	}
}

#[macro_export]
macro_rules! map_enum {
	($t: pat = $e: expr => $do: expr) => {{
		if let $t = $e {
			Some($do)
		} else {
			None
		}
	}};
}

pub fn Def<T: Default>() -> T {
	Default::default()
}

pub fn Box<T>(v: T) -> Box<T> {
	Box::new(v)
}

pub trait OptionSink {
	fn sink(self);
}
impl<T> OptionSink for Option<T> {
	fn sink(self) {
		let _ = self;
	}
}

pub trait OrAssignment {
	fn or_def(self, filter: bool) -> Self;
	fn or_val(self, filter: bool, val: Self) -> Self;
}
impl<T: Default> OrAssignment for T {
	#[inline(always)]
	fn or_def(self, filter: bool) -> Self {
		if filter {
			self
		} else {
			Def()
		}
	}
	#[inline(always)]
	fn or_val(self, filter: bool, v: Self) -> Self {
		if filter {
			self
		} else {
			v
		}
	}
}

pub trait LerpMix: Cast<f32> {
	fn mix<M>(self, a: M, r: Self) -> Self
	where
		f32: Cast<M>;
}
impl<T: Cast<f32> + Copy + Add<Output = T> + Mul<Output = T>> LerpMix for T {
	fn mix<M>(self, a: M, r: Self) -> Self
	where
		f32: Cast<M>,
	{
		let a = f32(a);
		self * Self::to(1. - a) + r * Self::to(a)
	}
}