interior mutability woes

main
Zynh0722 2024-04-24 00:29:21 -07:00
parent bc2f900f37
commit 50419ff0e8
1 changed files with 17 additions and 3 deletions

View File

@ -5,6 +5,7 @@ mod distance;
mod parse; mod parse;
use std::{ use std::{
borrow::Borrow,
cell::RefCell, cell::RefCell,
collections::{hash_map::Entry, HashMap}, collections::{hash_map::Entry, HashMap},
rc::Rc, rc::Rc,
@ -34,12 +35,23 @@ impl Node {
} }
} }
#[derive(Debug, Clone)] #[derive(Clone)]
struct Destination { struct Destination {
airport: Rc<RefCell<Node>>, node: Rc<RefCell<Node>>,
distance: f64, distance: f64,
} }
impl std::fmt::Debug for Destination {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let node = self.node.as_ref().borrow();
f.debug_struct("Node")
.field("origin", &node.origin)
.field("distance", &self.distance)
.finish()
}
}
impl Destination { impl Destination {
fn new(source: &airport::Airport, destination: Rc<airport::Airport>, graph: &Graph) -> Self { fn new(source: &airport::Airport, destination: Rc<airport::Airport>, graph: &Graph) -> Self {
Destination { Destination {
@ -47,7 +59,7 @@ impl Destination {
(source.lat, source.lon), (source.lat, source.lon),
(destination.lat, destination.lon), (destination.lat, destination.lon),
), ),
airport: graph[&destination.id].clone(), node: graph[&destination.id].clone(),
} }
} }
} }
@ -116,4 +128,6 @@ fn main() {
entry.destinations.push(destination); entry.destinations.push(destination);
}; };
} }
println!("{graph:#?}")
} }