Skip to content

Commit

Permalink
move data::convert types and traits to num and rename
Browse files Browse the repository at this point in the history
- rename  `Casting` to `Primiting`.
- rename `CastPrimitives` to `PrimitiveCast`.
- rename `FromPrimitives` to `PrimitiveJoin`.
- rename `IntoPrimitives` to `PrimitiveSplit`.
  • Loading branch information
joseluis committed Jan 20, 2024
1 parent 2a8e3ef commit ffc2d37
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 203 deletions.
21 changes: 0 additions & 21 deletions src/data/convert/cast/mod.rs

This file was deleted.

69 changes: 0 additions & 69 deletions src/data/convert/cast/tests.rs

This file was deleted.

25 changes: 0 additions & 25 deletions src/data/convert/mod.rs

This file was deleted.

11 changes: 3 additions & 8 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ mod error;
pub mod bit;
pub mod cmp;
pub mod collections;
pub mod convert;
pub mod hash;

#[allow(unused)]
#[cfg(not(feature = "data"))]
pub use {bit::*, cmp::*, collections::*, convert::*, error::*, hash::*};
pub use {bit::*, cmp::*, collections::*, error::*, hash::*};

/* feature-gated */

Expand Down Expand Up @@ -60,15 +59,11 @@ pub use error::*;
pub use dst::*;
#[doc(no_inline)]
#[cfg(feature = "data")]
pub use {
any::all::*, bit::all::*, cmp::all::*, collections::all::*, convert::all::*, hash::all::*,
};
pub use {any::all::*, bit::all::*, cmp::all::*, collections::all::*, hash::all::*};

