From d2c93f846797fb5396c02a42d2f7ff9867a7acca Mon Sep 17 00:00:00 2001 From: j-james Date: Sat, 10 Dec 2022 14:20:22 -0800 Subject: Day Seven: Switch to weak references --- 2022/rust/day07/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to '2022/rust') diff --git a/2022/rust/day07/src/main.rs b/2022/rust/day07/src/main.rs index 1ea8840..8ed8d24 100644 --- a/2022/rust/day07/src/main.rs +++ b/2022/rust/day07/src/main.rs @@ -2,7 +2,7 @@ use std::cell::{Cell, RefCell}; use std::cmp; use std::env; use std::fs; -use std::rc::Rc; +use std::rc::{Rc, Weak}; struct Directory { id: String, @@ -10,7 +10,7 @@ struct Directory { // mmm i'd rather have a plain Directory instead of Rc // but i get "cannot move out of x which is behind a shared reference" children: RefCell>>, - parent: Option> + parent: Option> } fn main() { @@ -33,7 +33,7 @@ fn main() { // println!("$ ls") }, ["$", "cd", ".."] => { - current = current.parent.clone().unwrap(); + current = current.parent.clone().unwrap().upgrade().unwrap(); // lmao // println!("$ cd .."); }, ["$", "cd", id] => { @@ -52,7 +52,7 @@ fn main() { id: String::from(id), size: Cell::from(0), children: RefCell::from(Vec::new()), - parent: Option::from(current.clone()) + parent: Option::from(Rc::downgrade(¤t)) })); // println!("dir {}", id) }, -- cgit v1.2.3-70-g09d2