Panic if we run out of serial numbers

This commit is contained in:
Aaron Fenyes 2024-11-21 16:42:43 -08:00
parent c5f09b99b3
commit 133b725053

View File

@ -44,13 +44,24 @@ impl Element {
color: ElementColor, color: ElementColor,
representation: DVector<f64> representation: DVector<f64>
) -> Element { ) -> 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 { Element {
id: id, id: id,
label: label, label: label,
color: color, color: color,
representation: create_signal(representation), representation: create_signal(representation),
constraints: create_signal(BTreeSet::default()), constraints: create_signal(BTreeSet::default()),
serial: NEXT_ELEMENT_SERIAL.fetch_add(1, Ordering::SeqCst), serial: serial,
column_index: 0 column_index: 0
} }
} }