From 13f2400c45c18c8c0adf502790c327474267240a Mon Sep 17 00:00:00 2001 From: Ellen Emilia Anna Zscheile Date: Tue, 18 Mar 2025 04:07:34 +0100 Subject: [PATCH] refactor(drawing): simplify traits --- src/drawing/collect.rs | 2 +- src/drawing/drawing.rs | 3 +- src/drawing/gear.rs | 2 +- src/drawing/guide.rs | 2 +- src/drawing/primitive.rs | 86 ++++++++++++++++------------------------ 5 files changed, 38 insertions(+), 57 deletions(-) diff --git a/src/drawing/collect.rs b/src/drawing/collect.rs index 827e33c..116094d 100644 --- a/src/drawing/collect.rs +++ b/src/drawing/collect.rs @@ -10,7 +10,7 @@ use super::{ gear::{GearIndex, GetNextGear}, graph::PrimitiveIndex, loose::{GetPrevNextLoose, LooseIndex}, - primitive::{GetInnerOuter, GetJoints}, + primitive::GetJoints, rules::AccessRules, Drawing, }; diff --git a/src/drawing/drawing.rs b/src/drawing/drawing.rs index 05a606a..31f1e6f 100644 --- a/src/drawing/drawing.rs +++ b/src/drawing/drawing.rs @@ -32,8 +32,7 @@ use crate::{ guide::Guide, loose::{GetPrevNextLoose, Loose, LooseIndex}, primitive::{ - GenericPrimitive, GetCore, GetInnerOuter, GetJoints, GetLimbs, GetOtherJoint, - MakePrimitiveShape, + GenericPrimitive, GetCore, GetJoints, GetLimbs, GetOtherJoint, MakePrimitiveShape, }, rules::{AccessRules, GetConditions}, seg::{ diff --git a/src/drawing/gear.rs b/src/drawing/gear.rs index 5804afc..c4b94e4 100644 --- a/src/drawing/gear.rs +++ b/src/drawing/gear.rs @@ -10,7 +10,7 @@ use crate::{ bend::{BendIndex, FixedBendIndex, LooseBendIndex}, dot::FixedDotIndex, graph::{MakePrimitive, PrimitiveIndex}, - primitive::{FixedBend, FixedDot, GetFirstGear, GetInnerOuter, LooseBend, Primitive}, + primitive::{FixedBend, FixedDot, GetFirstGear, LooseBend, Primitive}, rules::AccessRules, Drawing, }, diff --git a/src/drawing/guide.rs b/src/drawing/guide.rs index e47ec50..492f356 100644 --- a/src/drawing/guide.rs +++ b/src/drawing/guide.rs @@ -17,7 +17,7 @@ use super::{ dot::{DotIndex, FixedDotIndex, LooseDotIndex}, graph::{MakePrimitive, PrimitiveIndex}, head::{BareHead, CaneHead, GetFace, Head}, - primitive::{GetCore, GetInnerOuter, GetJoints, GetOtherJoint, GetWeight, MakePrimitiveShape}, + primitive::{GetCore, GetJoints, GetOtherJoint, GetWeight, MakePrimitiveShape}, rules::{AccessRules, Conditions, GetConditions}, Drawing, }; diff --git a/src/drawing/primitive.rs b/src/drawing/primitive.rs index 0d641b4..cc63a67 100644 --- a/src/drawing/primitive.rs +++ b/src/drawing/primitive.rs @@ -18,9 +18,9 @@ use crate::{ graph::{GenericIndex, GetPetgraphIndex}, }; -#[enum_dispatch] -pub trait GetDrawing<'a, R: AccessRules> { - fn drawing(&self) -> &Drawing; +pub trait GetDrawing { + type Rules: AccessRules; + fn drawing(&self) -> &Drawing; } #[enum_dispatch] @@ -55,8 +55,12 @@ pub trait GetInterior { fn interior(&self) -> Vec; } -pub trait GetOtherJoint>: - GetJoints +pub trait GetOtherJoint: GetJoints { + fn other_joint(&self, end: F) -> F; +} + +impl, S: GetJoints> GetOtherJoint + for S { fn other_joint(&self, end: F) -> F { let joints = self.joints(); @@ -72,7 +76,7 @@ pub trait GetJoints { fn joints(&self) -> (F, T); } -pub trait GetFirstGear<'a, R: AccessRules>: GetDrawing<'a, R> + GetPetgraphIndex { +pub trait GetFirstGear: GetDrawing + GetPetgraphIndex { fn first_gear(&self) -> Option { self.drawing() .geometry() @@ -85,7 +89,11 @@ pub trait GetBendIndex { fn bend_index(&self) -> BendIndex; } -pub trait GetCore<'a, R: AccessRules>: GetDrawing<'a, R> + GetBendIndex { +pub trait GetCore: GetBendIndex { + fn core(&self) -> FixedDotIndex; +} + +impl<'a, S: GetDrawing + GetBendIndex> GetCore for S { fn core(&self) -> FixedDotIndex { FixedDotIndex::new( self.drawing() @@ -96,22 +104,6 @@ pub trait GetCore<'a, R: AccessRules>: GetDrawing<'a, R> + GetBendIndex { } } -pub trait GetInnerOuter<'a, R: AccessRules>: GetDrawing<'a, R> + GetBendIndex { - fn inner(&self) -> Option { - self.drawing() - .geometry() - .inner(self.bend_index()) - .map(|ni| LooseBendIndex::new(ni.petgraph_index())) - } - - fn outer(&self) -> Option { - self.drawing() - .geometry() - .outer(self.bend_index()) - .map(|ni| LooseBendIndex::new(ni.petgraph_index())) - } -} - macro_rules! impl_primitive { ($primitive_struct:ident, $weight_struct:ident) => { impl<'a, CW: Copy, R: AccessRules> GetWeight<$weight_struct> @@ -218,7 +210,8 @@ impl<'a, W, CW: Copy, R: AccessRules> GetInterior } } -impl<'a, W, CW: Copy, R: AccessRules> GetDrawing<'a, R> for GenericPrimitive<'a, W, CW, R> { +impl<'a, W, CW: Copy, R: AccessRules> GetDrawing for GenericPrimitive<'a, W, CW, R> { + type Rules = R; fn drawing(&self) -> &Drawing { self.drawing } @@ -277,7 +270,7 @@ impl<'a, CW: Copy, R: AccessRules> GetLimbs for FixedDot<'a, CW, R> { } } -impl<'a, CW: Copy, R: AccessRules> GetFirstGear<'a, R> for FixedDot<'a, CW, R> {} +impl<'a, CW: Copy, R: AccessRules> GetFirstGear for FixedDot<'a, CW, R> {} pub type LooseDot<'a, CW, R> = GenericPrimitive<'a, LooseDotWeight, CW, R>; impl_loose_primitive!(LooseDot, LooseDotWeight); @@ -342,11 +335,6 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints for F } } -impl<'a, CW: Copy, R: AccessRules> GetOtherJoint - for FixedSeg<'a, CW, R> -{ -} - pub type LoneLooseSeg<'a, CW, R> = GenericPrimitive<'a, LoneLooseSegWeight, CW, R>; impl_loose_primitive!(LoneLooseSeg, LoneLooseSegWeight); @@ -370,11 +358,6 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints } } -impl<'a, CW: Copy, R: AccessRules> GetOtherJoint - for LoneLooseSeg<'a, CW, R> -{ -} - pub type SeqLooseSeg<'a, CW, R> = GenericPrimitive<'a, SeqLooseSegWeight, CW, R>; impl_loose_primitive!(SeqLooseSeg, SeqLooseSegWeight); @@ -408,11 +391,6 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints for SeqLoo } } -impl<'a, CW: Copy, R: AccessRules> GetOtherJoint - for SeqLooseSeg<'a, CW, R> -{ -} - pub type FixedBend<'a, CW, R> = GenericPrimitive<'a, FixedBendWeight, CW, R>; impl_fixed_primitive!(FixedBend, FixedBendWeight); @@ -442,13 +420,8 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints } } -impl<'a, CW: Copy, R: AccessRules> GetOtherJoint - for FixedBend<'a, CW, R> -{ -} -impl<'a, CW: Copy, R: AccessRules> GetFirstGear<'a, R> for FixedBend<'a, CW, R> {} -impl<'a, CW: Copy, R: AccessRules> GetCore<'a, R> for FixedBend<'a, CW, R> {} // TODO: Fixed bends don't have cores actually. - //impl<'a, R: QueryRules> GetInnerOuter for FixedBend<'a, CW, R> {} +impl<'a, CW: Copy, R: AccessRules> GetFirstGear for FixedBend<'a, CW, R> {} +//impl<'a, R: QueryRules> GetInnerOuter for FixedBend<'a, CW, R> {} pub type LooseBend<'a, CW, R> = GenericPrimitive<'a, LooseBendWeight, CW, R>; impl_loose_primitive!(LooseBend, LooseBendWeight); @@ -491,9 +464,18 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints } } -impl<'a, CW: Copy, R: AccessRules> GetOtherJoint - for LooseBend<'a, CW, R> -{ +impl<'a, CW: Copy, R: AccessRules> LooseBend<'a, CW, R> { + pub fn inner(&self) -> Option { + self.drawing() + .geometry() + .inner(self.bend_index()) + .map(|ni| LooseBendIndex::new(ni.petgraph_index())) + } + + pub fn outer(&self) -> Option { + self.drawing() + .geometry() + .outer(self.bend_index()) + .map(|ni| LooseBendIndex::new(ni.petgraph_index())) + } } -impl<'a, CW: Copy, R: AccessRules> GetCore<'a, R> for LooseBend<'a, CW, R> {} -impl<'a, CW: Copy, R: AccessRules> GetInnerOuter<'a, R> for LooseBend<'a, CW, R> {}