Give each element a serial number #22

Merged
glen merged 6 commits from element-serial into main 2024-11-22 02:25:11 +00:00
Showing only changes of commit 133b725053 - Show all commits

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
} }
} }