Skip to main content

rustc_hir/
canonical_symbols.rs

1use rustc_data_structures::fx::FxIndexMap;
2use rustc_macros::{Encodable, StableHash};
3use rustc_span::Symbol;
4use rustc_span::def_id::DefId;
5
6/// A representation of a canonical symbol
7#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CanonicalSymbol {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "CanonicalSymbol", "def_id", &self.def_id, "symbol",
            &&self.symbol)
    }
}Debug, #[automatically_derived]
impl ::core::marker::Copy for CanonicalSymbol { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CanonicalSymbol {
    #[inline]
    fn clone(&self) -> CanonicalSymbol {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for CanonicalSymbol {
    #[inline]
    fn eq(&self, other: &CanonicalSymbol) -> bool {
        self.def_id == other.def_id && self.symbol == other.symbol
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for CanonicalSymbol {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<DefId>;
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for CanonicalSymbol {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.def_id, state);
        ::core::hash::Hash::hash(&self.symbol, state)
    }
}Hash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for CanonicalSymbol {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    CanonicalSymbol {
                        def_id: ref __binding_0, symbol: ref __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };Encodable, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            CanonicalSymbol {
            #[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 {
                    CanonicalSymbol {
                        def_id: ref __binding_0, symbol: ref __binding_1 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
8pub struct CanonicalSymbol {
9    pub def_id: DefId,
10    pub symbol: Symbol,
11}
12
13#[derive(const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            CanonicalSymbols {
            #[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 {
                    CanonicalSymbols { symbols: ref __binding_0 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash, #[automatically_derived]
impl ::core::fmt::Debug for CanonicalSymbols {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "CanonicalSymbols", "symbols", &&self.symbols)
    }
}Debug)]
14pub struct CanonicalSymbols {
15    symbols: FxIndexMap<Symbol, CanonicalSymbol>,
16}
17
18impl CanonicalSymbols {
19    /// Construct an empty collection of canonical symbols
20    pub fn new() -> Self {
21        Self { symbols: FxIndexMap::default() }
22    }
23
24    pub fn get(&self, symbol: Symbol) -> Option<CanonicalSymbol> {
25        self.symbols.get(&symbol).copied()
26    }
27
28    pub fn set(&mut self, symbol: Symbol, def_id: DefId) -> Option<DefId> {
29        let preexisting = self.symbols.insert(symbol, CanonicalSymbol { def_id, symbol });
30
31        if let Some(preexisting) = preexisting {
32            (preexisting.def_id != def_id).then_some(preexisting.def_id)
33        } else {
34            None
35        }
36    }
37
38    pub fn iter(&self) -> impl Iterator<Item = CanonicalSymbol> {
39        self.symbols.values().copied()
40    }
41}