Expand description
§BorrowScope Runtime
A runtime tracking library for visualizing Rust’s ownership and borrowing system.
This crate captures ownership transfers, borrows, and smart pointer operations as they happen at runtime, generating structured event data for analysis.
§Quick Start
use borrowscope_runtime::*;
// Clear previous tracking data
reset();
// Track variable creation
let data = track_new("data", vec![1, 2, 3]);
// Track borrowing
let r = track_borrow("r", &data);
println!("{:?}", r);
// Track drops
track_drop("r");
track_drop("data");
// Export events as JSON
let events = get_events();
println!("{}", serde_json::to_string_pretty(&events).unwrap());§Feature Flags
track- Enables runtime tracking. Without this feature, all tracking functions compile to no-ops with zero overhead.
[dependencies]
borrowscope-runtime = { version = "0.1", features = ["track"] }§Modules
- Core tracking functions (41 functions for all ownership patterns)
- Event types and serialization
- Ownership graph building and analysis
- JSON export utilities
- Lifetime analysis and timeline construction
§Tracking Categories
| Category | Functions |
|---|---|
| Basic ownership | track_new, track_borrow, track_borrow_mut, track_move, track_drop |
| RAII guards | track_new_guard, track_borrow_guard, track_borrow_mut_guard |
| Smart pointers | track_rc_new, track_rc_clone, track_arc_new, track_arc_clone |
| Interior mutability | track_refcell_*, track_cell_* |
| Unsafe code | track_raw_ptr*, track_unsafe_*, track_ffi_call, track_transmute |
§RAII Guards
For automatic drop tracking, use the guard variants:
use borrowscope_runtime::*;
reset();
{
let data = track_new_guard("data", vec![1, 2, 3]);
println!("{:?}", *data);
// track_drop("data") called automatically when data goes out of scope
}
let events = get_events();
assert!(events.last().unwrap().is_drop());§Performance
- With
trackfeature: ~75-80ns per tracking call - Without
trackfeature: zero overhead (compiled away)
Macros§
- refcell_
borrow - Convenience macro for RefCell borrow tracking with auto file:line capture
- refcell_
borrow_ mut - Convenience macro for RefCell borrow_mut tracking with auto file:line capture
- refcell_
drop - Convenience macro for RefCell drop tracking with auto file:line capture
Structs§
- Borrow
Guard - RAII guard for borrow tracking (immutable).
- Borrow
MutGuard - RAII guard for mutable borrow tracking.
- Export
Data - Complete export format optimized for visualization tools.
- Export
Edge - Serializable edge for export.
- Export
Metadata - Export metadata with summary statistics.
- Graph
Stats - Graph statistics
- Lifetime
Relation - A lifetime relationship between a borrower and borrowed variable.
- Ownership
Graph - The complete ownership graph built from events.
- Timeline
- Timeline representation for visualization.
- Track
Guard - RAII guard that tracks drop automatically.
- Tracking
Summary - Summary statistics for tracked events.
- Variable
- A variable node in the ownership graph.
Enums§
- Elision
Rule - Lifetime elision rules (for documentation and analysis)
- Error
- Runtime errors that can occur during tracking or export.
- Event
- An ownership or borrowing event recorded at runtime.
- Relationship
- A relationship edge between variables.
Functions§
- build_
graph - Build a graph from events
- export_
json - Export current tracking data to JSON file
- get_
borrow_ events - Get all
Borrowevents (both mutable and immutable). - get_
drop_ events - Get all
Dropevents. - get_
event_ counts - Get count of events by type.
- get_
events - Get all recorded events.
- get_
events_ filtered - Get events filtered by a predicate.
- get_
events_ for_ var - Get events for a specific variable by name.
- get_
graph - Get the ownership graph built from current events
- get_
move_ events - Get all
Moveevents. - get_
new_ events - Get all
Newevents. - get_
summary - Get a summary of all tracked events.
- print_
summary - Print a summary of tracked events to stdout.
- reset
- Reset tracking state.
- track_
arc_ clone - Track
Arc::cloneoperation. - track_
arc_ clone_ with_ id - Track Arc::clone with explicit IDs and location (advanced API)
- track_
arc_ new - Track
Arc::newallocation. - track_
arc_ new_ with_ id - Track Arc::new with explicit ID and location (advanced API)
- track_
borrow - Track an immutable borrow.
- track_
borrow_ guard - Track an immutable borrow with automatic drop tracking.
- track_
borrow_ mut - Track a mutable borrow.
- track_
borrow_ mut_ guard - Track a mutable borrow with automatic drop tracking.
- track_
borrow_ mut_ with_ id - Track a mutable borrow with full metadata (advanced API)
- track_
borrow_ with_ id - Track an immutable borrow with full metadata (advanced API)
- track_
cell_ get - Track
Cell::getoperation. - track_
cell_ new - Track
Cell::newallocation. - track_
cell_ set - Track
Cell::setoperation. - track_
const_ eval - Track const evaluation.
- track_
drop - Track a variable going out of scope.
- track_
drop_ batch - Track multiple drops in batch (optimized).
- track_
drop_ with_ id - Track a drop with explicit ID and location (advanced API)
- track_
ffi_ call - Track FFI call.
- track_
move - Track an ownership move.
- track_
move_ with_ id - Track a move with explicit IDs and location (advanced API)
- track_
new - Track a new variable creation.
- track_
new_ guard - Track a new variable with automatic drop tracking.
- track_
new_ with_ id - Track a new variable with explicit ID and location (advanced API)
- track_
raw_ ptr - Track raw pointer creation.
- track_
raw_ ptr_ deref - Track raw pointer dereference.
- track_
raw_ ptr_ mut - Track mutable raw pointer creation.
- track_
rc_ clone - Track
Rc::cloneoperation. - track_
rc_ clone_ with_ id - Track Rc::clone with explicit IDs and location (advanced API)
- track_
rc_ new - Track
Rc::newallocation. - track_
rc_ new_ with_ id - Track Rc::new with explicit ID and location (advanced API)
- track_
refcell_ borrow - Track
RefCell::borrowoperation. - track_
refcell_ borrow_ mut - Track
RefCell::borrow_mutoperation. - track_
refcell_ drop - Track RefCell borrow drop (when Ref/RefMut is dropped).
- track_
refcell_ new - Track
RefCell::newallocation. - track_
static_ access - Track static variable access (read or write).
- track_
static_ init - Track static variable initialization.
- track_
transmute - Track transmute operation.
- track_
union_ field_ access - Track union field access.
- track_
unsafe_ block_ enter - Track unsafe block entry.
- track_
unsafe_ block_ exit - Track unsafe block exit.
- track_
unsafe_ fn_ call - Track unsafe function call.
Type Aliases§
- Result
- Result type alias