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

pub type CowStr = std::borrow::Cow<'static, str>;
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::new(f)
}

#[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 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)
	}
}