try_traits/ops/
bit.rs

1#[allow(unused)]
2use core::ops::{
3	Not,
4	BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign,
5	Shl, ShlAssign, Shr, ShrAssign
6};
7
8/// The try trait for [`Not`].
9pub trait TryNot {
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 [`Not::not`].
17	fn try_not(self) -> Result<Self::Output, Self::Error>;
18}
19
20/// The try trait for [`BitAnd`].
21pub trait TryBitAnd<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 [`BitAnd::bitand`].
29	fn try_bitand(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
30}
31
32/// The try trait for [`BitOr`].
33pub trait TryBitOr<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 [`BitOr::bitor`].
41	fn try_bitor(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
42}
43
44/// The try trait for [`BitXor`].
45pub trait TryBitXor<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 [`BitXor::bitxor`].
53	fn try_bitxor(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
54}
55
56/// The try trait for [`Shl`].
57pub trait TryShl<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 [`Shl::shl`].
65	fn try_shl(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
66}
67
68/// The try trait for [`Shr`].
69pub trait TryShr<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 [`Shr::shr`].
77	fn try_shr(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
78}
79
80/// The try trait for [`BitAndAssign`].
81pub trait TryBitAndAssign<Rhs = Self> {
82	/// The type returned in the event of an error.
83	type Error;
84
85	/// The fallible equivalent of [`BitAndAssign::bitand_assign`].
86	fn try_bitand_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error>;
87}
88
89/// The try trait for [`BitOrAssign`].
90pub trait TryBitOrAssign<Rhs = Self> {
91	/// The type returned in the event of an error.
92	type Error;
93
94	/// The fallible equivalent of [`BitOrAssign::bitor`].
95	fn try_bitor_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error>;
96}
97
98/// The try trait for [`BitXorAssign`].
99pub trait TryBitXorAssign<Rhs = Self> {
100	/// The type returned in the event of an error.
101	type Error;
102
103	/// The fallible equivalent of [`BitXorAssign::bitxor_assign`].
104	fn try_bitxor_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error>;
105}
106
107/// The try trait for [`ShlAssign`].
108pub trait TryShlAssign<Rhs = Self> {
109	/// The type returned in the event of an error.
110	type Error;
111
112	/// The fallible equivalent of [`ShlAssign::shl_assign`].
113	fn try_shl_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error>;
114}
115
116/// The try trait for [`ShrAssign`].
117pub trait TryShrAssign<Rhs = Self> {
118	/// The type returned in the event of an error.
119	type Error;
120
121	/// The fallible equivalent of [`ShrAssign::shr_assign`].
122	fn try_shr_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error>;
123}
124
125impl<T: Not> TryNot for T {
126	type Error = crate::Infallible;
127	type Output = <Self as Not>::Output;
128
129	#[inline]
130	fn try_not(self) -> Result<Self::Output, Self::Error> {
131		Ok(!self)
132	}
133}
134
135impl<T: BitAnd<Rhs>, Rhs> TryBitAnd<Rhs> for T {
136	type Error = crate::Infallible;
137	type Output = <Self as BitAnd<Rhs>>::Output;
138
139	#[inline]
140	fn try_bitand(self, rhs: Rhs) -> Result<Self::Output, Self::Error> {
141		Ok(self & rhs)
142	}
143}
144
145impl<T: BitOr<Rhs>, Rhs> TryBitOr<Rhs> for T {
146	type Error = crate::Infallible;
147	type Output = <Self as BitOr<Rhs>>::Output;
148
149	#[inline]
150	fn try_bitor(self, rhs: Rhs) -> Result<Self::Output, Self::Error> {
151		Ok(self | rhs)
152	}
153}
154
155impl<T: BitXor<Rhs>, Rhs> TryBitXor<Rhs> for T {
156	type Error = crate::Infallible;
157	type Output = <Self as BitXor<Rhs>>::Output;
158
159	#[inline]
160	fn try_bitxor(self, rhs: Rhs) -> Result<Self::Output, Self::Error> {
161		Ok(self ^ rhs)
162	}
163}
164
165impl<T: Shl<Rhs>, Rhs> TryShl<Rhs> for T {
166	type Error = crate::Infallible;
167	type Output = <Self as Shl<Rhs>>::Output;
168
169	#[inline]
170	fn try_shl(self, rhs: Rhs) -> Result<Self::Output, Self::Error> {
171		Ok(self << rhs)
172	}
173}
174
175impl<T: Shr<Rhs>, Rhs> TryShr<Rhs> for T {
176	type Error = crate::Infallible;
177	type Output = <Self as Shr<Rhs>>::Output;
178
179	#[inline]
180	fn try_shr(self, rhs: Rhs) -> Result<Self::Output, Self::Error> {
181		Ok(self >> rhs)
182	}
183}
184
185impl<T: BitAndAssign<Rhs>, Rhs> TryBitAndAssign<Rhs> for T {
186	type Error = crate::Infallible;
187
188	#[inline]
189	fn try_bitand_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error> {
190		Ok(self.bitand_assign(rhs))
191	}
192}
193
194impl<T: BitOrAssign<Rhs>, Rhs> TryBitOrAssign<Rhs> for T {
195	type Error = crate::Infallible;
196
197	#[inline]
198	fn try_bitor_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error> {
199		Ok(self.bitor_assign(rhs))
200	}
201}
202
203impl<T: BitXorAssign<Rhs>, Rhs> TryBitXorAssign<Rhs> for T {
204	type Error = crate::Infallible;
205
206	#[inline]
207	fn try_bitxor_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error> {
208		Ok(self.bitxor_assign(rhs))
209	}
210}
211
212impl<T: ShlAssign<Rhs>, Rhs> TryShlAssign<Rhs> for T {
213	type Error = crate::Infallible;
214
215	#[inline]
216	fn try_shl_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error> {
217		Ok(self.shl_assign(rhs))
218	}
219}
220
221impl<T: ShrAssign<Rhs>, Rhs> TryShrAssign<Rhs> for T {
222	type Error = crate::Infallible;
223
224	#[inline]
225	fn try_shr_assign(&mut self, rhs: Rhs) -> Result<(), Self::Error> {
226		Ok(self.shr_assign(rhs))
227	}
228}