Skip to content

Commit ae62b1b

Browse files
committed
Add Ord::clamp_min and clamp_max
1 parent f524236 commit ae62b1b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

library/core/src/cmp.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,52 @@ pub const trait Ord: [const] Eq + [const] PartialOrd<Self> + PointeeSized {
10961096
self
10971097
}
10981098
}
1099+
1100+
/// Restricts a value to a maximum bound.
1101+
///
1102+
/// Returns `max` if `self` is greater than `max`, otherwise returns `self`.
1103+
///
1104+
/// This is identical to `min`, but is easier to read when using method call syntax.
1105+
///
1106+
/// # Examples
1107+
///
1108+
/// ```
1109+
/// #![feature(clamp_min_max)]
1110+
/// assert_eq!(12.clamp_max(10), 10);
1111+
/// assert_eq!(4.clamp_max(7), 4);
1112+
/// ```
1113+
#[must_use]
1114+
#[inline]
1115+
#[unstable(feature = "clamp_min_max", issue = "147781")]
1116+
fn clamp_max(self, min: Self) -> Self
1117+
where
1118+
Self: Sized + [const] Destruct,
1119+
{
1120+
self.min(min)
1121+
}
1122+
1123+
/// Restricts a value to a minimum bound.
1124+
///
1125+
/// Returns `min` if `self` is less than `min`, otherwise returns `self`.
1126+
///
1127+
/// This is identical to `max`, but is easier to read when using method call syntax.
1128+
///
1129+
/// # Examples
1130+
///
1131+
/// ```
1132+
/// #![feature(clamp_min_max)]
1133+
/// assert_eq!((-3).clamp_min(0), 0);
1134+
/// assert_eq!(4.clamp_min(0), 4);
1135+
/// ```
1136+
#[must_use]
1137+
#[inline]
1138+
#[unstable(feature = "clamp_min_max", issue = "147781")]
1139+
fn clamp_min(self, min: Self) -> Self
1140+
where
1141+
Self: Sized + [const] Destruct,
1142+
{
1143+
self.max(min)
1144+
}
10991145
}
11001146

11011147
/// Derive macro generating an impl of the trait [`Ord`].

0 commit comments

Comments
 (0)