Skip to main content

SharedVec

Struct SharedVec 

Source
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§

Source§

impl<T> SharedVec<T>

Source

pub fn from_vec(vec: Vec<T>) -> Self

Create a SharedVec by consuming a Vec.

Source

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.

Source

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();
Source

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]);
Source

pub fn get(&self) -> &[T]

Directly get a slice to the values inside the SharedVec. This use rust built-in lifetime check to ensure the slice is valid as long as the SharedVec is alive, and has no runtime overhead.

Trait Implementations§

Source§

impl<T: Debug> Debug for SharedVec<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for SharedVec<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send> Send for SharedVec<T>

Source§

impl<T: Sync> Sync for SharedVec<T>

Auto Trait Implementations§

§

impl<T> Freeze for SharedVec<T>

§

impl<T> RefUnwindSafe for SharedVec<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for SharedVec<T>

§

impl<T> UnsafeUnpin for SharedVec<T>

§

impl<T> UnwindSafe for SharedVec<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.