pub struct SharedVec<T> { /* private fields */ }Expand description
A structure owning the original Vec which can be used to create multiple immutable
SharedVecRef values to send to other threads.
It uses a counter to record how many references have been created and not returned yet.
Dropping SharedVec without returning all SharedVecRef values leaks the underlying allocation.
When the panic-on-drop feature is enabled, it will panic:
let r = {
let mut values = manual_share::SharedVec::from_vec(vec![0]);
values.borrow()
};
println!("{:?}", r.as_slice());Once all SharedVecRef values have been returned, SharedVec can be converted back into
a Vec and its allocation will be released when dropped:
let mut values = manual_share::SharedVec::from_vec(vec![0]);
let reference = values.borrow();
values.try_return(reference).unwrap();
let values = values.try_into_vec().unwrap();
assert_eq!(values, vec![0]);Implementations§
Sourcepub fn borrow(&mut self) -> SharedVecRef<T>
pub fn borrow(&mut self) -> SharedVecRef<T>
Create a SharedVecRef and increase the borrow count.
let mut values = manual_share::SharedVec::from_vec(vec![1, 2, 3]);
let reference = values.borrow();
assert_eq!(reference.as_slice(), &[1, 2, 3]);
values.try_return(reference).unwrap();§panics
Panics when borrow count overflows usize.
Sourcepub fn try_return(
&mut self,
reference: SharedVecRef<T>,
) -> Result<(), SharedVecRef<T>>
pub fn try_return( &mut self, reference: SharedVecRef<T>, ) -> Result<(), SharedVecRef<T>>
Try to return back a SharedVecRef.
Returns Err if the SharedVecRef does not originate from the same SharedVec.
let mut first = manual_share::SharedVec::from_vec(vec![8]);
let first_ref = first.borrow();
first.try_return(first_ref).unwrap();
let mut second = manual_share::SharedVec::from_vec(vec![9]);
let second_ref = second.borrow();
let err = first.try_return(second_ref).unwrap_err();
assert_eq!(err.as_slice(), &[9]);
second.try_return(err).unwrap();Sourcepub fn try_into_vec(self) -> Result<Vec<T>, Self>
pub fn try_into_vec(self) -> Result<Vec<T>, Self>
Try to convert Self into a Vec once all borrowed references are returned.
let mut values = manual_share::SharedVec::from_vec(vec![0]);
let reference = values.borrow();
// Try to convert to Vec without returning all SharedVecRef returns Err.
let mut values = values.try_into_vec().unwrap_err();
values.try_return(reference).unwrap();
let values = values.try_into_vec().unwrap();
assert_eq!(values, vec![0]);