From 6104e761d249a6e78b583dcc338fc34d5cb539e1 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Fri, 11 Oct 2024 18:43:15 +0200 Subject: [PATCH] refactor: autogenerate getters with `derive-getters` crate --- src/autorouter/autorouter.rs | 10 ++-------- src/autorouter/history.rs | 12 ++---------- src/autorouter/invoker.rs | 12 ++---------- src/board/board.rs | 11 +++++------ src/drawing/drawing.rs | 9 +++------ src/geometry/geometry.rs | 7 ++----- src/geometry/with_rtree.rs | 16 ++-------------- src/layout/layout.rs | 7 ++----- src/router/route.rs | 2 +- src/router/router.rs | 8 +++----- 10 files changed, 24 insertions(+), 70 deletions(-) diff --git a/src/autorouter/autorouter.rs b/src/autorouter/autorouter.rs index c3b50d3..b011faa 100644 --- a/src/autorouter/autorouter.rs +++ b/src/autorouter/autorouter.rs @@ -1,3 +1,4 @@ +use derive_getters::Getters; use petgraph::graph::EdgeIndex; use serde::{Deserialize, Serialize}; use spade::InsertionError; @@ -43,6 +44,7 @@ pub enum AutorouterError { NeedExactlyTwoRatlines, } +#[derive(Getters)] pub struct Autorouter { pub(super) board: Board, pub(super) ratsnest: Ratsnest, @@ -201,12 +203,4 @@ impl Autorouter { }) .collect() } - - pub fn board(&self) -> &Board { - &self.board - } - - pub fn ratsnest(&self) -> &Ratsnest { - &self.ratsnest - } } diff --git a/src/autorouter/history.rs b/src/autorouter/history.rs index bd3d379..dd6ad98 100644 --- a/src/autorouter/history.rs +++ b/src/autorouter/history.rs @@ -2,7 +2,7 @@ //! Handles error scenarios related to command history, maintaining lists of executed //! and undone commands for easy navigation. -use derive_getters::Dissolve; +use derive_getters::{Dissolve, Getters}; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -16,7 +16,7 @@ pub enum HistoryError { NoNextCommand, } -#[derive(Debug, Default, Clone, Dissolve, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, Getters, Dissolve, Serialize, Deserialize)] pub struct History { done: Vec, undone: Vec, @@ -60,12 +60,4 @@ impl History { pub fn last_undone(&self) -> Result<&Command, HistoryError> { self.undone.last().ok_or(HistoryError::NoNextCommand) } - - pub fn done(&self) -> &[Command] { - &self.done - } - - pub fn undone(&self) -> &[Command] { - &self.undone - } } diff --git a/src/autorouter/invoker.rs b/src/autorouter/invoker.rs index b401413..aa96d68 100644 --- a/src/autorouter/invoker.rs +++ b/src/autorouter/invoker.rs @@ -3,7 +3,7 @@ use std::cmp::Ordering; use contracts_try::debug_requires; -use derive_getters::Dissolve; +use derive_getters::{Dissolve, Getters}; use enum_dispatch::enum_dispatch; use thiserror::Error; @@ -70,7 +70,7 @@ impl TryInto<()> for InvokerStatus { } } -#[derive(Dissolve)] +#[derive(Getters, Dissolve)] pub struct Invoker { pub(super) autorouter: Autorouter, pub(super) history: History, @@ -199,12 +199,4 @@ impl Invoker { self.history.set_undone(undone.into_iter()); } - - pub fn autorouter(&self) -> &Autorouter { - &self.autorouter - } - - pub fn history(&self) -> &History { - &self.history - } } diff --git a/src/board/board.rs b/src/board/board.rs index b12597d..63764b9 100644 --- a/src/board/board.rs +++ b/src/board/board.rs @@ -1,6 +1,7 @@ use std::{cmp::Ordering, collections::HashMap}; use bimap::BiHashMap; +use derive_getters::Getters; use serde::{Deserialize, Serialize}; use crate::{ @@ -41,10 +42,13 @@ impl BandName { /// /// The struct manages the relationships between board's layout, /// and its compounds, as well as provides methods to manipulate them. -#[derive(Debug)] +#[derive(Debug, Getters)] pub struct Board { layout: Layout, + // TODO: Simplify access logic to these members so that `#[getter(skip)]`s can be removed. + #[getter(skip)] node_to_pinname: HashMap, + #[getter(skip)] band_bandname: BiHashMap, } @@ -231,11 +235,6 @@ impl Board { self.layout.drawing().rules() } - /// Returns the layout managed by this board. - pub fn layout(&self) -> &Layout { - &self.layout - } - /// Returns a mutable reference to the layout, allowing modifications. pub fn layout_mut(&mut self) -> &mut Layout { &mut self.layout diff --git a/src/drawing/drawing.rs b/src/drawing/drawing.rs index 16387fb..5950aa8 100644 --- a/src/drawing/drawing.rs +++ b/src/drawing/drawing.rs @@ -1,4 +1,5 @@ use contracts_try::{debug_ensures, debug_invariant}; +use derive_getters::Getters; use enum_dispatch::enum_dispatch; use geo::Point; @@ -64,7 +65,7 @@ pub struct Collision(pub PrimitiveShape, pub PrimitiveIndex); #[error("{1:?} is already connected to net {0}")] pub struct AlreadyConnected(pub usize, pub PrimitiveIndex); -#[derive(Debug)] +#[derive(Debug, Getters)] pub struct Drawing { geometry_with_rtree: GeometryWithRtree< PrimitiveWeight, @@ -890,10 +891,6 @@ impl Drawing { self.geometry_with_rtree.rtree() } - pub fn rules(&self) -> &R { - &self.rules - } - #[debug_ensures(self.geometry_with_rtree.graph().node_count() == old(self.geometry_with_rtree.graph().node_count()))] #[debug_ensures(self.geometry_with_rtree.graph().edge_count() == old(self.geometry_with_rtree.graph().edge_count()))] pub fn rules_mut(&mut self) -> &mut R { @@ -917,7 +914,7 @@ impl Drawing { } pub fn layer_count(&self) -> usize { - self.geometry_with_rtree.layer_count() + *self.geometry_with_rtree.layer_count() } pub fn node_count(&self) -> usize { diff --git a/src/geometry/geometry.rs b/src/geometry/geometry.rs index 3831c46..58a87e6 100644 --- a/src/geometry/geometry.rs +++ b/src/geometry/geometry.rs @@ -1,5 +1,6 @@ use std::marker::PhantomData; +use derive_getters::Getters; use enum_dispatch::enum_dispatch; use geo::Point; use petgraph::{ @@ -68,7 +69,7 @@ pub trait AccessDotWeight: GetPos + SetPos + GetWidth + Into + Copy {} pub trait AccessSegWeight: GetWidth + Into + Copy {} pub trait AccessBendWeight: GetOffset + SetOffset + GetWidth + Into + Copy {} -#[derive(Debug)] +#[derive(Debug, Getters)] pub struct Geometry< PW: GetWidth + TryInto + TryInto + TryInto + Retag + Copy, DW: AccessDotWeight, @@ -478,10 +479,6 @@ impl< }) .map(|ni| self.primitive_weight(ni).retag(ni)) } - - pub fn graph(&self) -> &StableDiGraph, GeometryLabel, usize> { - &self.graph - } } impl< diff --git a/src/geometry/with_rtree.rs b/src/geometry/with_rtree.rs index f5ac4e8..d8f5b81 100644 --- a/src/geometry/with_rtree.rs +++ b/src/geometry/with_rtree.rs @@ -1,6 +1,7 @@ use std::marker::PhantomData; use contracts_try::debug_invariant; +use derive_getters::Getters; use geo::Point; use petgraph::stable_graph::StableDiGraph; use rstar::{primitives::GeomWithData, Envelope, RTree, RTreeObject, AABB}; @@ -36,7 +37,7 @@ impl RTreeObject for Bbox { pub type BboxedIndex = GeomWithData; -#[derive(Debug)] +#[derive(Debug, Getters)] pub struct GeometryWithRtree< PW: GetWidth + GetLayer + TryInto + TryInto + TryInto + Retag + Copy, DW: AccessDotWeight + GetLayer, @@ -347,19 +348,6 @@ impl< } } - pub fn layer_count(&self) -> usize { - self.layer_count - } - - pub fn geometry(&self) -> &Geometry { - &self.geometry - } - - // XXX: The type appears wrong? I don't think it should contain CW? - pub fn rtree(&self) -> &RTree>>> { - &self.rtree - } - pub fn graph(&self) -> &StableDiGraph, GeometryLabel, usize> { self.geometry.graph() } diff --git a/src/layout/layout.rs b/src/layout/layout.rs index d45a8bb..740b74a 100644 --- a/src/layout/layout.rs +++ b/src/layout/layout.rs @@ -1,4 +1,5 @@ use contracts_try::debug_ensures; +use derive_getters::Getters; use enum_dispatch::enum_dispatch; use geo::Point; use rstar::AABB; @@ -35,7 +36,7 @@ pub enum CompoundWeight { pub type NodeIndex = GenericNode>; -#[derive(Debug)] +#[derive(Debug, Getters)] pub struct Layout { drawing: Drawing, } @@ -256,10 +257,6 @@ impl Layout { .compound_members(GenericIndex::new(poly.petgraph_index())) } - pub fn drawing(&self) -> &Drawing { - &self.drawing - } - pub fn rules(&self) -> &R { self.drawing.rules() } diff --git a/src/router/route.rs b/src/router/route.rs index 678951e..f44043c 100644 --- a/src/router/route.rs +++ b/src/router/route.rs @@ -27,7 +27,7 @@ impl RouteStepper { to: FixedDotIndex, width: f64, ) -> Result { - let navmesh = Navmesh::new(router.layout(), from, to, router.options())?; + let navmesh = Navmesh::new(router.layout(), from, to, router.options().clone())?; Ok(Self::new_from_navmesh(router, navmesh, width)) } diff --git a/src/router/router.rs b/src/router/router.rs index 3456762..82abb71 100644 --- a/src/router/router.rs +++ b/src/router/router.rs @@ -1,3 +1,4 @@ +use derive_getters::Getters; use geo::EuclideanDistance; use petgraph::{data::DataMap, visit::EdgeRef}; use serde::{Deserialize, Serialize}; @@ -182,8 +183,9 @@ impl<'a, R: AccessRules> AstarStrategy } } -#[derive(Debug)] +#[derive(Debug, Getters)] pub struct Router<'a, R: AccessRules> { + #[getter(skip)] layout: &'a mut Layout, options: RouterOptions, } @@ -209,8 +211,4 @@ impl<'a, R: AccessRules> Router<'a, R> { pub fn layout(&self) -> &Layout { self.layout } - - pub fn options(&self) -> RouterOptions { - self.options - } }