1use std::fmt;
2use std::ops::{Div, Mul};
3
4use rustc_error_messages::{DiagArgValue, IntoDiagArg};
5use rustc_macros::{Decodable, Encodable, HashStable_Generic};
6
7#[derive(#[automatically_derived]
impl ::core::clone::Clone for Limit {
#[inline]
fn clone(&self) -> Limit {
let _: ::core::clone::AssertParamIsClone<usize>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Limit { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Limit {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Limit",
&&self.0)
}
}Debug, const _: () =
{
impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
for Limit where __CTX: crate::HashStableContext {
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
Limit(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_Generic, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for Limit {
fn encode(&self, __encoder: &mut __E) {
match *self {
Limit(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for Limit {
fn decode(__decoder: &mut __D) -> Self {
Limit(::rustc_serialize::Decodable::decode(__decoder))
}
}
};Decodable)]
10pub struct Limit(pub usize);
11
12impl Limit {
13 pub fn new(value: usize) -> Self {
15 Limit(value)
16 }
17
18 pub fn unlimited() -> Self {
20 Limit(usize::MAX)
21 }
22
23 #[inline]
26 pub fn value_within_limit(&self, value: usize) -> bool {
27 value <= self.0
28 }
29}
30
31impl From<usize> for Limit {
32 fn from(value: usize) -> Self {
33 Self::new(value)
34 }
35}
36
37impl fmt::Display for Limit {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 self.0.fmt(f)
40 }
41}
42
43impl Div<usize> for Limit {
44 type Output = Limit;
45
46 fn div(self, rhs: usize) -> Self::Output {
47 Limit::new(self.0 / rhs)
48 }
49}
50
51impl Mul<usize> for Limit {
52 type Output = Limit;
53
54 fn mul(self, rhs: usize) -> Self::Output {
55 Limit::new(self.0 * rhs)
56 }
57}
58
59impl IntoDiagArg for Limit {
60 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
61 self.to_string().into_diag_arg(&mut None)
62 }
63}