From def75e3afe549e27e712fdd1e2589aecaf047544 Mon Sep 17 00:00:00 2001 From: Arnaud de Bossoreille Date: Fri, 15 Nov 2024 21:50:58 +0100 Subject: [PATCH] Add DrawingPortId newtype and PortKey safety checks --- quirky_binder/src/drawing/graph/helper.rs | 20 ++++++++++-------- quirky_binder/src/drawing/graph/record.rs | 11 +++++----- quirky_binder/src/drawing/graph/stream.rs | 12 ++++++----- quirky_binder/src/drawing/mod.rs | 25 +++++++++++++---------- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/quirky_binder/src/drawing/graph/helper.rs b/quirky_binder/src/drawing/graph/helper.rs index 5e4cb83..df0c367 100644 --- a/quirky_binder/src/drawing/graph/helper.rs +++ b/quirky_binder/src/drawing/graph/helper.rs @@ -3,7 +3,7 @@ use std::collections::{btree_map::Entry, BTreeMap}; use crate::{ drawing::{ Drawing, DrawingColumn, DrawingEdge, DrawingNode, DrawingPort, DrawingPortAlign, - DrawingPortSize, DrawingPortsColumn, + DrawingPortId, DrawingPortSize, DrawingPortsColumn, }, prelude::*, }; @@ -26,7 +26,7 @@ const COLORS: [&str; 20] = [ pub struct DrawingHelper<'a, PortKey: Ord, ColorKey: Ord> { pub columns: Vec>, pub node_column_and_index: BTreeMap<&'a [Box], (usize, usize)>, - pub output_ports: BTreeMap, + pub output_ports: BTreeMap, pub edges: Vec>, pub edges_color: BTreeMap, pub next_color: usize, @@ -143,7 +143,7 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> { from: &PortKey, from_color_key: ColorKey, ) { - let input_port_id = *port_count; + let input_port_id = DrawingPortId::from(*port_count); *port_count += 1; let mut index = port_columns.len(); @@ -186,7 +186,7 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> { port_count: &mut usize, to: PortKey, ) { - let output_port_id = *port_count; + let output_port_id = DrawingPortId::from(*port_count); *port_count += 1; let mut index = port_columns.len(); @@ -217,7 +217,8 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> { }); } - self.output_ports.insert(to, output_port_id); + let old = self.output_ports.insert(to, output_port_id); + assert!(old.is_none()); } pub fn push_connected_ports( @@ -228,8 +229,8 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> { from_color_key: ColorKey, to: PortKey, ) { - let input_port_id = *port_count; - let output_port_id = *port_count + 1; + let input_port_id = DrawingPortId::from(*port_count); + let output_port_id = DrawingPortId::from(*port_count + 1); *port_count += 2; port_columns.push(DrawingPortsColumn { @@ -250,10 +251,11 @@ impl<'a, PortKey: Ord, ColorKey: Ord> DrawingHelper<'a, PortKey, ColorKey> { self.push_edge(from, input_port_id, from_color_key); - self.output_ports.insert(to, output_port_id); + let old = self.output_ports.insert(to, output_port_id); + assert!(old.is_none()); } - pub fn push_edge(&mut self, tail_key: &PortKey, head: usize, color_key: ColorKey) { + pub fn push_edge(&mut self, tail_key: &PortKey, head: DrawingPortId, color_key: ColorKey) { let color = self.get_color_for(color_key); self.edges.push(DrawingEdge { tail: self.output_ports[tail_key], diff --git a/quirky_binder/src/drawing/graph/record.rs b/quirky_binder/src/drawing/graph/record.rs index 47ac106..8218d9d 100644 --- a/quirky_binder/src/drawing/graph/record.rs +++ b/quirky_binder/src/drawing/graph/record.rs @@ -7,7 +7,8 @@ use super::{ }; use crate::{ drawing::{ - Drawing, DrawingPort, DrawingPortAlign, DrawingPortSize, DrawingPortsColumn, DynNode, + Drawing, DrawingPort, DrawingPortAlign, DrawingPortId, DrawingPortSize, DrawingPortsColumn, + DynNode, }, graph::visit::{visit_node, Visit}, prelude::*, @@ -33,7 +34,7 @@ impl<'a> RecordsDrawingHelper<'a> { let variant = &record_definition[stream.variant_id()]; for d in variant.data() { - let input_port_id = *port_count; + let input_port_id = DrawingPortId::from(*port_count); *port_count += 1; port_columns.push(DrawingPortsColumn { ports: vec![DrawingPort { @@ -78,7 +79,7 @@ impl<'a> RecordsDrawingHelper<'a> { let variant = &record_definition[stream.variant_id()]; for d in variant.data() { - let output_port_id = *port_count; + let output_port_id = DrawingPortId::from(*port_count); *port_count += 1; port_columns.push(DrawingPortsColumn { ports: vec![DrawingPort { @@ -120,8 +121,8 @@ impl<'a> RecordsDrawingHelper<'a> { let variant = &record_definition[stream.variant_id()]; for d in variant.data() { - let input_port_id = *port_count; - let output_port_id = *port_count + 1; + let input_port_id = DrawingPortId::from(*port_count); + let output_port_id = DrawingPortId::from(*port_count + 1); *port_count += 2; port_columns.push(DrawingPortsColumn { ports: vec![ diff --git a/quirky_binder/src/drawing/graph/stream.rs b/quirky_binder/src/drawing/graph/stream.rs index b80ac74..4554721 100644 --- a/quirky_binder/src/drawing/graph/stream.rs +++ b/quirky_binder/src/drawing/graph/stream.rs @@ -3,7 +3,9 @@ use truc::record::definition::{DatumId, RecordVariantId}; use super::helper::{drawing_source_node_name, DrawingHelper}; use crate::{ - drawing::{Drawing, DrawingPort, DrawingPortAlign, DrawingPortSize, DrawingPortsColumn}, + drawing::{ + Drawing, DrawingPort, DrawingPortAlign, DrawingPortId, DrawingPortSize, DrawingPortsColumn, + }, graph::visit::{visit_node, Visit}, prelude::*, }; @@ -23,7 +25,7 @@ impl<'a> StreamsDrawingHelper<'a> { stream: &'a NodeSubStream, depth: usize, ) { - let input_port_id = *port_count; + let input_port_id = DrawingPortId::from(*port_count); *port_count += 1; port_columns.push(DrawingPortsColumn { ports: vec![DrawingPort { @@ -68,7 +70,7 @@ impl<'a> StreamsDrawingHelper<'a> { stream: &'a NodeSubStream, depth: usize, ) { - let output_port_id = *port_count; + let output_port_id = DrawingPortId::from(*port_count); *port_count += 1; port_columns.push(DrawingPortsColumn { ports: vec![DrawingPort { @@ -112,8 +114,8 @@ impl<'a> StreamsDrawingHelper<'a> { stream: &'a NodeSubStream, depth: usize, ) { - let input_port_id = *port_count; - let output_port_id = *port_count + 1; + let input_port_id = DrawingPortId::from(*port_count); + let output_port_id = DrawingPortId::from(*port_count + 1); *port_count += 2; port_columns.push(DrawingPortsColumn { ports: vec![ diff --git a/quirky_binder/src/drawing/mod.rs b/quirky_binder/src/drawing/mod.rs index 13e3e06..d490dc3 100644 --- a/quirky_binder/src/drawing/mod.rs +++ b/quirky_binder/src/drawing/mod.rs @@ -28,9 +28,12 @@ pub struct DrawingPortsColumn { pub align: DrawingPortAlign, } +#[derive(Debug, Clone, Copy, From, Deref)] +pub struct DrawingPortId(usize); + #[derive(Debug)] pub struct DrawingPort { - pub id: usize, + pub id: DrawingPortId, pub size: DrawingPortSize, pub redundant: bool, } @@ -51,8 +54,8 @@ pub enum DrawingPortAlign { #[derive(Debug)] pub struct DrawingEdge<'a> { - pub tail: usize, - pub head: usize, + pub tail: DrawingPortId, + pub head: DrawingPortId, pub color: &'a str, } @@ -286,12 +289,12 @@ where } }; let mut go_down_next = false; - for DrawingPort { - id, - size, - redundant, - } in ports - { + for port in ports { + let DrawingPort { + id, + size, + redundant, + } = *port; if !redundant { if go_down_next { top += PORT_Y_DISTANCE; @@ -308,8 +311,8 @@ where + col * (LABEL_MAX_WIDTH + BLANK_WIDTH) + (ports_count + port_col) * PORT_X_DISTANCE, cy: top, - size: *size, - redundant: *redundant, + size, + redundant, }, ); }