Compare elements by serial number, not address

Rust pointers don't seem to be designed for use as identifiers, so it's
probably more idiomatic and possibly more reliable to manage our own
unique identifier system.

  https://stackoverflow.com/a/72149089

This also lets us get rid of the `ElementRc` newtype.
This commit is contained in:
Aaron Fenyes 2025-05-03 00:08:17 -07:00
parent 347981201c
commit 17f30d1312
2 changed files with 5 additions and 15 deletions

View file

@ -97,17 +97,9 @@ pub trait Element: ProblemPoser + DisplayItem {
fn set_column_index(&self, index: usize);
}
// the `Element` trait needs to be dyn-compatible, so its method signatures can
// only use `Self` in the type of the receiver. that means `Element` can't
// implement `PartialEq`. if you need partial equivalence for `Element` trait
// objects, use this wrapper
#[derive(Clone)]
pub struct ElementRc(pub Rc<dyn Element>);
impl PartialEq for ElementRc {
fn eq(&self, ElementRc(other): &Self) -> bool {
let ElementRc(rc) = self;
Rc::ptr_eq(rc, &other)
impl PartialEq for dyn Element {
fn eq(&self, other: &Self) -> bool {
self.serial() == other.serial()
}
}

View file

@ -12,7 +12,6 @@ use crate::{
assembly::{
Element,
ElementKey,
ElementRc,
HalfCurvatureRegulator,
InversiveDistanceRegulator,
Regulator,
@ -254,7 +253,6 @@ pub fn Outline() -> View {
.clone()
.into_iter()
.sorted_by_key(|(_, elt)| elt.id().clone())
.map(|(key, elt)| (key, ElementRc(elt)))
.collect()
);
@ -268,10 +266,10 @@ pub fn Outline() -> View {
) {
Keyed(
list=element_list,
view=|(key, ElementRc(elt))| view! {
view=|(key, elt)| view! {
ElementOutlineItem(key=key, element=elt)
},
key=|(_, ElementRc(elt))| elt.serial()
key=|(_, elt)| elt.serial()
)
}
}