MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / insert

Method insert

crates/data-structures/src/small_map.rs:49–66  ·  view source on GitHub ↗

Inserts the association `key -> value` into the map, returning the previous value for `key`, if any.

(&mut self, key: K, value: V)

Source from the content-addressed store, hash-verified

47 /// Inserts the association `key -> value` into the map,
48 /// returning the previous value for `key`, if any.
49 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
50 // Possibly convert to large map first.
51 self.maybe_convert_to_large();
52
53 match self {
54 Self::Small(list) => {
55 if let Some(idx) = Self::key_pos(list, &key) {
56 // SAFETY: `idx` was given by `key_pos`, so it must be in-bounds.
57 let (_, val) = unsafe { list.get_unchecked_mut(idx) };
58 return Some(mem::replace(val, value));
59 }
60
61 list.push((key, value));
62 None
63 }
64 Self::Large(map) => map.insert(key, value),
65 }
66 }
67
68 /// Returns either the existing value for `key`
69 /// or inserts into `key` using `or_insert`.

Callers

nothing calls this directly

Calls 3

insertMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected