Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publicly export constraint::Constraint but seal it #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ pub trait SubsetOf<P> {}

impl<P, Q> SubsetOf<Q> for P where Q: SupersetOf<P> {}

pub trait Sealed {}

/// Describes constraints on the set of floating-point values that a proxy type
/// may represent.
///
/// This trait expresses a constraint by filter-mapping values. Note that
/// constraints require `Member<RealSet>`, meaning that the set of real numbers
/// must always be supported and is implied.
pub trait Constraint<T>: Member<RealSet>
pub trait Constraint<T>: Member<RealSet> + Sealed
where
T: Float + Primitive,
{
Expand Down Expand Up @@ -52,6 +54,8 @@ where
}
}

impl<T> Sealed for UnitConstraint<T> where T: Float + Primitive {}

impl<T> Member<InfiniteSet> for UnitConstraint<T> where T: Float + Primitive {}

impl<T> Member<NanSet> for UnitConstraint<T> where T: Float + Primitive {}
Expand All @@ -78,13 +82,14 @@ where
fn filter_map(value: T) -> Option<T> {
if value.is_nan() {
None
}
else {
} else {
Some(value)
}
}
}

impl<T> Sealed for NotNanConstraint<T> where T: Float + Primitive {}

impl<T> Member<InfiniteSet> for NotNanConstraint<T> where T: Float + Primitive {}

impl<T> Member<RealSet> for NotNanConstraint<T> where T: Float + Primitive {}
Expand All @@ -107,11 +112,12 @@ where
fn filter_map(value: T) -> Option<T> {
if value.is_nan() || value.is_infinite() {
None
}
else {
} else {
Some(value)
}
}
}

impl<T> Sealed for FiniteConstraint<T> where T: Float + Primitive {}

impl<T> Member<RealSet> for FiniteConstraint<T> where T: Float + Primitive {}
13 changes: 5 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ mod proxy;
use crate::cmp::IntrinsicOrd;
use crate::constraint::{FiniteConstraint, NotNanConstraint, UnitConstraint};

pub use crate::constraint::Constraint;
pub use crate::proxy::Proxy;

/// Floating-point representation with total ordering.
Expand Down Expand Up @@ -167,13 +168,11 @@ where

if self.is_nan() {
CANONICAL_NAN_BITS
}
else {
} else {
let (mantissa, exponent, sign) = self.integer_decode();
if mantissa == 0 {
CANONICAL_ZERO_BITS
}
else {
} else {
let exponent = u64::from(unsafe { mem::transmute::<i16, u16>(exponent) });
let sign = if sign > 0 { 1u64 } else { 0u64 };
(mantissa & MANTISSA_MASK)
Expand Down Expand Up @@ -252,8 +251,7 @@ impl Encoding for f32 {
let exponent: i16 = ((bits >> 23) & 0xff) as i16;
let mantissa = if exponent == 0 {
(bits & 0x7f_ffff) << 1
}
else {
} else {
(bits & 0x7f_ffff) | 0x80_0000
};
(mantissa as u64, exponent - (127 + 23), sign)
Expand Down Expand Up @@ -288,8 +286,7 @@ impl Encoding for f64 {
let exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
let mantissa = if exponent == 0 {
(bits & 0xf_ffff_ffff_ffff) << 1
}
else {
} else {
(bits & 0xf_ffff_ffff_ffff) | 0x10_0000_0000_0000
};
(mantissa, exponent - (1023 + 52), sign)
Expand Down