trace_borrow

Attribute Macro trace_borrow 

Source
#[trace_borrow]
Expand description

Attribute macro to trace ownership and borrowing in a function.

This macro transforms a function to inject runtime tracking calls that record ownership transfers, borrows, drops, and other operations. The events can be retrieved using borrowscope_runtime::get_events().

§Basic Usage

use borrowscope_macro::trace_borrow;
use borrowscope_runtime::*;

#[trace_borrow]
fn example() {
    let x = String::from("hello");  // New event
    let y = &x;                      // Borrow event
    let z = x;                       // Move event
}                                    // Drop events

§Attribute Options

§quiet - Minimal tracking

Only tracks basic ownership: new, move, drop, borrow.

#[trace_borrow(quiet)]
fn minimal() {
    let x = vec![1, 2, 3];
    for i in &x { }  // Loop NOT tracked
}

§verbose - All tracking

Enables all tracking features (same as default currently).

#[trace_borrow(verbose)]
fn everything() { }

§skip - Disable specific features

Comma-separated list of feature groups to disable.

#[trace_borrow(skip = "loops,branches")]
fn skip_noisy() {
    for i in 0..10 { }  // NOT tracked
    if true { }         // NOT tracked
}

§only - Enable only specific features

Comma-separated list of feature groups to enable (all others disabled).

#[trace_borrow(only = "ownership,functions")]
fn focused() {
    let x = 1;  // Tracked (ownership)
    // FnEnter/FnExit tracked (functions)
}

§Feature Groups

GroupAliasesWhat it tracks
ownership-let, moves, drops, borrows
smart_pointerspointersRc, Arc, RefCell, Cell
loops-for, while, loop
branches-if/else, match
control_flowcontrolbreak, continue, return
try-? operator
methods-clone, lock, unwrap
async-async blocks, await
unsafe-unsafe blocks, raw pointers
expressionsexprsstruct, tuple, array, range, cast
functionsfnFunction entry/exit (off by default)

§Conditional Compilation

OptionDescription
debug_onlyOnly instrument in debug builds
release_onlyOnly instrument in release builds
feature = "name"Only instrument when cargo feature is enabled

§Limitations

  • Cannot be used on const fn (tracking requires runtime)
  • Cannot be used on extern functions
  • Async functions work but may miss some ownership across await points