Skip to main content

apple_quant_core/
checked_ops.rs

1use std::borrow::Borrow;
2
3pub trait AddReleaseUnchecked<
4	Rhs = Self,
5	UncheckedOutput = Self,
6	CheckedOutput = Option<UncheckedOutput>,
7>:
8	  AddChecked<Rhs, CheckedOutput>
9	+ AddUnchecked<Rhs, UncheckedOutput>
10where
11	UncheckedOutput: Into<CheckedOutput>,
12{
13	#[must_use]
14	fn add_release_unchecked(
15		self,
16		rhs: impl Borrow<Rhs>,
17	) -> CheckedOutput
18	where
19		Self: Sized,
20	{
21		#[cfg(debug_assertions)]
22		{ self.add_checked(rhs) }
23
24		#[cfg(not(debug_assertions))]
25		{ self.add_unchecked(rhs).into() }
26	}
27}
28
29pub trait SubReleaseUnchecked<
30	Rhs = Self,
31	UncheckedOutput = Self,
32	CheckedOutput = Option<UncheckedOutput>,
33>:
34	  SubChecked<Rhs, CheckedOutput>
35	+ SubUnchecked<Rhs, UncheckedOutput>
36where
37	UncheckedOutput: Into<CheckedOutput>,
38{
39	#[must_use]
40	fn sub_release_unchecked(
41		self,
42		rhs: impl Borrow<Rhs>,
43	) -> CheckedOutput
44	where
45		Self: Sized,
46	{
47		#[cfg(debug_assertions)]
48		{ self.sub_checked(rhs) }
49
50		#[cfg(not(debug_assertions))]
51		{ self.sub_unchecked(rhs).into() }
52	}
53}
54
55pub trait AddChecked<Rhs = Self, Output = Option<Self>> {
56	#[must_use]
57	fn add_checked(
58		self,
59		rhs: impl Borrow<Rhs>,
60	) -> Output
61	where
62		Self: Sized;
63}
64
65pub trait SubChecked<Rhs = Self, Output = Option<Self>> {
66	#[must_use]
67	fn sub_checked(
68		self,
69		rhs: impl Borrow<Rhs>,
70	) -> Output
71	where
72		Self: Sized;
73}
74
75pub trait AddUnchecked<Rhs = Self, Output = Self> {
76	#[must_use]
77	fn add_unchecked(
78		self,
79		rhs: impl Borrow<Rhs>,
80	) -> Output;
81}
82
83pub trait SubUnchecked<Rhs = Self, Output = Self> {
84	#[must_use]
85	fn sub_unchecked(
86		self,
87		rhs: impl Borrow<Rhs>,
88	) -> Output;
89}