Created
December 12, 2021 21:01
-
-
Save kkolyan/4bf0c0dc0e7842c51e2e57e93cf8a0c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cell::RefCell; | |
use std::rc::{Rc, Weak}; | |
use extend::ext; | |
struct Node { | |
name: String, | |
} | |
struct Engine { | |
selected_node: Option<Weak<RefCell<Node>>>, | |
} | |
impl Engine { | |
fn new() -> Self { | |
return Engine { selected_node: None }; | |
} | |
} | |
impl Node { | |
fn new(name: &str) -> Self { | |
return Node { name: name.to_string() }; | |
} | |
} | |
#[ext] | |
impl Rc<RefCell<Node>> { | |
fn do_some_action(&self, engine: &mut Engine) { | |
engine.selected_node = Some(Rc::downgrade(self)); | |
} | |
} | |
fn main() { | |
let mut engine = Engine::new(); | |
let a = Rc::from(RefCell::from(Node::new("sharik"))); | |
a.do_some_action(&mut engine); | |
assert_eq!( | |
engine.selected_node | |
.expect("none selected") | |
.upgrade() | |
.expect("sharik is dead") | |
.borrow_mut().name, | |
"sharik" | |
); | |
println!("Ok") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment