add Expected trait to utils; use (already transitive) num-traits.
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
7e4453620e
commit
1a71798859
5 changed files with 60 additions and 0 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -730,6 +730,7 @@ dependencies = [
|
|||
"libloading",
|
||||
"log",
|
||||
"nix",
|
||||
"num-traits",
|
||||
"rand",
|
||||
"regex",
|
||||
"reqwest",
|
||||
|
|
|
@ -501,6 +501,9 @@ version = "0.8.1"
|
|||
[workspace.dependencies.libc]
|
||||
version = "0.2"
|
||||
|
||||
[workspace.dependencies.num-traits]
|
||||
version = "0.2"
|
||||
|
||||
#
|
||||
# Patches
|
||||
#
|
||||
|
|
|
@ -77,6 +77,7 @@ itertools.workspace = true
|
|||
libc.workspace = true
|
||||
libloading.workspace = true
|
||||
log.workspace = true
|
||||
num-traits.workspace = true
|
||||
rand.workspace = true
|
||||
regex.workspace = true
|
||||
reqwest.workspace = true
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
mod expected;
|
||||
|
||||
use std::{cmp, convert::TryFrom};
|
||||
|
||||
pub use checked_ops::checked_ops;
|
||||
|
||||
pub use self::expected::Expected;
|
||||
use crate::{debug::type_name, err, Err, Error, Result};
|
||||
|
||||
/// Checked arithmetic expression. Returns a Result<R, Error::Arithmetic>
|
||||
|
|
52
src/core/utils/math/expected.rs
Normal file
52
src/core/utils/math/expected.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use num_traits::ops::checked::{CheckedAdd, CheckedDiv, CheckedMul, CheckedRem, CheckedSub};
|
||||
|
||||
use crate::expected;
|
||||
|
||||
pub trait Expected {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn expected_add(self, rhs: Self) -> Self
|
||||
where
|
||||
Self: CheckedAdd + Sized,
|
||||
{
|
||||
expected!(self + rhs)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn expected_sub(self, rhs: Self) -> Self
|
||||
where
|
||||
Self: CheckedSub + Sized,
|
||||
{
|
||||
expected!(self - rhs)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn expected_mul(self, rhs: Self) -> Self
|
||||
where
|
||||
Self: CheckedMul + Sized,
|
||||
{
|
||||
expected!(self * rhs)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn expected_div(self, rhs: Self) -> Self
|
||||
where
|
||||
Self: CheckedDiv + Sized,
|
||||
{
|
||||
expected!(self / rhs)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
fn expected_rem(self, rhs: Self) -> Self
|
||||
where
|
||||
Self: CheckedRem + Sized,
|
||||
{
|
||||
expected!(self % rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Expected for T {}
|
Loading…
Add table
Add a link
Reference in a new issue