try_traits/ops/
arith.rs

1#[allow(unused)]
2use core::ops::{
3	Neg,
4	Add, AddAssign, Sub, SubAssign,
5	Mul, MulAssign, Div, DivAssign, Rem, RemAssign
6};
7
8/// The try trait for [`Neg`].
9pub trait TryNeg {
10	/// The type returned in the event of an error.
11	type Error;
12
13	/// The type returned after performing the operation.
14	type Output;
15
16	/// The fallible equivalent of [`Neg::neg`].
17	fn try_neg(self) -> Result<Self::Output, Self::Error>;
18}
19
20/// The try trait for [`Add`].
21pub trait TryAdd<Rhs = Self> {
22	/// The type returned in the event of an error.
23	type Error;
24
25	/// The type returned after performing the operation.
26	type Output;
27
28	/// The fallible equivalent of [`Add::add`].
29	fn try_add(self, other: Rhs) -> Result<Self::Output, Self::Error>;
30}
31
32/// The try trait for [`Sub`].
33pub trait TrySub<Rhs = Self> {
34	/// The type returned in the event of an error.
35	type Error;
36
37	/// The type returned after performing the operation.
38	type Output;
39
40	/// The fallible equivalent of [`Sub::sub`].
41	fn try_sub(self, other: Rhs) -> Result<Self::Output, Self::Error>;
42}
43
44/// The try trait for [`Mul`].
45pub trait TryMul<Rhs = Self> {
46	/// The type returned in the event of an error.
47	type Error;
48
49	/// The type returned after performing the operation.
50	type Output;
51
52	/// The fallible equivalent of [`Mul::mul`].
53	fn try_mul(self, other: Rhs) -> Result<Self::Output, Self::Error>;
54}
55
56/// The try trait for [`Div`].
57pub trait TryDiv<Rhs = Self> {
58	/// The type returned in the event of an error.
59	type Error;
60
61	/// The type returned after performing the operation.
62	type Output;
63
64	/// The fallible equivalent of [`Div::div`].
65	fn try_div(self, other: Rhs) -> Result<Self::Output, Self::Error>;
66}
67
68/// The try trait for [`Rem`].
69pub trait TryRem<Rhs = Self> {
70	/// The type returned in the event of an error.
71	type Error;
72
73	/// The type returned after performing the operation.
74	type Output;
75
76	/// The fallible equivalent of [`Rem::rem`].
77	fn try_rem(self, other: Rhs) -> Result<Self::Output, Self::Error>;
78}
79
80/// The try trait for [`AddAssign`].
81pub trait TryAddAssign<Rhs = Self> {
82	/// The type returned in the event of an error.
83	type Error;
84
85	/// The fallible equivalent of [`AddAssign::add_assign`].
86	fn try_add_assign(&mut self, other: Rhs) -> Result<(), Self::Error>;
87}
88
89/// The try trait for [`SubAssign`].
90pub trait TrySubAssign<Rhs = Self> {
91	/// The type returned in the event of an error.
92	type Error;
93
94	/// The fallible equivalent of [`SubAssign::sub_assign`].
95	fn try_sub_assign(&mut self, other: Rhs) -> Result<(), Self::Error>;
96}
97
98/// The try trait for [`MulAssign`].
99pub trait TryMulAssign<Rhs = Self> {
100	/// The type returned in the event of an error.
101	type Error;
102
103	/// The fallible equivalent of [`MulAssign::mul_assign`].
104	fn try_mul_assign(&mut self, other: Rhs) -> Result<(), Self::Error>;
105}
106
107/// The try trait for [`DivAssign`].
108pub trait TryDivAssign<Rhs = Self> {
109	/// The type returned in the event of an error.
110	type Error;
111
112	/// The fallible equivalent of [`DivAssign::div_assign`].
113	fn try_div_assign(&mut self, other: Rhs) -> Result<(), Self::Error>;
114}
115
116/// The try trait for [`RemAssign`].
117pub trait TryRemAssign<Rhs = Self> {
118	/// The type returned in the event of an error.
119	type Error;
120
121	/// The fallible equivalent of [`RemAssign::rem_assign`].
122	fn try_rem_assign(&mut self, other: Rhs) -> Result<(), Self::Error>;
123}
124
125impl<T: Add<Rhs>, Rhs> TryAdd<Rhs> for T {
126	type Error = crate::Infallible;
127	type Output = <Self as Add<Rhs>>::Output;
128
129	#[inline]
130	fn try_add(self, other: Rhs) -> Result<Self::Output, Self::Error> {
131		Ok(self + other)
132	}
133}
134
135impl<T: Sub<Rhs>, Rhs> TrySub<Rhs> for T {
136	type Error = crate::Infallible;
137	type Output = <Self as Sub<Rhs>>::Output;
138
139	#[inline]
140	fn try_sub(self, other: Rhs) -> Result<Self::Output, Self::Error> {
141		Ok(self - other)
142	}
143}
144
145impl<T: Mul<Rhs>, Rhs> TryMul<Rhs> for T {
146	type Error = crate::Infallible;
147	type Output = <Self as Mul<Rhs>>::Output;
148
149	#[inline]
150	fn try_mul(self, other: Rhs) -> Result<Self::Output, Self::Error> {
151		Ok(self * other)
152	}
153}
154
155impl<T: Div<Rhs>, Rhs> TryDiv<Rhs> for T {
156	type Error = crate::Infallible;
157	type Output = <Self as Div<Rhs>>::Output;
158
159	#[inline]
160	fn try_div(self, other: Rhs) -> Result<Self::Output, Self::Error> {
161		Ok(self / other)
162	}
163}
164
165impl<T: Rem<Rhs>, Rhs> TryRem<Rhs> for T {
166	type Error = crate::Infallible;
167	type Output = <Self as Rem<Rhs>>::Output;
168
169	#[inline]
170	fn try_rem(self, other: Rhs) -> Result<Self::Output, Self::Error> {
171		Ok(self % other)
172	}
173}
174
175impl<T: AddAssign<Rhs>, Rhs> TryAddAssign<Rhs> for T {
176	type Error = crate::Infallible;
177
178	#[inline]
179	fn try_add_assign(&mut self, other: Rhs) -> Result<(), Self::Error> {
180		Ok(self.add_assign(other))
181	}
182}
183
184impl<T: SubAssign<Rhs>, Rhs> TrySubAssign<Rhs> for T {
185	type Error = crate::Infallible;
186
187	#[inline]
188	fn try_sub_assign(&mut self, other: Rhs) -> Result<(), Self::Error> {
189		Ok(self.sub_assign(other))
190	}
191}
192
193impl<T: MulAssign<Rhs>, Rhs> TryMulAssign<Rhs> for T {
194	type Error = crate::Infallible;
195
196	#[inline]
197	fn try_mul_assign(&mut self, other: Rhs) -> Result<(), Self::Error> {
198		Ok(self.mul_assign(other))
199	}
200}
201
202impl<T: DivAssign<Rhs>, Rhs> TryDivAssign<Rhs> for T {
203	type Error = crate::Infallible;
204
205	#[inline]
206	fn try_div_assign(&mut self, other: Rhs) -> Result<(), Self::Error> {
207		Ok(self.div_assign(other))
208	}
209}
210
211impl<T: RemAssign<Rhs>, Rhs> TryRemAssign<Rhs> for T {
212	type Error = crate::Infallible;
213
214	#[inline]
215	fn try_rem_assign(&mut self, other: Rhs) -> Result<(), Self::Error> {
216		Ok(self.rem_assign(other))
217	}
218}