pub fn insert(&mut self, key: K, value: V) -> Option<V> {
        if key.is_null() {
            return None;
        }

        let kd = key.data();

        if let Some(slot) = self.slots.get_mut(&kd.idx) {
            if slot.version == kd.version.get() {
                return Some(std::mem::replace(&mut slot.value, value));
            }

            // Don't replace existing newer values.
            if is_older_version(kd.version.get(), slot.version) {
                return None;
            }

            *slot = Slot {
                version: kd.version.get(),
                value,
            };

            return None;
        }

        self.slots.insert(kd.idx, Slot {
            version: kd.version.get(),
            value,
        });

        None
    }
/// Inserts a value into the secondary map at the given `key`. Can silently
/// fail if `key` was removed from the originating slot map.
///
/// Returns [`None`] if this key was not present in the map, the old value
/// otherwise.
///
/// # Examples
///
/// ```
/// # use slotmap::*;
/// let mut sm = SlotMap::new();
/// let k = sm.insert(4);
/// let mut squared = SparseSecondaryMap::new();
/// assert_eq!(squared.insert(k, 0), None);
/// assert_eq!(squared.insert(k, 4), Some(0));
/// // You don't have to use insert if the key is already in the secondary map.
/// squared[k] *= squared[k];
/// assert_eq!(squared[k], 16);
/// ```
Inserts a value into the secondary map at the given `key`. Can silently
fail if `key` was removed from the originating slot map.

Returns [`None`] if this key was not present in the map, the old value
otherwise.
Inserts a value into the secondary map at the given ` key ` . Can silently fail if ` key ` was removed from the originating slot map . Returns [ ` None ` ] if this key was not present in the map the old value otherwise .
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
        if key.is_null() {
            return None;
        }
        let kd = key.data();
        if let Some(slot) = self.slots.get_mut(&kd.idx) {
            if slot.version == kd.version.get() {
                return Some(std::mem::replace(&mut slot.value, value));
            }
            // Don't replace existing newer values.
            if is_older_version(kd.version.get(), slot.version) {
                return None;
            }
            *slot = Slot {
                version: kd.version.get(),
                value,
            };
            return None;
        }
        self.slots.insert(kd.idx, Slot {
            version: kd.version.get(),
            value,
        });
        None
    }
pub fn insert ( & mut self , key : K , value : V ) -> Option < V > { if key . is_null ( ) { return None ; } let kd = key . data ( ) ; if let Some ( slot ) = self . slots . get_mut ( & kd . idx ) { if slot . version == kd . version . get ( ) { return Some ( std :: mem :: replace ( & mut slot . value , value ) ) ; } // Don't replace existing newer values. if is_older_version ( kd . version . get ( ) , slot . version ) { return None ; } * slot = Slot { version : kd . version . get ( ) , value , } ; return None ; } self . slots . insert ( kd . idx , Slot { version : kd . version . get ( ) , value , } ) ; None }
Inserts a value into the secondary map at the given `key`.
Inserts a value into the secondary map at the given ` key ` .
&mut self
key
K
value
V
examples
None
use slotmap::*;
let mut sm = SlotMap::new();
let k = sm.insert(4);
let mut squared = SparseSecondaryMap::new();
assert_eq!(squared.insert(k, 0), None);
assert_eq!(squared.insert(k, 4), Some(0));
You don't have to use insert if the key is already in the secondary map.
let mut sm = SlotMap :: new () ; let k = sm . insert ( 4 ) ; let mut squared = SparseSecondaryMap :: new () ; assert_eq! ( squared . insert ( k 0 ) None ) ; assert_eq! ( squared . insert ( k 4 ) Some ( 0 )) ; You don ' t have to use insert if the key is already in the secondary map .