From e448f86374e1b2eda6cca493f70b6a0eeb7b8add Mon Sep 17 00:00:00 2001 From: Ellen Emilia Anna Zscheile Date: Fri, 2 May 2025 01:47:32 +0200 Subject: [PATCH] refactor(drawing): make traits more strict --- src/drawing/cane.rs | 4 +++- src/drawing/primitive.rs | 49 ++++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/drawing/cane.rs b/src/drawing/cane.rs index 408a1c3..7ac7ca1 100644 --- a/src/drawing/cane.rs +++ b/src/drawing/cane.rs @@ -43,7 +43,9 @@ impl GetInterior for Cane { } } -impl GetJoints for Cane { +impl GetJoints for Cane { + type F = SeqLooseSegIndex; + type T = LooseBendIndex; fn joints(&self) -> (SeqLooseSegIndex, LooseBendIndex) { (self.seg, self.bend) } diff --git a/src/drawing/primitive.rs b/src/drawing/primitive.rs index 2d26d97..e8c1105 100644 --- a/src/drawing/primitive.rs +++ b/src/drawing/primitive.rs @@ -26,8 +26,9 @@ pub trait GetDrawing { } #[enum_dispatch] -pub trait GetWeight { - fn weight(&self) -> W; +pub trait GetWeight { + type Weight; + fn weight(&self) -> Self::Weight; } #[enum_dispatch] @@ -57,13 +58,18 @@ pub trait GetInterior { fn interior(&self) -> Vec; } -pub trait GetOtherJoint: GetJoints { - fn other_joint(&self, end: F) -> F; +pub trait GetOtherJoint: GetJoints { + type J; + fn other_joint(&self, end: Self::J) -> Self::J; } -impl, S: GetJoints> GetOtherJoint - for S +impl GetOtherJoint for S +where + F: GetPetgraphIndex, + S: GetJoints, + ::T: GetPetgraphIndex + Into, { + type J = F; fn other_joint(&self, end: F) -> F { let joints = self.joints(); if joints.0.petgraph_index() != end.petgraph_index() { @@ -74,8 +80,10 @@ impl, S: GetJoints> Get } } -pub trait GetJoints { - fn joints(&self) -> (F, T); +pub trait GetJoints { + type F; + type T; + fn joints(&self) -> (Self::F, Self::T); } pub trait GetFirstGear: GetDrawing + GetPetgraphIndex { @@ -108,7 +116,8 @@ impl GetCore for S { macro_rules! impl_primitive { ($primitive_struct:ident, $weight_struct:ident) => { - impl GetWeight<$weight_struct> for $primitive_struct<'_, CW, Cel, R> { + impl GetWeight for $primitive_struct<'_, CW, Cel, R> { + type Weight = $weight_struct; fn weight(&self) -> $weight_struct { if let PrimitiveWeight::$primitive_struct(weight) = self.tagged_weight() { weight @@ -225,7 +234,7 @@ impl GetPetgraphIndex for GenericPrimitive<'_, W, CW, Cel, R> { impl<'a, W: GetWidth, CW, Cel, R> GetWidth for GenericPrimitive<'a, W, CW, Cel, R> where - GenericPrimitive<'a, W, CW, Cel, R>: GetWeight, + GenericPrimitive<'a, W, CW, Cel, R>: GetWeight, { fn width(&self) -> f64 { self.weight().width() @@ -325,7 +334,9 @@ impl MakePrimitiveShape for FixedSeg<'_, CW, Cel, R> { impl GetLimbs for FixedSeg<'_, CW, Cel, R> {} -impl GetJoints for FixedSeg<'_, CW, Cel, R> { +impl GetJoints for FixedSeg<'_, CW, Cel, R> { + type F = FixedDotIndex; + type T = FixedDotIndex; fn joints(&self) -> (FixedDotIndex, FixedDotIndex) { let (from, to) = self.drawing.geometry().seg_joints(self.index.into()); ( @@ -346,7 +357,9 @@ impl MakePrimitiveShape for LoneLooseSeg<'_, CW, Cel, R> { impl GetLimbs for LoneLooseSeg<'_, CW, Cel, R> {} -impl GetJoints for LoneLooseSeg<'_, CW, Cel, R> { +impl GetJoints for LoneLooseSeg<'_, CW, Cel, R> { + type F = FixedDotIndex; + type T = FixedDotIndex; fn joints(&self) -> (FixedDotIndex, FixedDotIndex) { let (from, to) = self.drawing.geometry().seg_joints(self.index.into()); ( @@ -367,7 +380,9 @@ impl MakePrimitiveShape for SeqLooseSeg<'_, CW, Cel, R> { impl GetLimbs for SeqLooseSeg<'_, CW, Cel, R> {} -impl GetJoints for SeqLooseSeg<'_, CW, Cel, R> { +impl GetJoints for SeqLooseSeg<'_, CW, Cel, R> { + type F = DotIndex; + type T = LooseDotIndex; fn joints(&self) -> (DotIndex, LooseDotIndex) { let joints = self.drawing.geometry().seg_joints(self.index.into()); if let DotWeight::Fixed(..) = self.drawing.geometry().dot_weight(joints.0) { @@ -406,7 +421,9 @@ impl MakePrimitiveShape for FixedBend<'_, CW, Cel, R> { impl GetLimbs for FixedBend<'_, CW, Cel, R> {} -impl GetJoints for FixedBend<'_, CW, Cel, R> { +impl GetJoints for FixedBend<'_, CW, Cel, R> { + type F = FixedDotIndex; + type T = FixedDotIndex; fn joints(&self) -> (FixedDotIndex, FixedDotIndex) { let (from, to) = self.drawing.geometry().bend_joints(self.index.into()); ( @@ -448,7 +465,9 @@ impl GetOffset for LooseBend<'_, CW, Cel, R> { } } -impl GetJoints for LooseBend<'_, CW, Cel, R> { +impl GetJoints for LooseBend<'_, CW, Cel, R> { + type F = LooseDotIndex; + type T = LooseDotIndex; fn joints(&self) -> (LooseDotIndex, LooseDotIndex) { let (from, to) = self.drawing.geometry().bend_joints(self.index.into()); (