From f9422e706d627ef967b4fa48c4ac54a39701e7cc Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 22 Mar 2021 15:22:47 -0400 Subject: [PATCH] Add LowerBounded/UpperBounded traits --- src/bounds.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/bounds.rs b/src/bounds.rs index c9ff749d..36e1bbdf 100644 --- a/src/bounds.rs +++ b/src/bounds.rs @@ -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 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 UpperBounded for T { + fn max_value() -> T { + Bounded::max_value() + } +} + macro_rules! bounded_impl { ($t:ty, $min:expr, $max:expr) => { impl Bounded for $t {