Skip to content

Commit

Permalink
Merge pull request #367 from tgross35/type-helpers
Browse files Browse the repository at this point in the history
Introduce helper types for accessing trait items
  • Loading branch information
tgross35 authored Dec 22, 2024
2 parents 145dbc4 + 498a11f commit ebdb38b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/libm-test/benches/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro_rules! musl_rand_benches {

#[cfg(feature = "build-musl")]
let musl_extra = MuslExtra {
musl_fn: Some(musl_math_sys::$fn_name as <Op as MathOp>::CFn),
musl_fn: Some(musl_math_sys::$fn_name as libm_test::CFn<Op>),
skip_on_i586: $skip_on_i586
};

Expand Down
4 changes: 2 additions & 2 deletions crates/libm-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub mod op;
mod precision;
mod test_traits;

pub use libm::support::{Float, Int};
pub use op::{BaseName, Identifier, MathOp};
pub use libm::support::{Float, Int, IntTy};
pub use op::{BaseName, CFn, FTy, Identifier, MathOp, RustFn, RustRet};
pub use precision::{MaybeOverride, SpecialCase, default_ulp};
pub use test_traits::{CheckBasis, CheckCtx, CheckOutput, GenerateInput, Hex, TupleCall};

Expand Down
9 changes: 9 additions & 0 deletions crates/libm-test/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ pub trait MathOp {
const ROUTINE: Self::RustFn;
}

/// Access the associated `FTy` type from an op (helper to avoid ambiguous associated types).
pub type FTy<Op> = <Op as MathOp>::FTy;
/// Access the associated `CFn` type from an op (helper to avoid ambiguous associated types).
pub type CFn<Op> = <Op as MathOp>::CFn;
/// Access the associated `RustFn` type from an op (helper to avoid ambiguous associated types).
pub type RustFn<Op> = <Op as MathOp>::RustFn;
/// Access the associated `RustRet` type from an op (helper to avoid ambiguous associated types).
pub type RustRet<Op> = <Op as MathOp>::RustRet;

macro_rules! do_thing {
// Matcher for unary functions
(
Expand Down
4 changes: 4 additions & 0 deletions src/math/support/float_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ pub trait Float:
}
}

/// Access the associated `Int` type from a float (helper to avoid ambiguous associated types).
#[allow(dead_code)]
pub type IntTy<F> = <F as Float>::Int;

macro_rules! float_impl {
($ty:ident, $ity:ident, $sity:ident, $expty:ident, $bits:expr, $significand_bits:expr) => {
impl Float for $ty {
Expand Down
6 changes: 5 additions & 1 deletion src/math/support/int_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub trait MinInt:
const MAX: Self;
}

/// Access the associated `OtherSign` type from an int (helper to avoid ambiguous associated
/// types).
pub type OtherSign<I> = <I as MinInt>::OtherSign;

/// Trait for some basic operations on integers
#[allow(dead_code)]
pub trait Int:
Expand Down Expand Up @@ -53,7 +57,7 @@ pub trait Int:
+ CastInto<usize>
+ CastFrom<u8>
{
fn signed(self) -> <Self::Unsigned as MinInt>::OtherSign;
fn signed(self) -> OtherSign<Self::Unsigned>;
fn unsigned(self) -> Self::Unsigned;
fn from_unsigned(unsigned: Self::Unsigned) -> Self;
fn abs(self) -> Self;
Expand Down
3 changes: 2 additions & 1 deletion src/math/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ mod float_traits;
mod hex_float;
mod int_traits;

pub use float_traits::Float;
#[allow(unused_imports)]
pub use float_traits::{Float, IntTy};
#[allow(unused_imports)]
pub use hex_float::{hf32, hf64};
pub use int_traits::{CastFrom, CastInto, DInt, HInt, Int, MinInt};

0 comments on commit ebdb38b

Please sign in to comment.