Skip to content

Commit

Permalink
replace Cast with NumericCast
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine authored and rogercloud committed May 27, 2022
1 parent bd79698 commit 6450ffd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 30 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ readme = "README.md"
license = "MIT"
keywords = ["utilities"]
categories = ["utilities"]

[dependencies]
numeric_cast = "0.1.2"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This utility lib helps for type casting and integer operation overflow checking.

```rust
let a: u64 = 10;
let b: i64 = a.cast();
let b: i64 = a.numeric_cast();
```

```rust
Expand Down
16 changes: 12 additions & 4 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A trait for overflowing arithmetic
use crate::conversion::Cast;
use crate::conversion::NumericCast;
use std::any::type_name;

/// Macros for checking arithmetic overflow, panicing when overflow happens.
Expand Down Expand Up @@ -66,7 +66,7 @@ macro_rules! impl_overflow_arithmetic {

#[inline]
fn overflow_shl(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_shl(other.cast());
let (res, overflow) = self.overflowing_shl(other.numeric_cast());
assert!(
!overflow,
"number = {}({}) left shift number = {}({}) overflowed",
Expand All @@ -80,7 +80,7 @@ macro_rules! impl_overflow_arithmetic {

#[inline]
fn overflow_shr(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_shr(other.cast());
let (res, overflow) = self.overflowing_shr(other.numeric_cast());
assert!(
!overflow,
"number = {}({}) right shift number = {}({}) overflowed",
Expand All @@ -106,7 +106,7 @@ macro_rules! impl_overflow_arithmetic {

#[inline]
fn overflow_rem(self, other: Self) -> Self {
let (res, overflow) = self.overflowing_rem(other.cast());
let (res, overflow) = self.overflowing_rem(other.numeric_cast());
assert!(
!overflow,
"number = {}({}) remainder number = {}({}) overflowed",
Expand Down Expand Up @@ -137,26 +137,34 @@ impl_overflow_arithmetic!(isize);
/// A type cast trait used to do the integer arithmetic.
pub trait OverflowArithmetic {
/// Overflow add.
#[must_use]
fn overflow_add(self, other: Self) -> Self;

/// Overflow sub.
#[must_use]
fn overflow_sub(self, other: Self) -> Self;

/// Overflow mul.
#[must_use]
fn overflow_mul(self, other: Self) -> Self;

/// Overflow div.
#[must_use]
fn overflow_div(self, other: Self) -> Self;

/// Overflow shl.
#[must_use]
fn overflow_shl(self, other: Self) -> Self;

/// Overflow shr.
#[must_use]
fn overflow_shr(self, other: Self) -> Self;

/// Overflow neg.
#[must_use]
fn overflow_neg(self) -> Self;

/// Overflow rem.
#[must_use]
fn overflow_rem(self, other: Self) -> Self;
}
25 changes: 1 addition & 24 deletions src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
//! A trait used to replace as conversion
use std::any::type_name;
use std::convert::TryFrom;

/// A type cast trait used to replace as conversion.
pub trait Cast {
/// Performs the conversion.
#[inline]
fn cast<T>(self) -> T
where
T: TryFrom<Self>,
Self: Sized + std::fmt::Display + Copy,
{
T::try_from(self).unwrap_or_else(|_| {
panic!(
"Failed to convert from {}: {} to {}",
type_name::<Self>(),
self,
type_name::<T>(),
)
})
}
}

impl<U> Cast for U {}
pub use numeric_cast::NumericCast;

/// Cast to pointer
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ mod arithmetic;
mod conversion;

pub use crate::arithmetic::OverflowArithmetic;
pub use crate::conversion::{cast_to_mut_ptr, cast_to_ptr, Cast};
pub use crate::conversion::{cast_to_mut_ptr, cast_to_ptr, NumericCast};

0 comments on commit 6450ffd

Please sign in to comment.