Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions rust/rubydex/src/model/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NameId>,
ref_count: usize,
ref_count: u32,
}
assert_mem_size!(Name, 48);

Expand Down Expand Up @@ -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]
Expand Down
14 changes: 10 additions & 4 deletions rust/rubydex/src/model/string_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::ops::Deref;
#[derive(Debug)]
pub struct StringRef {
value: String,
ref_count: usize,
ref_count: u32,
}

impl StringRef {
Expand All @@ -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]
Expand Down
Loading