pub struct CallGraphBuilder<'a> { /* private fields */ }Expand description
Builds a call graph by recursively tracing function calls.
§Rust Book Reference
Chapter 10.3: Validating References with Lifetimes https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html
§Educational Notes - Lifetime Parameters
This struct demonstrates explicit lifetime annotations:
pub struct CallGraphBuilder<'a> {
finder: &'a mut FunctionFinder,
extractor: &'a CallExtractor,
}What does <'a> mean?
'ais a lifetime parameter (read as “lifetime a”)- It says: “This struct holds references that live for lifetime ’a”
- The struct cannot outlive the data it references
Why do we need lifetimes here?
CallGraphBuilderstores references toFunctionFinderandCallExtractor- Without lifetimes, Rust doesn’t know how long these references are valid
- The lifetime
'aconnects the struct’s lifetime to its references
What does this prevent?
let builder = {
let finder = FunctionFinder::new(...);
let extractor = CallExtractor::new(...);
CallGraphBuilder::new(..., &mut finder, &extractor)
}; // ERROR! finder and extractor are dropped here
// builder would have dangling references!The lifetime constraint:
CallGraphBuilder<'a>can only exist whilefinderandextractorexist- Rust enforces this at compile time
- No runtime overhead - pure compile-time safety
Alternative without lifetimes:
- Could use
Box<FunctionFinder>(heap allocation + ownership) - Could use
Rc<RefCell<FunctionFinder>>(reference counting + runtime checks) - Lifetimes are zero-cost: no allocation, no runtime checks
Implementations§
Source§impl<'a> CallGraphBuilder<'a>
impl<'a> CallGraphBuilder<'a>
Sourcepub fn new(
direction: TraceDirection,
max_depth: usize,
finder: &'a mut FunctionFinder,
extractor: &'a CallExtractor,
) -> Self
pub fn new( direction: TraceDirection, max_depth: usize, finder: &'a mut FunctionFinder, extractor: &'a CallExtractor, ) -> Self
Create a new CallGraphBuilder.
§Rust Book Reference
Chapter 10.3: Lifetime Annotations in Function Signatures https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-annotations-in-function-signatures
§Educational Notes - Lifetime in Function Signatures
This function signature shows how lifetimes flow through constructors:
pub fn new(
finder: &'a mut FunctionFinder,
extractor: &'a CallExtractor,
) -> SelfWhat this means:
- The returned
CallGraphBuilder<'a>contains references with lifetime'a - Those references come from the input parameters
- The builder cannot outlive
finderorextractor
Lifetime elision:
Without the struct’s <'a>, we’d need to write:
pub fn new<'a>(
finder: &'a mut FunctionFinder,
extractor: &'a CallExtractor,
) -> CallGraphBuilder<'a>But since the struct already has <'a>, we can use Self.
§Arguments
direction- Whether to trace forward or backwardmax_depth- Maximum depth of the call treefinder- Service to find function definitionsextractor- Service to extract calls from code
Sourcepub fn build_trace(
&mut self,
start_fn: &FunctionDef,
) -> Result<Option<CallTree>>
pub fn build_trace( &mut self, start_fn: &FunctionDef, ) -> Result<Option<CallTree>>
Build a call trace tree starting from the given function