use core::fmt;
use core::ops::{Div, Mul};
#[cfg(feature = "nightly")]
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, StableHash};
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "nightly", derive(StableHash, Encodable_NoContext, Decodable_NoContext))]
pub struct Limit(pub usize);
impl Limit {
pub fn new(value: usize) -> Self {
Limit(value)
}
pub fn unlimited() -> Self {
Limit(usize::MAX)
}
#[inline]
pub fn value_within_limit(&self, value: usize) -> bool {
value <= self.0
}
}
impl From<usize> for Limit {
fn from(value: usize) -> Self {
Self::new(value)
}
}
impl fmt::Display for Limit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl Div<usize> for Limit {
type Output = Limit;
fn div(self, rhs: usize) -> Self::Output {
Limit::new(self.0 / rhs)
}
}
impl Mul<usize> for Limit {
type Output = Limit;
fn mul(self, rhs: usize) -> Self::Output {
Limit::new(self.0 * rhs)
}
}
#[cfg(feature = "nightly")]
impl crate::rustc_error_messages::IntoDiagArg for Limit {
fn into_diag_arg(
self,
_: &mut crate::rustc_error_messages::LongTyPath,
) -> crate::rustc_error_messages::DiagArgValue {
self.0.into_diag_arg(&mut None)
}
}