Skip to main content

rustc_data_structures/
limit.rs

1use std::fmt;
2use std::ops::{Div, Mul};
3
4use rustc_macros::{Decodable_NoContext, Encodable_NoContext, StableHash};
5
6/// New-type wrapper around `usize` for representing limits. Ensures that comparisons against
7/// limits are consistent throughout the compiler.
8#[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 ::rustc_data_structures::stable_hash::StableHash for Limit {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    Limit(ref __binding_0) => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash, const _: () =
    {
        impl<__E: ::rustc_serialize::Encoder>
            ::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_NoContext, const _: () =
    {
        impl<__D: ::rustc_serialize::Decoder>
            ::rustc_serialize::Decodable<__D> for Limit {
            fn decode(__decoder: &mut __D) -> Self {
                Limit(::rustc_serialize::Decodable::decode(__decoder))
            }
        }
    };Decodable_NoContext)]
9pub struct Limit(pub usize);
10
11impl Limit {
12    /// Create a new limit from a `usize`.
13    pub fn new(value: usize) -> Self {
14        Limit(value)
15    }
16
17    /// Create a new unlimited limit.
18    pub fn unlimited() -> Self {
19        Limit(usize::MAX)
20    }
21
22    /// Check that `value` is within the limit. Ensures that the same comparisons are used
23    /// throughout the compiler, as mismatches can cause ICEs, see #72540.
24    #[inline]
25    pub fn value_within_limit(&self, value: usize) -> bool {
26        value <= self.0
27    }
28}
29
30impl From<usize> for Limit {
31    fn from(value: usize) -> Self {
32        Self::new(value)
33    }
34}
35
36impl fmt::Display for Limit {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        self.0.fmt(f)
39    }
40}
41
42impl Div<usize> for Limit {
43    type Output = Limit;
44
45    fn div(self, rhs: usize) -> Self::Output {
46        Limit::new(self.0 / rhs)
47    }
48}
49
50impl Mul<usize> for Limit {
51    type Output = Limit;
52
53    fn mul(self, rhs: usize) -> Self::Output {
54        Limit::new(self.0 * rhs)
55    }
56}