|
| 1 | +use crate::marker::Destruct; |
| 2 | +use crate::ops::{RangeFrom, RangeFull, RangeInclusive, RangeToInclusive}; |
| 3 | + |
| 4 | +/// Trait for ranges supported by [`Ord::clamp_to`]. |
| 5 | +#[unstable(feature = "clamp_bounds", issue = "147781")] |
| 6 | +#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] |
| 7 | +pub const trait ClampBounds<T>: Sized { |
| 8 | + /// The implementation of [`Ord::clamp_to`]. |
| 9 | + fn clamp(self, value: T) -> T |
| 10 | + where |
| 11 | + T: [const] Destruct; |
| 12 | +} |
| 13 | + |
| 14 | +#[unstable(feature = "clamp_bounds", issue = "147781")] |
| 15 | +#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] |
| 16 | +impl<T> const ClampBounds<T> for RangeFrom<T> |
| 17 | +where |
| 18 | + T: [const] Ord, |
| 19 | +{ |
| 20 | + fn clamp(self, value: T) -> T |
| 21 | + where |
| 22 | + T: [const] Destruct, |
| 23 | + { |
| 24 | + value.max(self.start) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[unstable(feature = "clamp_bounds", issue = "147781")] |
| 29 | +#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] |
| 30 | +impl<T> const ClampBounds<T> for RangeToInclusive<T> |
| 31 | +where |
| 32 | + T: [const] Ord, |
| 33 | +{ |
| 34 | + fn clamp(self, value: T) -> T |
| 35 | + where |
| 36 | + T: [const] Destruct, |
| 37 | + { |
| 38 | + value.min(self.end) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[unstable(feature = "clamp_bounds", issue = "147781")] |
| 43 | +#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] |
| 44 | +impl<T> const ClampBounds<T> for RangeInclusive<T> |
| 45 | +where |
| 46 | + T: [const] Ord, |
| 47 | +{ |
| 48 | + fn clamp(self, value: T) -> T |
| 49 | + where |
| 50 | + T: [const] Destruct, |
| 51 | + { |
| 52 | + let (start, end) = self.into_inner(); |
| 53 | + value.clamp(start, end) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[unstable(feature = "clamp_bounds", issue = "147781")] |
| 58 | +#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] |
| 59 | +impl<T> const ClampBounds<T> for RangeFull { |
| 60 | + fn clamp(self, value: T) -> T { |
| 61 | + value |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +macro impl_for_float($t:ty) { |
| 66 | + #[unstable(feature = "clamp_bounds", issue = "147781")] |
| 67 | + #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] |
| 68 | + impl const ClampBounds<$t> for RangeFrom<$t> { |
| 69 | + fn clamp(self, value: $t) -> $t { |
| 70 | + value.max(self.start) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + #[unstable(feature = "clamp_bounds", issue = "147781")] |
| 75 | + #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] |
| 76 | + impl const ClampBounds<$t> for RangeToInclusive<$t> { |
| 77 | + fn clamp(self, value: $t) -> $t { |
| 78 | + value.min(self.end) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + #[unstable(feature = "clamp_bounds", issue = "147781")] |
| 83 | + #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] |
| 84 | + impl const ClampBounds<$t> for RangeInclusive<$t> { |
| 85 | + fn clamp(self, value: $t) -> $t { |
| 86 | + let (start, end) = self.into_inner(); |
| 87 | + // Deliberately avoid using `clamp` to handle NaN consistently |
| 88 | + value.max(start).min(end) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// #[unstable(feature = "f16", issue = "116909")] |
| 94 | +impl_for_float!(f16); |
| 95 | +impl_for_float!(f32); |
| 96 | +impl_for_float!(f64); |
| 97 | +// #[unstable(feature = "f128", issue = "116909")] |
| 98 | +impl_for_float!(f128); |
0 commit comments