diff --git a/wutil-rs/src/bag.rs b/wutil-rs/src/bag.rs index 5115c91f..ffca050d 100644 --- a/wutil-rs/src/bag.rs +++ b/wutil-rs/src/bag.rs @@ -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 { - self.search(key).ok() - } - - /// Returns the value associated with `key`. - fn find_value(&self, key: c_int) -> Option> { - 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) { 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 {