pub(crate) mod all {
#[doc(inline)]
pub use super::{
bit::all::*, cmp::all::*, collections::all::*, convert::all::*, error::*, hash::all::*,
};
pub use super::{bit::all::*, cmp::all::*, collections::all::*, error::*, hash::all::*};

#[doc(inline)]
#[cfg(feature = "data")]
Expand Down
12 changes: 6 additions & 6 deletions src/num/int/wrapper/impl_combinatorics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
// - permute
// - permute_rep

use super::Int;
use crate::code::{cfor, iif, paste};
use crate::data::Casting;
use crate::num::{NumErrors as E, NumResult as Result};
use crate::{
code::{cfor, iif, paste},
num::{Int, NumErrors as E, NumResult as Result, Primiting},
};
use E::{MismatchedSizes, NonNegativeRequired, Overflow};

// $t: the input/output type
Expand Down Expand Up @@ -195,7 +195,7 @@ macro_rules! impl_combinatorics {
pub const fn permute_rep(self, r: $t) -> Result<Int<$t>> {
let n = self.0;
iif![n < 0 || r < 0; return Err(NonNegativeRequired)];
let r_u32 = if let Ok(res) = Casting(r).checked_cast_to_u32() {
let r_u32 = if let Ok(res) = Primiting(r).checked_cast_to_u32() {
res
} else {
return Err(Overflow(None));
Expand Down Expand Up @@ -455,7 +455,7 @@ macro_rules! impl_combinatorics {
#[inline]
pub const fn permute_rep(self, r: $t) -> Result<Int<$t>> {
let n = self.0;
let r_u32 = if let Ok(res) = Casting(r).checked_cast_to_u32() {
let r_u32 = if let Ok(res) = Primiting(r).checked_cast_to_u32() {
res
} else {
return Err(Overflow(None));
Expand Down
6 changes: 4 additions & 2 deletions src/num/int/wrapper/impl_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
// - gcd_ext_euc

use super::Int;
use crate::code::{iif, paste};
use crate::num::{isize_up, usize_up, NumErrors, NumResult as Result};
use crate::{
code::{iif, paste},
num::{isize_up, usize_up, Casting, NumErrors, NumResult as Result},
};
use NumErrors::Overflow;

// $t: the input/output type
Expand Down
9 changes: 6 additions & 3 deletions src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod _private;
mod alias;
mod error;
mod float;
mod primitive;
mod sign;

pub mod niche;
Expand All @@ -22,7 +23,7 @@ pub mod ops;
pub(crate) use _private::*;
#[allow(unused)]
#[cfg(not(feature = "num"))]
pub use {alias::*, error::*, float::*, niche::*, ops::*, sign::*};
pub use {alias::*, error::*, float::*, niche::*, ops::*, primitive::*, sign::*};

/* feature-gated */

Expand All @@ -37,7 +38,7 @@ mod r#trait;

// re-export private sub-modules
#[cfg(feature = "num")]
pub use {alias::*, error::*, float::*, frac::*, int::*, no::*, r#trait::*, sign::*};
pub use {alias::*, error::*, float::*, frac::*, int::*, no::*, primitive::*, r#trait::*, sign::*};

// re-export public sub-modules
#[doc(no_inline)]
Expand All @@ -46,7 +47,9 @@ pub use {niche::all::*, ops::all::*};

pub(crate) mod all {
#[doc(inline)]
pub use super::{alias::*, error::*, float::*, niche::all::*, ops::all::*, sign::*};
pub use super::{
alias::*, error::*, float::*, niche::all::*, ops::all::*, primitive::*, sign::*,
};

#[doc(inline)]
#[cfg(feature = "num")]
Expand Down
33 changes: 17 additions & 16 deletions src/data/convert/cast/casts.rs → src/num/primitive/casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
// - trait and wrapper implementation
// - functions definitions

use super::Casting;
use crate::{
code::{iif, paste},
data::{DataErrors as E, DataResult as Result},
num::{NumErrors, NumResult as Result, Primiting, Sign},
};
use {
NumErrors::Overflow,
Sign::{Negative, Positive},
};
#[cfg(doc)]
use E::{Overflow, Underflow};

/// Offers methods for casting between primitives.
///
/// See also the [`Primiting`] type for the equivalent *const* methods.
/// # Errors
/// Checked methods will return [`Overflow`] or [`Underflow`] if `self` value can't fit
/// in the returned type.
/// Checked methods will return [`Overflow`]
/// if the original value can't fit in the returned type.
#[cfg_attr(feature = "nightly", doc(cfg(feature = "data")))]
pub trait CastPrimitives {
pub trait PrimitiveCast {
/// Casts `self` to `u8` with range check.
fn checked_cast_to_u8(self) -> Result<u8>;
/// Casts `self` to `u16` with range check.
Expand Down Expand Up @@ -84,11 +86,11 @@ pub trait CastPrimitives {
fn saturating_cast_to_isize(self) -> isize;
}

// Implements the public casting methods for the trait and Casting wrapper
// Implements the public casting methods for the trait and Primiting wrapper
macro_rules! impl_cast_methods {
($($t:ty),+) => { $( impl_cast_methods![@$t]; )+ };
(@$t:ty) => { paste! {
impl CastPrimitives for $t {
impl PrimitiveCast for $t {
#[inline(always)]
fn checked_cast_to_u8(self) -> Result<u8> { [<checked_cast_ $t _to_u8>](self) }
#[inline(always)]
Expand Down Expand Up @@ -142,9 +144,8 @@ macro_rules! impl_cast_methods {
/// Checked casts and saturating casts.
///
/// # Errors
/// Checked methods will return [`Overflow`] or [`Underflow`] if `self` value can't fit
/// in the returned type.
impl Casting<$t> {
/// Checked methods will return [`Overflow`] if `self` can't fit in the returned type.
impl Primiting<$t> {
#[doc = "Casts from `" $t "` to `u8` with range check."]
#[inline(always)]
pub const fn checked_cast_to_u8(self) -> Result<u8> {
Expand Down Expand Up @@ -432,9 +433,9 @@ macro_rules! impl_cast_fns {
#[inline]
const fn [<checked_cast_ $f _to_ $t>](p: $f) -> Result<$t> {
if p < <$t>::MIN as $f {
Err(E::Underflow)
Err(Overflow(Some(Negative)))
} else if p > $t::MAX as $f {
Err(E::Overflow)
Err(Overflow(Some(Positive)))
} else {
Ok(p as $t)
}
Expand All @@ -454,7 +455,7 @@ macro_rules! impl_cast_fns {
(@can_overflow $f:ty:$t:ty) => { paste! {
#[inline]
const fn [<checked_cast_ $f _to_ $t>](p: $f) -> Result<$t> {
iif![p > <$t>::MAX as $f; Err(E::Overflow); Ok(p as $t)]
iif![p > <$t>::MAX as $f; Err(Overflow(Some(Positive))); Ok(p as $t)]
}
#[must_use] #[inline]
const fn [<saturating_cast_ $f _to_ $t>](p: $f) -> $t {
Expand All @@ -465,7 +466,7 @@ macro_rules! impl_cast_fns {
(@can_underflow $f:ty:$t:ty) => { paste! {
#[inline]
const fn [<checked_cast_ $f _to_ $t>](p: $f) -> Result<$t> {
iif![p < 0; Err(E::Underflow); Ok(p as $t)]
iif![p < 0; Err(Overflow(Some(Negative))); Ok(p as $t)]
}
#[must_use] #[inline]
const fn [<saturating_cast_ $f _to_ $t>](p: $f) -> $t {
Expand Down
Loading

0 comments on commit ffc2d37

Please sign in to comment.