From 133b725053f8c925f416d7e6e7f6f91b11be62b7 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Thu, 21 Nov 2024 16:42:43 -0800 Subject: [PATCH] Panic if we run out of serial numbers --- app-proto/src/assembly.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 92eaa62..8cf3bb3 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -44,13 +44,24 @@ impl Element { color: ElementColor, representation: DVector ) -> Element { + // take the next serial number, panicking if that was the last number we + // had left. the technique we use to panic on overflow is taken from + // _Rust Atomics and Locks_, by Mara Bos + // + // https://marabos.nl/atomics/atomics.html#example-handle-overflow + // + let serial = NEXT_ELEMENT_SERIAL.fetch_update( + Ordering::SeqCst, Ordering::SeqCst, + |serial| serial.checked_add(1) + ).expect("Out of serial numbers for elements"); + Element { id: id, label: label, color: color, representation: create_signal(representation), constraints: create_signal(BTreeSet::default()), - serial: NEXT_ELEMENT_SERIAL.fetch_add(1, Ordering::SeqCst), + serial: serial, column_index: 0 } }