Move functions on Bag which were only called by FFI code inline.

This commit was merged in pull request #4.
This commit is contained in:
2025-11-12 17:03:56 -05:00
parent 8270124869
commit 0097d1819e

View File

@@ -29,25 +29,6 @@ impl Bag {
self.inner.binary_search_by_key(&key, |(key, _)| *key)
}
/// Returns the index of `self.inner` where `key` is found.
fn find_index(&self, key: c_int) -> Option<usize> {
self.search(key).ok()
}
/// Returns the value associated with `key`.
fn find_value(&self, key: c_int) -> Option<NonNull<c_void>> {
let i = self.find_index(key)?;
self.inner.get(i).copied().map(|(_, value)| value)
}
/// Removes any value associated with `key`.
fn remove(&mut self, key: c_int) {
let Some(i) = self.find_index(key) else {
return;
};
self.inner.remove(i);
}
/// Sets a value associated with `key` to `value`. Clobbers any extant value.
fn set(&mut self, key: c_int, value: NonNull<c_void>) {
match self.search(key) {
@@ -77,12 +58,13 @@ pub mod ffi {
if bag.is_null() {
return ptr::null_mut();
}
unsafe {
(*bag)
.find_value(key)
.map(|p| p.as_ptr())
.unwrap_or(ptr::null_mut())
}
let bag = unsafe { &mut *bag };
bag
.search(key)
.ok()
.and_then(|index| bag.inner.get(index).copied())
.map(|(_, value)| value.as_ptr())
.unwrap_or(ptr::null_mut())
}
/// Sets the value associated with `key` to `item`. If `item` is null,
@@ -97,8 +79,9 @@ pub mod ffi {
if let Some(item) = NonNull::new(item) {
bag.set(key, item);
} else {
bag.remove(key);
return;
if let Some(i) = bag.search(key).ok() {
bag.inner.remove(i);
}
}
}
@@ -255,7 +238,7 @@ pub mod ffi {
return ptr::null_mut();
}
if let Some(index) = bag.find_index(key) {
if let Some(index) = bag.search(key).ok() {
*ptr = index as c_int;
bag.inner[index].1.as_ptr()
} else {