Skip to content

How to implement a graph (in our case, the call graph) idiomatically? #3

@jiezhoucs

Description

@jiezhoucs

The current implementation of the CallGraphNode

struct CallGraphNode<'a> {

simply uses summarize_fn::FnID to represent a function's callers and callees. It works fine, but more naturally, we should directly use CallGraphNode instead of FnID, which means something like

struct CallGraphNode<'a> {                                                         
    fn_id: FnID,                                                                   
    crate_name: &'a str,                                                           
    fn_name: &'a str,                                                              
    callees: FxHashSet<Rc<RefCell<CallGraphNode<'a>>>>,                            
    callers: FxHashSet<Rc<RefCell<CallGraphNode<'a>>>>,                            
} 

However, there seems to be no idiomatic way to implement this. This CallGraphNode cannot derive Hash. Simply implementing Hash for it as follows does not work:

impl Hash for CallGraphNode<'_> {                                                  
    fn hash<H: Hasher>(&self, state: &mut H) {                                     
        self.fn_id.hash(state);                                                                                       
    }                                                                              
}

We still got unsatisfied traits bounds forLrc<RefCell<CallGraphNode<'_>>>: Hash and "the trait Hash is not implemented for RefCell<CallGraphNode<'a>>".

It seems that this can be "solved" by adding a wrapper to CallGraphNode, and implement Hash for this wrapper. However, this is not elegant as there would be unnecessary layers.

References:
HashMap keyed by Rc<RefCell<..>>
Hash based on address (not value) of Rc
Set of Rc where T isn't Hash or Ord?
Hash trait does not work for Rc<RefCell> in enum

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions