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

        let kd = key.data();
        self.slots
            .extend((self.slots.len()..=kd.idx as usize).map(|_| Slot::new_vacant()));

        let slot = &mut self.slots[kd.idx as usize];
        if slot.version() == kd.version.get() {
            // Is always occupied.
            return Some(replace(unsafe { slot.get_unchecked_mut() }, value));
        }

        if slot.occupied() {
            // Don't replace existing newer values.
            if is_older_version(kd.version.get(), slot.version()) {
                return None;
            }
        } else {
            self.num_elems += 1;
        }

        *slot = Slot::new_occupied(kd.version.get(), value);
        None
    }
/// Inserts a value into the secondary map at the given `key`. Can silently
/// fail and return `None` 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 = SecondaryMap::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 and return `None` 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 and return ` None ` 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();
        self.slots
            .extend((self.slots.len()..=kd.idx as usize).map(|_| Slot::new_vacant()));
        let slot = &mut self.slots[kd.idx as usize];
        if slot.version() == kd.version.get() {
            // Is always occupied.
            return Some(replace(unsafe { slot.get_unchecked_mut() }, value));
        }
        if slot.occupied() {
            // Don't replace existing newer values.
            if is_older_version(kd.version.get(), slot.version()) {
                return None;
            }
        } else {
            self.num_elems += 1;
        }
        *slot = Slot::new_occupied(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 ( ) ; self . slots . extend ( ( self . slots . len ( ) ..= kd . idx as usize ) . map ( | _ | Slot :: new_vacant ( ) ) ) ; let slot = & mut self . slots [ kd . idx as usize ] ; if slot . version ( ) == kd . version . get ( ) { // Is always occupied. return Some ( replace ( unsafe { slot . get_unchecked_mut ( ) } , value ) ) ; } if slot . occupied ( ) { // Don't replace existing newer values. if is_older_version ( kd . version . get ( ) , slot . version ( ) ) { return None ; } } else { self . num_elems += 1 ; } * slot = Slot :: new_occupied ( 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 = SecondaryMap::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 = SecondaryMap :: 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 .