Skip to content

Instantly share code, notes, and snippets.

@dginev
Created September 30, 2015 21:42
Show Gist options
  • Save dginev/d80c1b8379ba1ea0981e to your computer and use it in GitHub Desktop.
Save dginev/d80c1b8379ba1ea0981e to your computer and use it in GitHub Desktop.
Trying to clone an owned Vec out of a shared Arc<Mutex<Vec>>
fn fetch_vec<T>(vec_arc: &Arc<Mutex<Vec<T>>>) -> Vec<T> {
let mut vec_mutex_guard = vec_arc.lock().unwrap();
let fetched_vec : Vec<T> = vec_mutex_guard.deref().clone();
vec_mutex_guard.clear();
fetched_vec
}
@dginev
Copy link
Author

dginev commented Sep 30, 2015

error: mismatched types:
 expected `collections::vec::Vec<T>`,
    found `&collections::vec::Vec<T>`
(expected struct `collections::vec::Vec`,
    found &-ptr) [E0308]

at

     let fetched_vec : Vec<T> = vec_mutex_guard.deref().clone();

@dginev
Copy link
Author

dginev commented Sep 30, 2015

Thanks to durka42 from IRC for the fix:
http://is.gd/alHwe2

@dginev
Copy link
Author

dginev commented Oct 1, 2015

Just to have it along the original Gist, copying the fully operational version here:

fn fetch_vec<T: Clone>(vec_arc: &Arc<Mutex<Vec<T>>>) -> Vec<T> {
  let mut vec_mutex_guard = vec_arc.lock().unwrap();
  let fetched_vec : Vec<T> = (*vec_mutex_guard).clone();
  vec_mutex_guard.clear();
  fetched_vec
}

And I used it to improve my shared reporting queue in the CorTeX build system here: dginev/CorTeX@0a4ed16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment