use memscope_rs::{global_tracker, init_global_tracking, track, MemScopeResult};
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
#[derive(Debug)]
struct Node {
next: Option<Rc<RefCell<Node>>>,
}
impl Node {
fn new(_name: impl Into<String>) -> Self {
Node { next: None }
}
}
fn main() -> MemScopeResult<()> {
println!("đ Variable Relationships Showcase");
println!("===================================\n");
init_global_tracking()?;
let tracker = global_tracker()?;
println!("1. Clone Relationships (same type Vec<i32>)");
let mut vec_clones = Vec::new();
for i in 0..10 {
let vec: Vec<i32> = vec![i, i * 2, i * 3];
track!(tracker, vec);
vec_clones.push(vec);
}
println!("â Created {} Vec<i32> clones", vec_clones.len());
println!("\n2. Smart Pointer Relationships (Arc)");
let data = vec![1, 2, 3, 4, 5];
let arc1 = Arc::new(data);
track!(tracker, arc1.clone());
for _i in 0..8 {
let arc_clone = Arc::clone(&arc1);
track!(tracker, arc_clone);
}
println!("â Created Arc with {} clones", 9);
println!("\n3. Smart Pointer Relationships (Rc)");
let rc_data = "Hello, Rc!".to_string();
let rc1 = Rc::new(rc_data);
track!(tracker, rc1.clone());
for _i in 0..6 {
let rc_clone = Rc::clone(&rc1);
track!(tracker, rc_clone);
}
println!("â Created Rc with {} clones", 7);
println!("\n4. Rc Retain Cycle (Memory Leak Warning)");
let a = Rc::new(RefCell::new(Node::new("Node A")));
let b = Rc::new(RefCell::new(Node::new("Node B")));
track!(tracker, a.clone());
track!(tracker, b.clone());
a.borrow_mut().next = Some(b.clone());
b.borrow_mut().next = Some(a.clone());
println!("â Created cycle: A -> B -> A");
println!(" â ī¸ These Rc objects will never be dropped (memory leak)");
println!(" âšī¸ Current system shows this as same-type Rc relationships");
println!("\n5. Safe Rc Structure (No Cycle)");
let c = Rc::new(RefCell::new(Node::new("Node C")));
let d = Rc::new(RefCell::new(Node::new("Node D")));
let e = Rc::new(RefCell::new(Node::new("Node E")));
track!(tracker, c.clone());
track!(tracker, d.clone());
track!(tracker, e.clone());
c.borrow_mut().next = Some(d.clone());
d.borrow_mut().next = Some(e.clone());
println!("â Created linear structure: C -> D -> E");
println!(" âšī¸ This will be properly dropped when out of scope");
println!("\n6. String Type Relationships");
let mut strings = Vec::new();
for i in 0..12 {
let s = format!("String number {}", i);
track!(tracker, s);
strings.push(s);
}
println!("â Created {} String instances", strings.len());
println!("\n7. HashMap Type Relationships");
let mut maps = Vec::new();
for i in 0..5 {
let mut map = std::collections::HashMap::new();
for j in 0..10 {
map.insert(format!("key_{}_{}", i, j), j * i);
}
track!(tracker, map);
maps.push(map);
}
println!("â Created {} HashMap instances", maps.len());
println!("\n8. BTreeMap Type Relationships");
let mut btree_maps = Vec::new();
for i in 0..4 {
let mut map = std::collections::BTreeMap::new();
for j in 0..8 {
map.insert(j, format!("value_{}_{}", i, j));
}
track!(tracker, map);
btree_maps.push(map);
}
println!("â Created {} BTreeMap instances", btree_maps.len());
println!("\n9. Vector<u8> Type Relationships");
let mut byte_vecs = Vec::new();
for i in 0..8 {
let vec: Vec<u8> = (0..128).map(|j| ((i * 128 + j) % 256) as u8).collect();
track!(tracker, vec);
byte_vecs.push(vec);
}
println!("â Created {} Vec<u8> instances", byte_vecs.len());
println!("\n10. Box Type Relationships");
let mut boxes = Vec::new();
for i in 0..6 {
let boxed: Box<i32> = Box::new(i * 100);
track!(tracker, boxed);
boxes.push(boxed);
}
println!("â Created {} Box<i32> instances", boxes.len());
let stats = tracker.get_stats();
println!("\nđ Statistics:");
println!(" Total allocations: {}", stats.total_allocations);
println!(" Active allocations: {}", stats.active_allocations);
println!(" Peak memory: {} bytes", stats.peak_memory_bytes);
let output_path = "MemoryAnalysis/variable_relationships_showcase";
tracker.export_html(output_path)?;
tracker.export_json(output_path)?;
println!("\nâ
Export successful!");
println!("đ Results saved to: {}", output_path);
println!("\nđ Key Relationships in Dashboard:");
println!(" đĸ Green: Vec<i32>, Vec<u8>, String, Box clones");
println!(" đŖ Purple: Arc, Rc smart pointers");
println!(" đ Yellow: HashMap, BTreeMap collections");
println!(" đĩ Blue: Other same-type relationships");
println!(" â ī¸ Note: Rc cycles shown as same-type relationships");
println!(" đĄ True cycle detection needs TrackedRc implementation");
Ok(())
}