Expand description
§Manually shared vector
SharedVec is the Vec-based counterpart to SharedBox.
It owns the original allocation and lets you create multiple immutable SharedVecRef
values that can be sent to other threads while keeping a single shared owner.
The API is similar to SharedBox:
- use
SharedVec::from_vecto create a shared vector from aVec - use
SharedVec::borrowto create aSharedVecRef - use
SharedVec::try_returnto give a borrowed reference back - use
SharedVec::try_into_vecto recover the originalVeconce no references remain
use std::thread;
use manual_share::SharedVec;
let values = vec![1, 2, 3];
let mut shared = SharedVec::from_vec(values);
let shared_ref = shared.borrow();
let handle = thread::spawn(move || {
assert_eq!(shared_ref.as_slice(), &[1, 2, 3]);
shared_ref
});
let shared_ref = shared.borrow();
let handle2 = thread::spawn(move || {
assert_eq!(shared_ref.as_slice(), &[1, 2, 3]);
shared_ref
});
shared.try_return(handle.join().unwrap()).unwrap();
shared.try_return(handle2.join().unwrap()).unwrap();
let values = shared.try_into_vec().unwrap();
assert_eq!(values, vec![1, 2, 3]);Structs§
- Shared
Vec - A structure owning the original
Vecwhich can be used to create multiple immutableSharedVecRefvalues to send to other threads. It uses a counter to record how many references have been created and not returned yet. - Shared
VecMut - A container of a
Vecallocation that can be split into multipleSharedVecPartvalues. - Shared
VecPart - A slice-like view into a segment of a
SharedVecMutallocation. - Shared
VecRef - A reference to
SharedVecthat can be sent to other threads.