pub fn push(&mut self, key: K, value: V) -> Option<(K, V)> {
        if let Entry::Occupied(occupied) = self.values.entry(key) {
            // If already exists - replace existing value
            occupied.replace_entry(value);

            return None;
        }

        if self.ring.len() < self.capacity {
            // Still populating the ring
            assert_eq!(self.start_idx, 0);
            self.ring.push(key);
            self.values.insert(key, value);

            return None;
        }

        // Need to swap something out of the ring
        let mut old_key = key;
        std::mem::swap(&mut self.ring[self.start_idx], &mut old_key);

        self.start_idx += 1;
        if self.start_idx == self.capacity {
            self.start_idx = 0;
        }

        let old_value = self.values.remove(&old_key).unwrap();
        self.values.insert(key, value);

        Some((old_key, old_value))
    }
/// Push a new value into the ring buffer
///
/// If a value with the given key already exists, it will replace the value
/// Otherwise it will add the key and value to the buffer.
///
/// If there is insufficient capacity it will drop the oldest key value pair
/// from the buffer.
///
/// This returns the replaced value (if any).
Push a new value into the ring buffer
If a value with the given key already exists, it will replace the value
Otherwise it will add the key and value to the buffer.

If there is insufficient capacity it will drop the oldest key value pair
from the buffer.

This returns the replaced value (if any).
Push a new value into the ring buffer If a value with the given key already exists it will replace the value Otherwise it will add the key and value to the buffer . If there is insufficient capacity it will drop the oldest key value pair from the buffer . This returns the replaced value ( if any ) .
pub fn push(&mut self, key: K, value: V) -> Option<(K, V)> {
        if let Entry::Occupied(occupied) = self.values.entry(key) {
            // If already exists - replace existing value
            occupied.replace_entry(value);
            return None;
        }
        if self.ring.len() < self.capacity {
            // Still populating the ring
            assert_eq!(self.start_idx, 0);
            self.ring.push(key);
            self.values.insert(key, value);
            return None;
        }
        // Need to swap something out of the ring
        let mut old_key = key;
        std::mem::swap(&mut self.ring[self.start_idx], &mut old_key);
        self.start_idx += 1;
        if self.start_idx == self.capacity {
            self.start_idx = 0;
        }
        let old_value = self.values.remove(&old_key).unwrap();
        self.values.insert(key, value);
        Some((old_key, old_value))
    }
pub fn push ( & mut self , key : K , value : V ) -> Option < ( K , V ) > { if let Entry :: Occupied ( occupied ) = self . values . entry ( key ) { // If already exists - replace existing value occupied . replace_entry ( value ) ; return None ; } if self . ring . len ( ) < self . capacity { // Still populating the ring assert_eq ! ( self start_idx 0 ) ; self . ring . push ( key ) ; self . values . insert ( key , value ) ; return None ; } // Need to swap something out of the ring let mut old_key = key ; std :: mem :: swap ( & mut self . ring [ self . start_idx ] , & mut old_key ) ; self . start_idx += 1 ; if self . start_idx == self . capacity { self . start_idx = 0 ; } let old_value = self . values . remove ( & old_key ) . unwrap ( ) ; self . values . insert ( key , value ) ; Some ( ( old_key , old_value ) ) }
Push a new value into the ring buffer
If a value with the given key already exists, it will replace the value
Otherwise it will add the key and value to the buffer.
Push a new value into the ring buffer If a value with the given key already exists it will replace the value Otherwise it will add the key and value to the buffer .
&mut self
key
K
value
V
&mut self
key
K
value
V