/rust/registry/src/index.crates.io-1949cf8c6b5b557f/moxcms-0.7.11/src/safe_math.rs
Line | Count | Source |
1 | | /* |
2 | | * // Copyright (c) Radzivon Bartoshyk 3/2025. All rights reserved. |
3 | | * // |
4 | | * // Redistribution and use in source and binary forms, with or without modification, |
5 | | * // are permitted provided that the following conditions are met: |
6 | | * // |
7 | | * // 1. Redistributions of source code must retain the above copyright notice, this |
8 | | * // list of conditions and the following disclaimer. |
9 | | * // |
10 | | * // 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | * // this list of conditions and the following disclaimer in the documentation |
12 | | * // and/or other materials provided with the distribution. |
13 | | * // |
14 | | * // 3. Neither the name of the copyright holder nor the names of its |
15 | | * // contributors may be used to endorse or promote products derived from |
16 | | * // this software without specific prior written permission. |
17 | | * // |
18 | | * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | | * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | | * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
22 | | * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | | * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
24 | | * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
25 | | * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
26 | | * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | | * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | use crate::CmsError; |
30 | | use std::ops::Add; |
31 | | |
32 | | pub(crate) trait SafeAdd<T: Copy + Add<T, Output = T>> { |
33 | | fn safe_add(&self, other: T) -> Result<T, CmsError>; |
34 | | } |
35 | | |
36 | | pub(crate) trait SafeMul<T: Copy + Add<T, Output = T>> { |
37 | | fn safe_mul(&self, other: T) -> Result<T, CmsError>; |
38 | | } |
39 | | |
40 | | pub(crate) trait SafePowi<T: Copy + Add<T, Output = T>> { |
41 | | fn safe_powi(&self, power: u32) -> Result<T, CmsError>; |
42 | | } |
43 | | |
44 | | macro_rules! safe_add_impl { |
45 | | ($type_name: ident) => { |
46 | | impl SafeAdd<$type_name> for $type_name { |
47 | | #[inline(always)] |
48 | 0 | fn safe_add(&self, other: $type_name) -> Result<$type_name, CmsError> { |
49 | 0 | if let Some(result) = self.checked_add(other) { |
50 | 0 | return Ok(result); |
51 | 0 | } |
52 | 0 | Err(CmsError::OverflowingError) |
53 | 0 | } Unexecuted instantiation: <usize as moxcms::safe_math::SafeAdd<usize>>::safe_add Unexecuted instantiation: <u16 as moxcms::safe_math::SafeAdd<u16>>::safe_add Unexecuted instantiation: <i32 as moxcms::safe_math::SafeAdd<i32>>::safe_add Unexecuted instantiation: <isize as moxcms::safe_math::SafeAdd<isize>>::safe_add Unexecuted instantiation: <u32 as moxcms::safe_math::SafeAdd<u32>>::safe_add |
54 | | } |
55 | | }; |
56 | | } |
57 | | |
58 | | safe_add_impl!(u16); |
59 | | safe_add_impl!(u32); |
60 | | safe_add_impl!(i32); |
61 | | safe_add_impl!(usize); |
62 | | safe_add_impl!(isize); |
63 | | |
64 | | macro_rules! safe_mul_impl { |
65 | | ($type_name: ident) => { |
66 | | impl SafeMul<$type_name> for $type_name { |
67 | | #[inline(always)] |
68 | 0 | fn safe_mul(&self, other: $type_name) -> Result<$type_name, CmsError> { |
69 | 0 | if let Some(result) = self.checked_mul(other) { |
70 | 0 | return Ok(result); |
71 | 0 | } |
72 | 0 | Err(CmsError::OverflowingError) |
73 | 0 | } Unexecuted instantiation: <usize as moxcms::safe_math::SafeMul<usize>>::safe_mul Unexecuted instantiation: <u16 as moxcms::safe_math::SafeMul<u16>>::safe_mul Unexecuted instantiation: <u32 as moxcms::safe_math::SafeMul<u32>>::safe_mul Unexecuted instantiation: <i32 as moxcms::safe_math::SafeMul<i32>>::safe_mul Unexecuted instantiation: <isize as moxcms::safe_math::SafeMul<isize>>::safe_mul |
74 | | } |
75 | | }; |
76 | | } |
77 | | |
78 | | safe_mul_impl!(u16); |
79 | | safe_mul_impl!(u32); |
80 | | safe_mul_impl!(i32); |
81 | | safe_mul_impl!(usize); |
82 | | safe_mul_impl!(isize); |
83 | | |
84 | | macro_rules! safe_powi_impl { |
85 | | ($type_name: ident) => { |
86 | | impl SafePowi<$type_name> for $type_name { |
87 | | #[inline(always)] |
88 | 0 | fn safe_powi(&self, power: u32) -> Result<$type_name, CmsError> { |
89 | 0 | if let Some(result) = self.checked_pow(power) { |
90 | 0 | return Ok(result); |
91 | 0 | } |
92 | 0 | Err(CmsError::OverflowingError) |
93 | 0 | } Unexecuted instantiation: <usize as moxcms::safe_math::SafePowi<usize>>::safe_powi Unexecuted instantiation: <u32 as moxcms::safe_math::SafePowi<u32>>::safe_powi Unexecuted instantiation: <u8 as moxcms::safe_math::SafePowi<u8>>::safe_powi Unexecuted instantiation: <u16 as moxcms::safe_math::SafePowi<u16>>::safe_powi Unexecuted instantiation: <i32 as moxcms::safe_math::SafePowi<i32>>::safe_powi Unexecuted instantiation: <isize as moxcms::safe_math::SafePowi<isize>>::safe_powi |
94 | | } |
95 | | }; |
96 | | } |
97 | | |
98 | | safe_powi_impl!(u8); |
99 | | safe_powi_impl!(u16); |
100 | | safe_powi_impl!(u32); |
101 | | safe_powi_impl!(i32); |
102 | | safe_powi_impl!(usize); |
103 | | safe_powi_impl!(isize); |