Skip to main content

apple_quant_algorithmic/volume/zeroable/
ext.rs

1use apple_quant_core::log::error;
2
3pub(crate) const ZERO_ERROR: &'static str = "ZeroableExt operation failed while `debug_assertions`.";
4
5pub trait ZeroableExt<T: Sized>
6where
7	Self: Sized,
8{
9	fn as_optional_nonzero_ref(
10		&self,
11	) -> Option<&T>;
12
13	fn as_optional_nonzero_mut(
14		&mut self,
15	) -> Option<&mut T>;
16
17	fn into_optional_nonzero(
18		self,
19	) -> Option<T>;
20
21	fn as_zeroable(
22		&self,
23	) -> T;
24
25	fn into_zeroable(
26		self,
27	) -> T;
28
29	fn as_nonzero_unchecked_ref(
30		&self,
31	) -> &T;
32
33	fn as_nonzero_unchecked_mut(
34		&mut self,
35	) -> &mut T;
36
37	fn into_nonzero_unchecked(
38		self,
39	) -> T;
40
41	fn as_nonzero_unchecked_release(
42		&self,
43	) -> &T {
44		#[cfg(debug_assertions)]
45		match self.as_optional_nonzero_ref() {
46			Some(t) => t,
47			None => {
48				error!("{ZERO_ERROR}");
49				panic!();
50			},
51		}
52
53		#[cfg(not(debug_assertions))]
54		self.as_nonzero_unchecked_ref()
55	}
56
57	fn as_nonzero_unchecked_release_mut(
58		&mut self,
59	) -> &mut T {
60		#[cfg(debug_assertions)]
61		match self.as_optional_nonzero_mut() {
62			Some(t) => t,
63			None => {
64				error!("{ZERO_ERROR}");
65				panic!();
66			},
67		}
68
69		#[cfg(not(debug_assertions))]
70		self.as_nonzero_unchecked_mut()
71	}
72
73	fn into_nonzero_unchecked_release(
74		self,
75	) -> T {
76		#[cfg(debug_assertions)]
77		match self.into_optional_nonzero() {
78			Some(t) => t,
79			None => {
80				error!("{ZERO_ERROR}");
81				panic!();
82			},
83		}
84
85		#[cfg(not(debug_assertions))]
86		self.into_nonzero_unchecked()
87	}
88}