Browse Source

feat(ThingBuf): add `pop_with` and `push_with`

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Eliza Weisman 3 years ago
parent
commit
9192c60385
1 changed files with 11 additions and 2 deletions
  1. 11 2
      src/lib.rs

+ 11 - 2
src/lib.rs

@@ -128,6 +128,10 @@ impl<T> ThingBuf<T> {
         }
     }
 
+    #[inline]
+    pub fn push_with<U>(&self, f: impl FnOnce(&mut T) -> U) -> Result<U, AtCapacity> {
+        self.push_ref().map(|mut r| r.with_mut(f))
+    }
     pub fn push_ref(&self) -> Result<Ref<'_, T>, AtCapacity> {
         let mut backoff = Backoff::new();
         let mut tail = self.tail.load(Ordering::Relaxed);
@@ -176,6 +180,11 @@ impl<T> ThingBuf<T> {
         }
     }
 
+    #[inline]
+    pub fn pop_with<U>(&self, f: impl FnOnce(&mut T) -> U) -> Option<U> {
+        self.pop_ref().map(|mut r| r.with_mut(f))
+    }
+
     pub fn pop_ref(&self) -> Option<Ref<'_, T>> {
         let mut backoff = Backoff::new();
         let mut head = self.head.load(Ordering::Relaxed);
@@ -244,7 +253,7 @@ impl<T> fmt::Debug for ThingBuf<T> {
 
 impl<T> Ref<'_, T> {
     #[inline]
-    pub fn with<U>(&self, f: impl Fn(&T) -> U) -> U {
+    pub fn with<U>(&self, f: impl FnOnce(&T) -> U) -> U {
         self.slot.value.with(|value| unsafe {
             // Safety: if a `Ref` exists, we have exclusive ownership of the slot.
             f(&*value)
@@ -252,7 +261,7 @@ impl<T> Ref<'_, T> {
     }
 
     #[inline]
-    pub fn with_mut<U>(&mut self, f: impl Fn(&mut T) -> U) -> U {
+    pub fn with_mut<U>(&mut self, f: impl FnOnce(&mut T) -> U) -> U {
         self.slot.value.with_mut(|value| unsafe {
             // Safety: if a `Ref` exists, we have exclusive ownership of the slot.
             f(&mut *value)