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
use std::{cmp, ops};
pub const DEFAULT_PORT: u16 = 15384;
pub fn is_valid_name(name: &str) -> bool { name.trim().len() >= 3 && name.len() <= 31 }
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Finite(f64);
impl Finite {
pub fn new(f: f64) -> Self {
assert!(f.is_finite(), "Attempt to create Finite with non-finite float");
Self(f)
}
pub fn value(self) -> f64 { self.0 }
}
impl Eq for Finite {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Finite {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.partial_cmp(other).expect("Values should be finite")
}
}
pub fn lerp<T, U>(a: T, b: T, ratio: U) -> T
where
T: Copy + ops::Add<Output = T> + ops::Sub<Output = T> + ops::Mul<U, Output = T>,
{
a + (b - a) * ratio
}
#[inline(always)]
pub fn is_default<T: PartialEq + Default>(value: &T) -> bool { *value == T::default() }