From 4d20362e00c0fa5ac53e43c86e81c7071c015cb0 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Fri, 20 Oct 2023 01:08:58 +0000 Subject: [PATCH] primitive: Move `.weight()` method to a new `GetWeight` trait --- src/draw.rs | 1 + src/guide.rs | 2 +- src/layout.rs | 2 +- src/primitive.rs | 23 +++++++++++++++++------ src/router.rs | 1 + src/tracer.rs | 2 +- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/draw.rs b/src/draw.rs index 2eaa032..258d1de 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -7,6 +7,7 @@ use crate::{ guide::Guide, layout::Layout, math::Circle, + primitive::GetWeight, rules::{Conditions, Rules}, segbend::Segbend, }; diff --git a/src/guide.rs b/src/guide.rs index e39c1cd..da0087c 100644 --- a/src/guide.rs +++ b/src/guide.rs @@ -5,7 +5,7 @@ use crate::{ graph::{BendIndex, DotIndex}, layout::Layout, math::{self, Circle}, - primitive::MakeShape, + primitive::{GetWeight, MakeShape}, rules::{Conditions, Rules}, }; diff --git a/src/layout.rs b/src/layout.rs index 8c1b62b..6556c09 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -12,7 +12,7 @@ use crate::graph::{ BendIndex, BendWeight, DotIndex, DotWeight, GenericIndex, GetNodeIndex, Index, Interior, Label, Retag, SegIndex, SegWeight, Weight, }; -use crate::primitive::{GenericPrimitive, MakeShape}; +use crate::primitive::{GenericPrimitive, GetWeight, MakeShape}; use crate::segbend::Segbend; use crate::shape::{Shape, ShapeTrait}; diff --git a/src/primitive.rs b/src/primitive.rs index 4d77b54..ce1586e 100644 --- a/src/primitive.rs +++ b/src/primitive.rs @@ -11,6 +11,11 @@ use crate::graph::{ use crate::math::{self, Circle}; use crate::shape::{BendShape, DotShape, SegShape, Shape, ShapeTrait}; +#[enum_dispatch] +pub trait GetWeight { + fn weight(&self) -> W; +} + #[enum_dispatch] pub trait MakeShape { fn shape(&self) -> Shape; @@ -248,8 +253,10 @@ impl<'a> Dot<'a> { .filter(|bend| self.primitive(*bend).inner().is_none()) .next() } +} - pub fn weight(&self) -> DotWeight { +impl<'a> GetWeight for Dot<'a> { + fn weight(&self) -> DotWeight { self.tagged_weight().into_dot().unwrap() } } @@ -272,8 +279,10 @@ impl<'a> Seg<'a> { pub fn prev(&self) -> Option { self.prev_node().map(|ni| DotIndex::new(ni)) } +} - pub fn weight(&self) -> SegWeight { +impl<'a> GetWeight for Seg<'a> { + fn weight(&self) -> SegWeight { self.tagged_weight().into_seg().unwrap() } } @@ -334,10 +343,6 @@ impl<'a> Bend<'a> { self.prev_node().map(|ni| DotIndex::new(ni)) } - pub fn weight(&self) -> BendWeight { - self.tagged_weight().into_bend().unwrap() - } - fn inner_radius(&self) -> f64 { let mut r = 0.0; let mut layer = BendIndex::new(self.index.node_index()); @@ -368,6 +373,12 @@ impl<'a> Bend<'a> { } } +impl<'a> GetWeight for Bend<'a> { + fn weight(&self) -> BendWeight { + self.tagged_weight().into_bend().unwrap() + } +} + impl<'a> MakeShape for Bend<'a> { fn shape(&self) -> Shape { let ends = self.ends(); diff --git a/src/router.rs b/src/router.rs index b3701e6..fa126a7 100644 --- a/src/router.rs +++ b/src/router.rs @@ -8,6 +8,7 @@ use crate::layout::Layout; use crate::math::Circle; use crate::mesh::{Mesh, MeshEdgeReference, VertexIndex}; +use crate::primitive::GetWeight; use crate::rules::Rules; use crate::tracer::{Trace, Tracer}; diff --git a/src/tracer.rs b/src/tracer.rs index 29e4f2d..07b5bd1 100644 --- a/src/tracer.rs +++ b/src/tracer.rs @@ -6,7 +6,7 @@ use crate::{ graph::{BendIndex, DotIndex, Ends}, layout::Layout, mesh::{Mesh, VertexIndex}, - primitive::MakeShape, + primitive::{GetWeight, MakeShape}, rules::Rules, };