MCPcopy Create free account
hub / github.com/cosdata/cosdata / get_or_insert_with

Method get_or_insert_with

src/models/atomic_array.rs:113–137  ·  view source on GitHub ↗
(&self, idx: usize, mut f: F)

Source from the content-addressed store, hash-verified

111 }
112
113 pub fn get_or_insert_with<F>(&self, idx: usize, mut f: F) -> (*mut T, bool)
114 where
115 F: FnMut() -> T,
116 {
117 let mut return_value = ptr::null_mut();
118 let mut v: *mut T = ptr::null_mut();
119 let res = self.items[idx].fetch_update(Ordering::SeqCst, Ordering::SeqCst, |existing| {
120 if existing.is_null() {
121 if v.is_null() {
122 v = Box::into_raw(Box::new(f()))
123 }
124 return_value = v;
125 Some(v)
126 } else {
127 return_value = existing;
128 None
129 }
130 });
131 if !v.is_null() && res.is_err() {
132 unsafe {
133 drop(Box::from_raw(v));
134 }
135 }
136 (return_value, res.is_ok())
137 }
138}

Callers 1

find_or_create_nodeMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected