From 2444baffefce7ad1ff6ab356bdf7129999ca2cc9 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Wed, 28 Jan 2026 14:54:56 -0500 Subject: [PATCH] Reduce ref count sizes --- rust/rubydex/src/model/name.rs | 20 +++++++++++++------- rust/rubydex/src/model/string_ref.rs | 14 ++++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/rust/rubydex/src/model/name.rs b/rust/rubydex/src/model/name.rs index c0512c541..b017d9d5f 100644 --- a/rust/rubydex/src/model/name.rs +++ b/rust/rubydex/src/model/name.rs @@ -82,7 +82,7 @@ pub struct Name { /// The ID of the name for the nesting where we found this name. This effectively turns the structure into a linked /// list of names to represent the nesting nesting: Option, - ref_count: usize, + ref_count: u32, } assert_mem_size!(Name, 48); @@ -190,18 +190,24 @@ impl NameRef { } #[must_use] - pub fn ref_count(&self) -> usize { + pub fn ref_count(&self) -> u32 { match self { NameRef::Unresolved(name) => name.ref_count, NameRef::Resolved(resolved_name) => resolved_name.name.ref_count, } } - pub fn increment_ref_count(&mut self, count: usize) { - match self { - NameRef::Unresolved(name) => name.ref_count += count, - NameRef::Resolved(resolved_name) => resolved_name.name.ref_count += count, - } + /// # Panics + /// + /// Panics if we exceed the maximum size of the reference count + pub fn increment_ref_count(&mut self, count: u32) { + let ref_count = match self { + NameRef::Unresolved(name) => &mut name.ref_count, + NameRef::Resolved(resolved_name) => &mut resolved_name.name.ref_count, + }; + *ref_count = ref_count + .checked_add(count) + .expect("Should not exceed maximum name ref count"); } #[must_use] diff --git a/rust/rubydex/src/model/string_ref.rs b/rust/rubydex/src/model/string_ref.rs index 4989f8f4c..b93c0a609 100644 --- a/rust/rubydex/src/model/string_ref.rs +++ b/rust/rubydex/src/model/string_ref.rs @@ -9,7 +9,7 @@ use std::ops::Deref; #[derive(Debug)] pub struct StringRef { value: String, - ref_count: usize, + ref_count: u32, } impl StringRef { @@ -19,12 +19,18 @@ impl StringRef { } #[must_use] - pub fn ref_count(&self) -> usize { + pub fn ref_count(&self) -> u32 { self.ref_count } - pub fn increment_ref_count(&mut self, count: usize) { - self.ref_count += count; + /// # Panics + /// + /// This function will panic if the reference count would exceed `u32::MAX` + pub fn increment_ref_count(&mut self, count: u32) { + self.ref_count = self + .ref_count + .checked_add(count) + .expect("Should not exceed maximum string ref count"); } #[must_use]