Skip to main content

rustc_middle/traits/
cache.rs

1//! Cache for candidate selection.
2
3use std::hash::Hash;
4
5use rustc_data_structures::fx::FxHashMap;
6use rustc_data_structures::sync::Lock;
7
8use crate::dep_graph::DepNodeIndex;
9use crate::ty::TyCtxt;
10
11pub struct WithDepNodeCache<Key, Value> {
12    hashmap: Lock<FxHashMap<Key, WithDepNode<Value>>>,
13}
14
15impl<Key: Clone, Value: Clone> Clone for WithDepNodeCache<Key, Value> {
16    fn clone(&self) -> Self {
17        Self { hashmap: Lock::new(self.hashmap.borrow().clone()) }
18    }
19}
20
21impl<Key, Value> Default for WithDepNodeCache<Key, Value> {
22    fn default() -> Self {
23        Self { hashmap: Default::default() }
24    }
25}
26
27impl<Key: Eq + Hash, Value: Clone> WithDepNodeCache<Key, Value> {
28    pub fn get<'tcx>(&self, key: &Key, tcx: TyCtxt<'tcx>) -> Option<Value> {
29        Some(self.hashmap.borrow().get(key)?.get(tcx))
30    }
31
32    pub fn insert(&self, key: Key, dep_node: DepNodeIndex, value: Value) {
33        self.hashmap.borrow_mut().insert(key, WithDepNode::new(dep_node, value));
34    }
35}
36
37#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for WithDepNode<T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "WithDepNode",
            "dep_node", &self.dep_node, "cached_value", &&self.cached_value)
    }
}Debug, #[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for WithDepNode<T> {
    #[inline]
    fn clone(&self) -> WithDepNode<T> {
        WithDepNode {
            dep_node: ::core::clone::Clone::clone(&self.dep_node),
            cached_value: ::core::clone::Clone::clone(&self.cached_value),
        }
    }
}Clone, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for WithDepNode<T> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<DepNodeIndex>;
        let _: ::core::cmp::AssertParamIsEq<T>;
    }
}Eq, #[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for WithDepNode<T> {
    #[inline]
    fn eq(&self, other: &WithDepNode<T>) -> bool {
        self.dep_node == other.dep_node &&
            self.cached_value == other.cached_value
    }
}PartialEq)]
38pub struct WithDepNode<T> {
39    dep_node: DepNodeIndex,
40    cached_value: T,
41}
42
43impl<T: Clone> WithDepNode<T> {
44    pub(crate) fn new(dep_node: DepNodeIndex, cached_value: T) -> Self {
45        WithDepNode { dep_node, cached_value }
46    }
47
48    pub(crate) fn get<'tcx>(&self, tcx: TyCtxt<'tcx>) -> T {
49        tcx.dep_graph.read_index(self.dep_node);
50        self.cached_value.clone()
51    }
52}