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

Add LowerBounded/UpperBounded traits #210

Merged
merged 1 commit into from Apr 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/bounds.rs
Expand Up @@ -8,12 +8,38 @@ use core::{u16, u32, u64, u8, usize};
/// Numbers which have upper and lower bounds
pub trait Bounded {
// FIXME (#5527): These should be associated constants
/// returns the smallest finite number this type can represent
/// Returns the smallest finite number this type can represent
fn min_value() -> Self;
/// returns the largest finite number this type can represent
/// Returns the largest finite number this type can represent
fn max_value() -> Self;
}

/// Numbers which have lower bounds
pub trait LowerBounded {
/// Returns the smallest finite number this type can represent
fn min_value() -> Self;
}

// FIXME: With a major version bump, this should be a supertrait instead
impl<T: Bounded> LowerBounded for T {
fn min_value() -> T {
Bounded::min_value()
}
}

/// Numbers which have upper bounds
pub trait UpperBounded {
/// Returns the largest finite number this type can represent
fn max_value() -> Self;
}

// FIXME: With a major version bump, this should be a supertrait instead
impl<T: Bounded> UpperBounded for T {
fn max_value() -> T {
Bounded::max_value()
}
}

macro_rules! bounded_impl {
($t:ty, $min:expr, $max:expr) => {
impl Bounded for $t {
Expand Down