refactor(Retag): only implement trait once, Index should be associated type

This commit is contained in:
Ellen Emilia Anna Zscheile 2025-01-19 21:28:34 +01:00
parent 1e690ace11
commit 61c7b5450f
8 changed files with 41 additions and 22 deletions

View File

@ -6,7 +6,7 @@ use enum_dispatch::enum_dispatch;
use crate::{ use crate::{
drawing::{ drawing::{
graph::{GetLayer, GetMaybeNet, MakePrimitive, PrimitiveIndex, PrimitiveWeight, Retag}, graph::{GetLayer, GetMaybeNet, MakePrimitive, PrimitiveIndex, PrimitiveWeight},
primitive::{GenericPrimitive, Primitive}, primitive::{GenericPrimitive, Primitive},
rules::AccessRules, rules::AccessRules,
Drawing, Drawing,

View File

@ -9,7 +9,7 @@ use petgraph::stable_graph::NodeIndex;
use crate::{ use crate::{
drawing::{ drawing::{
graph::{GetLayer, GetMaybeNet, MakePrimitive, PrimitiveIndex, PrimitiveWeight, Retag}, graph::{GetLayer, GetMaybeNet, MakePrimitive, PrimitiveIndex, PrimitiveWeight},
primitive::{GenericPrimitive, Primitive}, primitive::{GenericPrimitive, Primitive},
rules::AccessRules, rules::AccessRules,
Drawing, Drawing,

View File

@ -5,7 +5,7 @@
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use petgraph::stable_graph::NodeIndex; use petgraph::stable_graph::NodeIndex;
use crate::{drawing::Drawing, graph::GetPetgraphIndex}; use crate::graph::{GenericIndex, GetPetgraphIndex};
use super::{ use super::{
bend::{FixedBendIndex, FixedBendWeight, LooseBendIndex, LooseBendWeight}, bend::{FixedBendIndex, FixedBendWeight, LooseBendIndex, LooseBendWeight},
@ -16,11 +16,12 @@ use super::{
FixedSegIndex, FixedSegWeight, LoneLooseSegIndex, LoneLooseSegWeight, SeqLooseSegIndex, FixedSegIndex, FixedSegWeight, LoneLooseSegIndex, LoneLooseSegWeight, SeqLooseSegIndex,
SeqLooseSegWeight, SeqLooseSegWeight,
}, },
Drawing,
}; };
#[enum_dispatch] pub trait Retag {
pub trait Retag<PrimitiveIndex> { type Index: Sized + GetPetgraphIndex + PartialEq + Copy;
fn retag(&self, index: NodeIndex<usize>) -> PrimitiveIndex; fn retag(&self, index: NodeIndex<usize>) -> Self::Index;
} }
#[enum_dispatch] #[enum_dispatch]
@ -61,12 +62,6 @@ pub trait MakePrimitive {
macro_rules! impl_weight_forward { macro_rules! impl_weight_forward {
($weight_struct:ty, $weight_variant:ident, $index_struct:ident) => { ($weight_struct:ty, $weight_variant:ident, $index_struct:ident) => {
impl Retag<PrimitiveIndex> for $weight_struct {
fn retag(&self, index: NodeIndex<usize>) -> PrimitiveIndex {
PrimitiveIndex::$weight_variant($index_struct::new(index))
}
}
impl GetLayer for $weight_struct { impl GetLayer for $weight_struct {
fn layer(&self) -> usize { fn layer(&self) -> usize {
self.0.layer() self.0.layer()
@ -112,7 +107,7 @@ pub enum PrimitiveIndex {
LooseBend(LooseBendIndex), LooseBend(LooseBendIndex),
} }
#[enum_dispatch(GetWidth, GetLayer, Retag<PrimitiveIndex>)] #[enum_dispatch(GetWidth, GetLayer)]
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum PrimitiveWeight { pub enum PrimitiveWeight {
FixedDot(FixedDotWeight), FixedDot(FixedDotWeight),
@ -123,3 +118,27 @@ pub enum PrimitiveWeight {
FixedBend(FixedBendWeight), FixedBend(FixedBendWeight),
LooseBend(LooseBendWeight), LooseBend(LooseBendWeight),
} }
impl Retag for PrimitiveWeight {
type Index = PrimitiveIndex;
fn retag(&self, index: NodeIndex<usize>) -> PrimitiveIndex {
macro_rules! match_self {
($self:expr, $($kind:ident),*,) => {{
match $self {
$(PrimitiveWeight::$kind(_) => PrimitiveIndex::$kind(GenericIndex::new(index))),*
}
}}
}
match_self!(
self,
FixedDot,
LooseDot,
FixedSeg,
LoneLooseSeg,
SeqLooseSeg,
FixedBend,
LooseBend,
)
}
}

View File

@ -6,7 +6,7 @@ use enum_dispatch::enum_dispatch;
use crate::{ use crate::{
drawing::{ drawing::{
graph::{GetLayer, GetMaybeNet, MakePrimitive, PrimitiveIndex, PrimitiveWeight, Retag}, graph::{GetLayer, GetMaybeNet, MakePrimitive, PrimitiveIndex, PrimitiveWeight},
primitive::{GenericPrimitive, Primitive}, primitive::{GenericPrimitive, Primitive},
rules::AccessRules, rules::AccessRules,
Drawing, Drawing,

View File

@ -12,7 +12,7 @@ use crate::{
use super::{AccessBendWeight, AccessDotWeight, AccessSegWeight, GetWidth}; use super::{AccessBendWeight, AccessDotWeight, AccessSegWeight, GetWidth};
pub trait ApplyGeometryEdit< pub trait ApplyGeometryEdit<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer, DW: AccessDotWeight + Into<PW> + GetLayer,
SW: AccessSegWeight + Into<PW> + GetLayer, SW: AccessSegWeight + Into<PW> + GetLayer,
BW: AccessBendWeight + Into<PW> + GetLayer, BW: AccessBendWeight + Into<PW> + GetLayer,
@ -37,7 +37,7 @@ pub struct GeometryEdit<PW, DW, SW, BW, CW, PI, DI, SI, BI> {
} }
impl< impl<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer, DW: AccessDotWeight + Into<PW> + GetLayer,
SW: AccessSegWeight + Into<PW> + GetLayer, SW: AccessSegWeight + Into<PW> + GetLayer,
BW: AccessBendWeight + Into<PW> + GetLayer, BW: AccessBendWeight + Into<PW> + GetLayer,

View File

@ -113,7 +113,7 @@ impl<PW, DW, SW, BW, CW, PI, DI, SI, BI> Geometry<PW, DW, SW, BW, CW, PI, DI, SI
} }
impl< impl<
PW: GetWidth + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW>, DW: AccessDotWeight + Into<PW>,
SW: AccessSegWeight + Into<PW>, SW: AccessSegWeight + Into<PW>,
BW: AccessBendWeight + Into<PW>, BW: AccessBendWeight + Into<PW>,

View File

@ -27,7 +27,7 @@ pub struct RecordingGeometryWithRtree<PW, DW, SW, BW, CW, PI, DI, SI, BI> {
} }
impl< impl<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer, DW: AccessDotWeight + Into<PW> + GetLayer,
SW: AccessSegWeight + Into<PW> + GetLayer, SW: AccessSegWeight + Into<PW> + GetLayer,
BW: AccessBendWeight + Into<PW> + GetLayer, BW: AccessBendWeight + Into<PW> + GetLayer,
@ -338,7 +338,7 @@ fn edit_remove_from_map<I: Ord, T>(
} }
impl< impl<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer, DW: AccessDotWeight + Into<PW> + GetLayer,
SW: AccessSegWeight + Into<PW> + GetLayer, SW: AccessSegWeight + Into<PW> + GetLayer,
BW: AccessBendWeight + Into<PW> + GetLayer, BW: AccessBendWeight + Into<PW> + GetLayer,

View File

@ -49,7 +49,7 @@ pub struct GeometryWithRtree<PW, DW, SW, BW, CW, PI, DI, SI, BI> {
#[debug_invariant(self.test_envelopes())] #[debug_invariant(self.test_envelopes())]
#[debug_invariant(self.geometry.graph().node_count() == self.rtree.size())] #[debug_invariant(self.geometry.graph().node_count() == self.rtree.size())]
impl< impl<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer, DW: AccessDotWeight + Into<PW> + GetLayer,
SW: AccessSegWeight + Into<PW> + GetLayer, SW: AccessSegWeight + Into<PW> + GetLayer,
BW: AccessBendWeight + Into<PW> + GetLayer, BW: AccessBendWeight + Into<PW> + GetLayer,
@ -262,7 +262,7 @@ impl<
} }
impl< impl<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer, DW: AccessDotWeight + Into<PW> + GetLayer,
SW: AccessSegWeight + Into<PW> + GetLayer, SW: AccessSegWeight + Into<PW> + GetLayer,
BW: AccessBendWeight + Into<PW> + GetLayer, BW: AccessBendWeight + Into<PW> + GetLayer,
@ -392,7 +392,7 @@ impl<
} }
impl< impl<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<Index = PI> + Copy,
DW: AccessDotWeight + Into<PW> + GetLayer, DW: AccessDotWeight + Into<PW> + GetLayer,
SW: AccessSegWeight + Into<PW> + GetLayer, SW: AccessSegWeight + Into<PW> + GetLayer,
BW: AccessBendWeight + Into<PW> + GetLayer, BW: AccessBendWeight + Into<PW> + GetLayer,